call by Reference is possible?

Hai....Call by Reference is possible in Java passing variables into one function to other?Advance Wishes....
[129 byte] By [Daiesha] at [2007-10-3 8:50:06]
# 1
in java everything is pass by value
java_usera at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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

Daiesha at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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_usera at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks for ur suggestion....
Daiesha at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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

evnafetsa at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
why this statement x.append (y); value only changed ?In these statement y = x; why it doesn't change?Because both are object reference.
Daiesha at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

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

johannefa at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
> Hai....> > Call by Reference is possible in Java passing> variables into one function to other?> > Advance Wishes....hi :-)Java only supports pass by value :-)regards,
jie2eea at 2007-7-15 3:59:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...