Jesper de Jong wrote:What is the output of the first program: Did you try running it? It shows that in Java, parameters are passed by value. See Pass-by-Value Please.
The second program shows a clever trick for swapping two values without using a temporary variable. Note that it's just a clever trick, and it has no practical advantage - you should never use this trick in any really useful program.
Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion
Jesper de Jong wrote:
Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion
Ok, first let's make it a complete program, otherwise it's hard to know exactly what we are talking about.
You say you understand what pass by value means, so you should be able to tell what the output of this program will be. What do you think will this print and why?
For the sorting program, please provide a complete working program yourself, and if it doesn't do what you expect, explain in detail what you expected and how that's different from what the program outputs.
VM
Vineeth Menon wrote:try this link
Will Zelan wrote:
Jesper de Jong wrote:
Will Zelan wrote:i didnt got any output,whats the output will come?i understand abt pass by value but litle bit confusion
Ok, first let's make it a complete program, otherwise it's hard to know exactly what we are talking about.
You say you understand what pass by value means, so you should be able to tell what the output of this program will be. What do you think will this print and why?
iam still confused abt passby value,i dnt know whats output will come,it was interview question,so is that program corect?whats the output will come i think pqrxyz is the output,bt now iam totally confused?
First, to avoid confusion, let's rename the parameter that the update method takes:
When you call update in line 4, the value of the variable str is passed to the update method, and is assigned to the parameter variable which I've called something in the code above. When you change the variable something in line 9, it will not have any effect on the variable str in the main method. So when the update method returns, and the program continues on line 5, the variable str will still hold the value that it had before the update method was called. So the output is: pqr
Will Zelan wrote:what about xyz values where it will be passed?
Will Zelan wrote:so where the loop goes again now
Jesper de Jong wrote:
Will Zelan wrote:what about xyz values where it will be passed?
It's not passed anywhere. When the update method returns, the variable something goes out of scope and is thrown away. The update method has a misleading name; it really does not have any effect at all.
Will Zelan wrote:so where the loop goes again now
It simply executes the next iteration of the loop over k (line 3), and if k becomes 5, it executes the next iteration of the loop over j (line 1).
Put some System.out.println() statements in your code to print the values of j and k and certain points and then run the program, then you'll see how the flow goes.
Will Zelan wrote:
Jesper de Jong wrote:
Will Zelan wrote:what about xyz values where it will be passed?
It's not passed anywhere. When the update method returns, the variable something goes out of scope and is thrown away. The update method has a misleading name; it really does not have any effect at all.
Will Zelan wrote:so where the loop goes again now
It simply executes the next iteration of the loop over k (line 3), and if k becomes 5, it executes the next iteration of the loop over j (line 1).
Put some System.out.println() statements in your code to print the values of j and k and certain points and then run the program, then you'll see how the flow goes.
yeah thankx it helps i understand,but i need some clarification that is
in the above program if i give the input 5 4 2 3 1 then output will be 1 2 3 4 5
so here we see the loop process how it goes
here actually my doubt is what value will be print a[j] at a[0] index which one and why?1 will be printed when i check the output but how it determines to print that value
Start outer loop, j = 0
Start inner loop, j = 0 and k = 0
End inner loop, j = 0 and k = 0
Start inner loop, j = 0 and k = 1
The array contains: [5, 4, 2, 3, 1]; swapping a[0] and a[1]
After swapping, the array contains: [4, 5, 2, 3, 1]
End inner loop, j = 0 and k = 1
Start inner loop, j = 0 and k = 2
The array contains: [4, 5, 2, 3, 1]; swapping a[0] and a[2]
After swapping, the array contains: [2, 5, 4, 3, 1]
End inner loop, j = 0 and k = 2
Start inner loop, j = 0 and k = 3
End inner loop, j = 0 and k = 3
Start inner loop, j = 0 and k = 4
The array contains: [2, 5, 4, 3, 1]; swapping a[0] and a[4]
After swapping, the array contains: [1, 5, 4, 3, 2]
End inner loop, j = 0 and k = 4
End outer loop, j = 0
a[0] = 1
Start outer loop, j = 1
...
Will Zelan wrote:... there is value of a[j] gives 4 types of values are there on that how 1 will be printed
Jesper de Jong wrote:If you've added the println statements, compiled and ran the program and looked at the output, you would see exactly what it is doing, and when in the process it's printing 1. You just have to carefully trace the steps to see what exactly is happening.
I added a few more println statements to print the content of the array, and the output now looks like this:
Start outer loop, j = 0
Start inner loop, j = 0 and k = 0
End inner loop, j = 0 and k = 0
Start inner loop, j = 0 and k = 1
The array contains: [5, 4, 2, 3, 1]; swapping a[0] and a[1]
After swapping, the array contains: [4, 5, 2, 3, 1]
End inner loop, j = 0 and k = 1
Start inner loop, j = 0 and k = 2
The array contains: [4, 5, 2, 3, 1]; swapping a[0] and a[2]
After swapping, the array contains: [2, 5, 4, 3, 1]
End inner loop, j = 0 and k = 2
Start inner loop, j = 0 and k = 3
End inner loop, j = 0 and k = 3
Start inner loop, j = 0 and k = 4
The array contains: [2, 5, 4, 3, 1]; swapping a[0] and a[4]
After swapping, the array contains: [1, 5, 4, 3, 2]
End inner loop, j = 0 and k = 4
End outer loop, j = 0
a[0] = 1
Start outer loop, j = 1
...
oh ok i understand bt how it swaps the indexes which code be used to swap
Will Zelan wrote:... there is value of a[j] gives 4 types of values are there on that how 1 will be printed
What does that mean, "a[j] gives 4 types of values"?
Ivan Jozsef Balazs wrote:> That is a party trick whose performance is slower than a temporary variable;
... and in contrary to the temporary variable solution, is prone to over- or underflow with some values.
I didn't check it precisely but I think that with this trick the over- and underflow will compensate for each other, so that it still works with all values.
Will Zelan wrote:
k if we use temp variable like the above,how it will done swap,i mean temp=a[0],then a[0]=a[1] so it means a[0]will swap to a[1],and a[1]=temp it means previously temp is a[0] so it will be swaped by a[1],is it corect
Jesper de Jong wrote:
Will Zelan wrote:
k if we use temp variable like the above,how it will done swap,i mean temp=a[0],then a[0]=a[1] so it means a[0]will swap to a[1],and a[1]=temp it means previously temp is a[0] so it will be swaped by a[1],is it corect
I wouldn't say "will swap to" - the = operator does assignment, it doesn't swap anything.
Line 1: Assign the current value of a[j] to temp.
Line 2: Assign the current value of a[k] to a[j]. Note that a[j] now contains the same value as a[k].
Line 3: Assign the value of temp, which is the old value of a[j], to a[k].
End result: The values of a[j] and a[k] are swapped (a[j] now contains what a[k] contained first and vice versa).
Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )
Jeff Verdegan wrote:
Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )
Nothing is interchanged. Saying "interchanged" implies two things switch places. All that happens here--in any assignment statement in Java*--is that the value of the expression on the RHS is copied into the variable on the LHS.
*other than possibly some automatic type conversions, such as widening an int to a long
Will Zelan wrote:
Jeff Verdegan wrote:
Will Zelan wrote:
a[j]=a[k]-->a[0]=a[1];(which is a[1]values is equal to a[0] which means the values interchanging so 4 moved to a[0]index )
Nothing is interchanged. Saying "interchanged" implies two things switch places. All that happens here--in any assignment statement in Java*--is that the value of the expression on the RHS is copied into the variable on the LHS.
*other than possibly some automatic type conversions, such as widening an int to a long
k above example is my logic is correct or not?it means a[j] value is copied to a[k]and a[k]value is copied to a[j] so is it right
The third line copies the value from a[k] into temp. So now a[k] holds the same value as temp (a[j]'s original value( and a[j] still holds a[k]'s original value, which it got assigned in the previous step.
Jesper de Jong wrote:Suppose that a[0] = 5, a[1] = 4, a[2] = 2.
Line 1: Copy the value of a[0] to temp, so now we have: temp = 5, a[0] = 5, a[1] = 4, a[2] = 2.
Line 2: Copy the value of a[1] to a[0], so now we have: temp = 5, a[0] = 4, a[1] = 4, a[2] = 2.
Line 3: Copy the value of temp to a[1], so now we have: temp = 5, a[0] = 4, a[1] = 5, a[2] = 2.
End result: a[0] = 4, a[1] = 5, a[2] = 2.
It's simple. Don't make it harder than it is.
Will Zelan wrote:
The third line copies the value from a[k] into temp. So now a[k] holds the same value as temp (a[j]'s original value( and a[j] still holds a[k]'s original value, which it got assigned in the previous step.
in that line copies values from temp into a[k] or a[k] into temp?
Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me
Jesper de Jong wrote:
Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me
What do you think the answer to that question is yourself?
Will Zelan wrote:
Jesper de Jong wrote:
Will Zelan wrote:i had doubt in 14 and 22 nd line,i know it is the method which do some functions,in 14th line what are passing and 22nd line what is inta[] and int n can you tell me
What do you think the answer to that question is yourself?
passing the parameter as array and array length in 14th line,will you tell me
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|