• 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

reference variable & object

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not still able to catch the concept. Can you give me an example....?

Regards,
ABHIJIT
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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 !!!

 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic