Abhijit Kangale wrote:Hi all,
Since I m new to java, I m not getting difference between reference variable & object. Can anyone explain me with an example.
Thanks & Regards,
ABHIJIT
Abhijit Kangale wrote:I am not still able to catch the concept. Can you give me an example....?
Regards,
ABHIJIT
Abhijit Kangale wrote:Since I m new to java, I m not getting difference between reference variable & object. Can anyone explain me with an example.
Muhammad Ali Khojaye wrote:
A reference is an alias to another variable. Any manipulation done to the reference variable directly changes the original variable.
....
Check out my blog on software development: http://www.turnleafdesign.com
SCJP 6.0 96%
(Connecting the Dots ....)
Billy Korando wrote:I actually just written an article on my site going over and fairly good detail of how Java handles objects, http://www.turnleafdesign.com/?p=5. I hope it helps any questions you might have. If you have any more questions the article didn't answer please ask.
Fred Hamilton wrote:
a pass to a method creates a new reference variable that refers to the original object, not the original variable. It contains a copy of the reference to that object, if that's what you mean.
I just distinquish between a reference variable and its content, which is a reference to an object.
Fred Hamilton wrote:
Billy Korando wrote:I actually just written an article on my site going over and fairly good detail of how Java handles objects, http://www.turnleafdesign.com/?p=5. I hope it helps any questions you might have. If you have any more questions the article didn't answer please ask.
I quote the following from your article...
----------------------------
Remember
When you assign an object variable to another object variable you are not copying the value of the assigner variable to the assignee, but the reference. What this means is when you change the value of either variable, you change the value for both variables as shown in the example below:
-----------------------------
May I clarify one point here? Your example seems clear enough, a & b are reference variables ( they contain references to the object, i.e. the values of a & b are references). If you mean a is an assigner variable, then indeed its value is the reference. When you change the value ( the contents, the reference ) of either reference variable, it is changed for that reference variable only. In your example you changed an instance variable of the object. Which is something a little different. Now because both a & b refer to the same object, then when you change any instance variable of the object, that change affects both a & b.
See the difference?
p.s. in my description, when i say the variable contains a reference, it means it contains a pointer of sorts to another memory location where the actual object is stored.
Check out my blog on software development: http://www.turnleafdesign.com
Muhammad Ali Khojaye wrote:
Fred Hamilton wrote:
a pass to a method creates a new reference variable that refers to the original object, not the original variable. It contains a copy of the reference to that object, if that's what you mean.
I just distinquish between a reference variable and its content, which is a reference to an object.
Yes and its already quoted.
Billy Korando wrote:
Fred Hamilton wrote:
Billy Korando wrote:I actually just written an article on my site going over and fairly good detail of how Java handles objects, http://www.turnleafdesign.com/?p=5. I hope it helps any questions you might have. If you have any more questions the article didn't answer please ask.
I quote the following from your article...
----------------------------
Remember
When you assign an object variable to another object variable you are not copying the value of the assigner variable to the assignee, but the reference. What this means is when you change the value of either variable, you change the value for both variables as shown in the example below:
-----------------------------
May I clarify one point here? Your example seems clear enough, a & b are reference variables ( they contain references to the object, i.e. the values of a & b are references). If you mean a is an assigner variable, then indeed its value is the reference. When you change the value ( the contents, the reference ) of either reference variable, it is changed for that reference variable only. In your example you changed an instance variable of the object. Which is something a little different. Now because both a & b refer to the same object, then when you change any instance variable of the object, that change affects both a & b.
See the difference?
p.s. in my description, when i say the variable contains a reference, it means it contains a pointer of sorts to another memory location where the actual object is stored.
I think I understand the point you are trying to make. I was trying to illustrate the difference between how a primitive data type would work and how Object variables work. If I was using ints and then b would get a copy of the value of a and any changes made to either of them would not affect the value stored in the other variable.
So is the point you are trying to make is that I should explain about de-referencing? i.e.
String a = "test";
String b = a;
a = "test1";
In the example above b still references the value "test" while a references the value "test1"? I have been a bit removed from using the formal Java terminology. Should I start including hat in my posts?
EDIT:
Thanks for critiquing my article. I want to have what I have on my site to be as accurate as possible.
Consider Paul's rocket mass heater. |