then how this code o/p value will be "AB,B"?
1. public class Foo {
2. public static void main (String [] args) {
3. StringBuffer a = new StringBuffer (揂?;
4. StringBuffer b = new StringBuffer (揃?;
5. operate (a,b);
6. system.out.printIn(a + ?+b);
7. }
8. static void operate (StringBuffer x, StringBuffer y) {
9. x.append (y);
10. y = x;
11. }
12. }
how its output should be "AB,B"
Message was edited by:
Daiesh
Hi ,
I suggest u dont jump directly on the questions....
If u r planning for the certification then pls go thru the ebooks and study material first.
for ur info .....
"A" is inside a
"B" inside b
now whent it invokes method
"x" is pointing to "a" block means hving "A"and
"Y" is pointing to "b" block means hving "B"and
after x.append(y).
inside block x and a value becomes "AB"
after y=x; means now y is pointing to "x" block
now come back to ur main method
when it prints a n b u will get AB n B
Java is ALWAYS pass by value.
When you are passing objects, it passes the object reference by value.
You can change the contents of that object, and the changes will be reflected in the calling method.In your example, the stringBuffer.append call is an example of that.
However if you assign a completely new value to that parameter, THAT change won't be reflected in the calling class
So assigning y = x changes it in your method, but has no effect on the calling method.
Does that help?
evnafets
Because in x=y, you are updating the x variable to point to another value. This change is (naturally) limited to the scope of x.
In x.append(y), you are not updating the x variable, but the object x refers to. So this change will be visible for all references to that object, even when x has passed from scope.
Message was edited by:
johannef