• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....

 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post a reference for your thesis that classes, or objects, are passed by reference in Java.
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic