wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes reference variable & object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reference variable & object" Watch "reference variable & object" New topic
Author

reference variable & object

Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
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
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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


Well, this is kind of an important concept to understand, if you want to make progress with java.

Think of a reference variable as a name for an object. It's a little more complicated than that, I guess. But that is a pretty good place to start.

Here's another way to think about it. An object is a collection of data. A reference variable is a name for that data. In most cases, you need to have a name for the data so you can refer to it, or use it.
Abhijit Kangale
Ranch Hand

Joined: Sep 17, 2009
Posts: 39
I am not still able to catch the concept. Can you give me an example....?

Regards,
ABHIJIT
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Abhijit Kangale wrote:I am not still able to catch the concept. Can you give me an example....?

Regards,
ABHIJIT


It's not so easy to give a simple example that will make it all clear. Maybe someone else has a better answer

I think the following web pages might help you a lot.

http://java.sun.com/docs/books/tutorial/java/concepts/index.html

http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.

A reference is an alias to another variable. Any manipulation done to the reference variable directly changes the original variable.

However in java, objects are not passed by reference, but their references are passed by value.

In this example aDog.name will still be "Max". "d" is not overwritten in the function as the object reference is passed by value.

Likewise:

The point to remember is that the objects themselves are never passed to a method, but the objects are always in the heap and only a reference to the object is passed to the method.

Reference here


http://muhammadkhojaye.blogspot.com/
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Muhammad Ali Khojaye wrote:
A reference is an alias to another variable. Any manipulation done to the reference variable directly changes the original variable.
....



oh? A reference is an alias to another variable? That's a new one for me. The original question asked about a reference variable. I don't see how this explanation illuminates anything.

If you are talking about a method call, 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.
Billy Korando
Greenhorn

Joined: Sep 18, 2009
Posts: 15
I actually just written an article on my site going over in fairly good detail of how Java handles objects, http://www.turnleafdesign.com/?p=5. I hope it helps answers questions you might have. If you have any more questions please ask.


Check out my blog on software development: http://www.turnleafdesign.com
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@ Abhijeet

Well think this way...it wuld help you !!!

Refernce Variable is like a " Dummy Remote Control". Dummy means it is not programmed to control any TV. (Television)...Okay
Object is like a TV in our context.

When i say ..... String s; This implies i am creating a "Dummy Remote Cotrol". Obviously this REMOTE CONTROL can just operate any STRING TV. But i have not still said which particular STRING TV.

When i say...... new String(); This Implies i am purchasing one New STRING TV.

When i say...... s= new String(); This implies that i am Purchasing a New String TV and "assigning its functionality( How to operate it) to the STRING Remote Control which i purchased earlier"

Hope you get an idea.

NOTE: the above theory is valid for every object and i have just taken an example of STRING class already shiped with java.lang package !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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, (i.e they are reference variables for the same object) then when you change any instance variable of the object, that change affects both a & b. But it is not the "value" of a or b that you are changing when you assign a first name of Joe.

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.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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 .

If we talk about objects then the objects themselves are never passed to a method, but the objects are always in the heap and only a reference to the object is passed to the method.
Billy Korando
Greenhorn

Joined: Sep 18, 2009
Posts: 15
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.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.


Is it? My point was that a reference in the context of this thread a reference is hardly an alias to another variable. In the case of a pass to a method. Then the variable in the method refers not to the original variable, but to the object. I suspect you realize that perfectly well, but we just don't agree on the definition of alias. I might be wrong on my definition of alias. Disagreements based on semantics are common for me, I guess I am wrong about as often as I am right when it comes to semantics.

regards.

p.s. ok suppose there was no method call, just the object and it's reference variable. In that case there would be no other variable for the reference to be an alias of, if we are to go by your definition of alias.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.


I think we are more or less on the same page. I just was making the point that as I see it, the values (contents) of a and b are references, and they do not change in your example. If the contents of a was changed, then it would no longer point to that object (person), regardless of whether or not the name was changed to Joe. But b would still point to the person

I guess it depends what you mean when by values. As I see it, and most others IMO, the values of a & b are the references themselves, ( or pointers if you prefer) it does not matter if the first name of the person is changed.

ok I re-read your article. I should mention that when I use the word reference, I mean the same thing as what you call a pointer in your article. But is is still the pointer that is the value of a.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reference variable & object
 
Similar Threads
No. of Objects & Reference Variables
ZooKeeper
help-SCJP-65
Diff b/w ref variable & object ref
Inner Class