• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refrence Variable and object question  RSS feed

 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, lets suppose there is a class Num(). When I create an reference objects and set them equal to each other and then I create another reference object assigning to one of the earlier object. Now, shouldnt all the object have the same memory address?


 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first half is correct: there's only one Num object and both i and k refer to it. So far, so good.

But not the second half, as you already know from the output: now there are two Num objects. The second one, originally w refers to it and then you make i refer to it as well. So now i doesn't refer to the first one any more, but k still refers to the first one.

I assume that matches what you're seeing in your output?
 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the output is i and w have the same address. I think I'm confusing this question with pass by reference?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reference variables are like notecards, where you write down a n address hashcode.

So, on line 11, you say "erase what's on notecard "i", and write on it what's written on "w"". 

That will have zero effect on what's written on card k.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Lee wrote:and then I create another reference object assigning to one of the earlier object.


Confusing, yes. First of all "reference object" doesn't mean anything in Java. You can create an object, and you can create a reference to an object. So "Num w = new Num()" creates a Num object (that's the right-hand side of the assignment) and then it creates a reference to that object and assigns it to the variable w (that's the left-hand side of the assignment).

And second, you can't assign an object to another object. In fact you can't assign an object to anything at all, nor can you assign anything to an object. You can only work with references to objects. Although I'm sort of guessing at what "assigning to one of the earlier objects" might mean to you.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Lee wrote:Yes, the output is i and w have the same address. I think I'm confusing this question with pass by reference?
You cannot actually see the address of an object in Java®. Do you mean they have the same hashcode as appears when you print them?
Pass by reference is something completely different. It doesn't exist in Java®.
 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, You guys are very helpful. I really appreciate you taking your time to answer.
 
Bobby Lee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Campbell, I meant when I print out the objects I get  hashcodes, people refer those hashcode to address, isn't it?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashcodes are not addresses.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Lee wrote:people refer those hashcode to address, isn't it?

I am guilty of this, but I am sloppy.

I have edited my post above to correctly use the term "hashcode".
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!