pass by reference

do java support pass by reference?I understand that all the premitive data types are passed by value and objects are passed by reference. Is it correct.
[166 byte] By [RYALIa] at [2007-10-3 3:32:24]
# 1
> do java support pass by reference?No.> I understand that all the premitive data types are> passed by value and objects are passed by reference.> Is it correct.It's wrong. Go and search, this is discussed twice a week.
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 2
patterns & oo design forumyou can't miss the thread, it's gone into 1326 posts now
r035198xa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 3

Quote:

Everything in Java is passed "by value". Everything.

Pass-by-value

- When an argument is passed to a function, the invoked function gets a copy of the original value.

- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.

- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- James Gosling, et al., The Java Programming Language, 4th Edition

PhHeina at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 4
"The Java programming language does not pass objects by reference; it passes object references by value. "I got confused and not able to understand the above sentence. especiallypass objects by reference and pass object references by value what is the difference.
RYALIa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 5

hai

most of the guys are given that there will be no question of pass by reference in java. but when u r try to pass the object to the method, u have to access through the object reference only..

I can say that all object interaction will hapen through the object reference only..

regards & thanks..

vijay

vijay_sekhara at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 6

> hai

> most of the guys are given that there will be no

> question of pass by reference in java. but when u r

> try to pass the object to the method, u have to

> access through the object reference only..

Still, there is no pass by reference. And you dont't pass objects anyway.

> I can say that all object interaction will hapen

> through the object reference only..

Yes. So what?

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 7

> "The Java programming language does not pass objects

> by reference; it passes object references by value.

> "

>

>

> I got confused and not able to understand the above

> sentence. especially

>

> pass objects by reference and pass object references

> by value what is the difference.

Both the actual argument, and the way it is passed, are different between the two. Why don't you tell us what you think they do have in common?

Lokoa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 8
> when u r> try to pass the object to the method...... then u r spking thngs that donot exst in teh jav
Lokoa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 9
[url= http://www.javaranch.com/campfire/StoryCups.jsp]Javaranch: Cup Size -- a story about variables[/url][url= http://www.javaranch.com/campfire/StoryPassBy.jsp]Javaranch: Pass-by-Value Please (Cup Size continued)[/url]
mlka at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 10

> pass objects by reference and pass object references

> by value what is the difference.

The swap method.

Pass by References means the following would work:

public void swap( String a, String b ) {

String c = a;

a = b;

b = c;

}

String a = "a";

String b = "b";

swap( a, b );

System.out.println( "a=" + a );

System.out.println( "b=" + b );

Now if Java implemented PbR then it would output

a=b

b=a

mlka at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 11

class Point{

int x;

public Point(){

x = 10;

}

}

public class Main{

public static void main(String[] args){

String word = "Hello";

Point p = new Point();

passValue(word);

passValue(p);

System.out.println("String : " + word);

System.out.println("x : " + p.x);

}

public static void passValue(String word){

word = "Java";

}

public static void passValue(Point point){

point.x = 20;

point = new Point();

}

}

You can look that point cannot change the reference. So java pass object as pass object reference by value. You can change the value of the object

Bina_Nusantaraa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 12

> You can look that point cannot change the reference.

> So java pass object

Incorrect.

>as pass object reference by

> value. You can change the value of the object

The variable is the object reference (not the object) and the method works on a copy if it. Changing "the value of the object" is irrelevant.

Lokoa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 13
Hey i got the answer from this disuccion and for the link below as well. http://www.javaworld.com/gomail.cgi?id=659058
RYALIa at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 14
> Hey i got the answer from this disuccion and for the> link below as well.> > http://www.javaworld.com/gomail.cgi?id=659058Congrats. :) And if you want a re-run, just wait three days until the next person comes along asking the same.
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:38 > top of Java-index,Java Essentials,New To Java...
# 15
Dear mlkThe link one you gave is quite a good one with clear pictorial demonstration. It will make me remember this point for ever.Thank you one and all who have participated in this disucssion and help me out.
RYALIa at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 16

hai

I got a clear picture of the process. When u r passing reference of the object, u will send the address of the object. The address will pass by value only. I can sya in other words like

Object obj stored at 1000 memory location. This 1000 value stored at 2000 location as reference to obect. Now When u r passing the object, u will pass 1000. In the method execution it will create a refernce at 3000 & it will store the value 1000. Now this methos can access the object with value 1000 only. So u r passing the reference using pass by value.

Am I correct? If any modification in my understanding, pls do inform to me.

regards & thanks..

vijay

vijay_sekhara at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 17
sorry, but my brain is not equipped to decrypt the results of your brain's encryption algorithm.
jwentinga at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 18

> Am I correct? If any modification in my

> understanding, pls do inform to me.

You may be interested in looking at this from a VM instruction perspective...

Despite the fact that the answer to the question of Java's parameter passing semantics does not depend on implementation, it's interesting to see what is actually happening underneath the covers, as specified by the Virtual Machine specification. Take this class, for instance:

class Foo {

void a() {

Object obj = new Object();

b(obj);

}

void b(Object x) { }

}

Here is the relevant javap output for this class, broken down by method:

void a() {

/*

* Stack=2, Locals=2, Args_size=1

*/

Object obj = new Object();

/* new #2;//Object

* dup

* invokespecial #1; // Method <init>

* astore_1

*/

b(obj);

/* aload_0

* aload_1

* invokevirtual #3; // Method b

* return

*/

}

void b(Object x) {

/*

* Stack=0, Locals=2, Args_size=2

*/

}

Let's take a closer look at the instructions for method a() to see how parameters are passed to method b():

void a();

// On this method's invocation, the local variables are

// initialized with arguments sent by the caller. In this

// method, there are two locals, but only one argument

// allowed. The first local is used to store an object

// reference sent by the caller. In this case, it's "this".

// The second local is used as a storage area during method

// execution.

// Since we cannot know the value of the reference stored

// in local variable #0, we'll call it "0xAAAA", and know

// that it is a pointer to "this" object on the heap. The

// value is not "this" object itself.

: [variables: ( 0xAAAA ) ( )] [stack: { }]

:

: new #2;// Creates a new instance of java.lang.Object, with memory

// allocated from the heap. A reference to this instance

// is pushed onto the stack. Since we cannot know the

// value of this reference in Java, we'll just call it

// "0xXXXX", and know that it is a pointer to the new

// object on the heap, not the new object itself.

: [variables: ( 0xAAAA ) ( )] [stack: { 0xXXXX }]

:

: dup// Duplicates the top operand stack value, and pushes the

// duplicated value onto the stack.

: [variables: ( 0xAAAA ) ( )] [stack: { 0xXXXX, 0xXXXX }]

:

: invokespecial #1; // Invokes the <init> method, popping the object reference

// from the stack, to initialize the object pointed to by

// the reference 0xXXXX.

: [variables: ( 0xAAAA ) ( )] [stack: { 0xXXXX }]

:

: astore_1 // Pops the object reference from the top of the stack,

// and stores it in variable #1.

: [variables: ( 0xAAAA ) ( 0xXXXX )] [stack: { }]

:

: aload_0// Loads the object reference from variable #0 (this) and

// pushes it onto the stack

: [variables: ( 0xAAAA ) ( 0xXXXX )] [stack: { 0xAAAA }]

:

: aload_1// Loads the object reference from variable #1 and pushes

// it onto the stack

: [variables: ( 0xAAAA ) ( 0xXXXX )] [stack: { 0xAAAA, 0xXXXX }]

:

: invokevirtual #3; // Creates a new frame for method b(), pops the arguments

// from the stack, and stores those values in the local

// variables of the new frame. The local variables in the

// new frame for b() will look like this:

// [variables: ( 0xAAAA ) ( 0xXXXX )]

// Note that these variables are copies of the variables

// from this stack frame. If the method b() modifies the

// values it has received, it will not affect the variables

// in this stack frame.

: [variables: ( 0xAAAA ) ( 0xXXXX )] [stack: { }]

:

: return// Returns void from this method, discarding the current

// frame, and reinstates the calling stack frame as the

// current frame.

void b(Object):

// As before, the local variables are initialized with

// values sent by the caller. If we're continuing with

// the example above, we can picture ourselves in a

// new frame (pun intended) with the following locals:

: [variables: ( 0xAAAA ) ( 0xXXXX )] [stack: { }]

// These are different variables from those in the previous

// frame, yet they contain the same values. In this case,

// the values are object references, pointing to the same

// objects pointed to by the variables in the previous

// frame.

:

: return// As above.

For a simple, more graphic representation of what's going on:

VM STACKHEAP

a(): initial state

for purposes of illustration,

assume "this" object is the

caller.

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | ++

| [] |

++

| op stack |

| [] |

| [] |

++

a(): new #2;

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [] | | |

++ ++

| op stack |

| [0xXXXX] |

| [] |

++

a(): dup

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [] | | |

++ ++

| op stack |

| [0xXXXX] |

| [0xXXXX] |

++

a(): invokespecial #1;

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [] | | |

++ ++

| op stack |

| [0xXXXX] |

| [] |

++

++

| FRAME init |

++

| =locals= | // details of <init> are

| [0xXXXX] | // undisclosed

++

||

++

<init>(): initialize and return

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [] | | |

++ ++

| op stack |

| [0xXXXX] |

| [] |

++

a(): astore_1

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [0xXXXX] | | |

++ ++

| op stack |

| [] |

| [] |

++

a(): aload_0

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [0xXXXX] | | |

++ ++

| op stack |

| [0xAAAA] |

| [] |

++

a(): aload_1

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [0xXXXX] | | |

++ ++

| op stack |

| [0xAAAA] |

| [0xXXXX] |

++

a(): invokevirtual #3;

++

|CALLER FRAME|

++

++ ++

| FRAME a() | | |

+++--+--> 0xAAAA "this"|

| =locals= ||| |

| [0xAAAA]--+-++-+--> 0xXXXX Object|

||||| |

| [0xXXXX]--+--+++

++||

||

++||

| FRAME b() |||

++||

| =locals= |||

| [0xAAAA]--+-+|// Different variables,

|||// same reference values,

| [0xXXXX]--+--+// pointing to the objects

++ // on the heap.

b(): return

++

|CALLER FRAME|

++

++ ++

| FRAME a| | |

++ |0xAAAA "this"|

| =locals= | | |

| [0xAAAA] | |0xXXXX Object|

| [0xXXXX] | | |

++ ++

| op stack |

| [] |

| [] |

++

a(): return

++ ++

|CALLER FRAME| | |

++ |0xAAAA "this"|

| |

|0xXXXX Object|

| |

++

So, there you have it: pass-by-value from a Java VM perspective. I hope you find some value in it! :)

~

yawmarka at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 19

> hai

> I got a clear picture of the process.

Okay...

> Now When u r passing the object

Again, objects are not stored in variables, and they are not passed as arguments, in Java.

There are two kinds of types in the Java programming language: primitive types (4.2) and reference types (4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (4.2) and reference values (4.3).

[url]http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.1[/url]

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

[url]http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1[/url]

Lokoa at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 20
Nice bit of ASCII art Yawmark.
mlka at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 21
we can only pray the darth vader (dafei'er) won't strike
mchan0a at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...
# 22
> Nice bit of ASCII art Yawmark.I thought it was fairly pathetic myself, but thanks for the compliment. :o)~
yawmarka at 2007-7-21 10:12:28 > top of Java-index,Java Essentials,New To Java...