Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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....

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post a reference for your thesis that classes, or objects, are passed by reference in Java.
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Posts: 5329
48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5329
48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic