This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes If Classes are passed by reference why doesnt this work ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If Classes are passed by reference why doesnt this work ??" Watch "If Classes are passed by reference why doesnt this work ??" New topic
Author

If Classes are passed by reference why doesnt this work ??

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124



I was expecting the output of the final line to be 65 but its 24 why ??
since refa points to or references refb (which now has a different value) wasnt refa suppose to display the new value ??

When it comes to dealing with other classes that i created the above concept works.. Is there something that i dont know about these wrapper classes?? Any explaination or help would be helpful....


Don’t look where you fall, but where you slipped
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Post a reference for your thesis that classes, or objects, are passed by reference in Java.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Hi Adam

Integer and other wrapper classes behave differently from regular classes because the wrapper objects are immutable.

So when you say refa = refb; then both references refer to same immutable object having value 24.

But when you write refb = 65; then you are not modifying the value in the original object (which is holding value 24) because it is immutable.

Internally it is something like this: refb = new Integer(65);

But refa still refers to an Integer object having value 24.


Piyush
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Piyush Joshi wrote:Hi Adam

Integer and other wrapper classes behave differently from regular classes because the wrapper objects are immutable.

So when you say refa = refb; then both references refer to same immutable object having value 24.

But when you write refb = 65; then you are not modifying the value in the original object (which is holding value 24) because it is immutable.

Internally it is something like this: refb = new Integer(65);

But refa still refers to an Integer object having value 24.


Very well explained... thanks for the help...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

First of all, the "pass by reference" statement is for parameter passing. There is no passing of anything with assignments.

To answer your question. This statement....



Assigns refa to point to the same object that is pointed to by refb. The refa and refb variables are two different variables that now happens to have the same value. Changing one of those variables later to point to something else doesn't magically change the other variable.

Of course, you have autoboxing mixed into your example, which may be adding to the confusion.


[EDIT: Missed the previous answer -- I guess I should have reloaded the page first.]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3387
    
    9
In Java there is no such thing as pass by reference. Everything is passed by value.

For objects, the assignment operator assigns the memory location of the object to the variable. When you assign refb to refa, they will both point to the same object (the Integer 24).
Now, when you assign 65 to refb, refb will point to this new Integer object. But this doesn't change the value stored in refa, which is still the memory location of the Integer 24.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Stephan van Hulst wrote:In Java there is no such thing as pass by reference. Everything is passed by value.

For objects, the assignment operator assigns the memory location of the object to the variable. When you assign refb to refa, they will both point to the same object (the Integer 24).
Now, when you assign 65 to refb, refb will point to this new Integer object. But this doesn't change the value stored in refa, which is still the memory location of the Integer 24.


Thanks for pointing that out.. All the folks here gave out an excellent explanation.
Looks like i was simply unaware of the mere fact that when I write

refb = 65 (changing from the original value)
The compiler doesnt write it on its original address and instead goes like
refb = new Integer(65); //thus having a new address altogether

Courtesy : Piyush Joshi
and the previous allocated unreferenced memory block of refb (24) will hopefully be cleared by the GC (Garbage collector)



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3387
    
    9
Actually, it doesn't.

When you assign a primitive literal to an Integer variable, it's closer to Integer.valueOf(65) than new Integer(65);

The valueOf method will cache certain values, so the garbage collector does not clean them up. Anyway, this isn't something you should have to concern yourself about. It's just a bit of an optimization by Java.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Stephan van Hulst wrote:Actually, it doesn't.

When you assign a primitive literal to an Integer variable, it's closer to Integer.valueOf(65) than new Integer(65);

The valueOf method will cache certain values, so the garbage collector does not clean them up. Anyway, this isn't something you should have to concern yourself about. It's just a bit of an optimization by Java.


Thanks for pointing out the actual mechanism for autoboxing.

I found a good reference for clearing my concepts on autoboxing (may be someone will find it useful): http://chaoticjava.com/posts/autoboxing-tips/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If Classes are passed by reference why doesnt this work ??
 
Similar Threads
String problems
hashCode
reference type URGENT!!!
Ques on casting referances ...
Ploymorphism code in this example? Please Explain.