• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object Reference Variable vs Primitive variables  RSS feed

 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

What I understand is that primitive variables store the actual values whereas reference variables store the addresses of the object they refer to...right.

But to me it seem the same???I mean they both point to the content of the variable.

Therefore there is no difference to what they store.

Am I right?It seems I am missing something??

Confusion is what I am experiencing.☺

Thank you for reading.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are mistaken. The two are very different.
If you have a String like "Raghav" the variable pointing to it does not store "Raghav" but abcd1234 or some other location where that String object can be found. In C they would call that a pointer.
Or more likely where the memory address for that String can be found. In C they call that a handle, which is a pointer to a pointer. There is no straightforward way to find out the memory location in Java®.

If you have a primitive, for example an int containing the value 12345, the memory actually contains 12345.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You open a drawer and find an apple versus you open the drawer and find a note that tells you that the apple is over on the table.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, you are mistaken. The two are very different.
If you have a String like "Raghav" the variable pointing to it does not store "Raghav" but abcd1234 or some other location where that String object can be found. In C they would call that a pointer.
Or more likely where the memory address for that String can be found. In C they call that a handle, which is a pointer to a pointer. There is no straightforward way to find out the memory location in Java®.

If you have a primitive, for example an int containing the value 12345, the memory actually contains 12345.



OK.what now I understand... Is that the primitive contained the actual value and the object reference variable point to the address of the object which it in turn point at the class of the object.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You open a drawer and find an apple versus you open the drawer and find a note that tells you that the apple is over on the table.


Wow..It is clear we are not on the same frequency of understanding.That way beyond my meek intelligence.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to think about it like those old Rolodex cards people used to keep information on. Or even a stack of 3x5 notecards.

I can write a number on a card for my age - 46
I can also write an address on a card: 1600 Pensylvania Avenue NW, Washington D.C., 20500

One tells me exactly what I want to know - what my age is.

The other tells me WHERE something is - how to get there. If I then want to do something with that (like paint the Red Room), I'd have to go there first.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:

One tells me exactly what I want to know - what my age is.

The other tells me WHERE something is - how to get there. If I then want to do something with that (like paint the Red Room), I'd have to go there first.


Best Explanation.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You open a drawer and find an apple versus you open the drawer and find a note that tells you that the apple is over on the table.


or more likely knowing Bear: You open a drawer and find an apple versus you open the drawer and find a note that tells you that the apple pie is over on the table. :-)



-steve
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You open a drawer and find an apple versus you open the drawer and find a note that tells you that the apple is over on the table.


At first it was quite a criptic explanation for me.

After reading and understanding what Fred Rosenberger said.

Now I understand what you were saying.

Primitive Variables - You open a drawer and find an apple

Object Reference Variables - You open the drawer and find a note that tells you that the apple is over on the table.





 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This JavaRanch story explains it, I hope this will make it clear: Cup Size -- a story about variables
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:At first it was quite a criptic explanation for me.
After reading and understanding what Fred Rosenberger said.
Now I understand what you were saying.

Great. And just to take Fred's Rolodex anaolgy a bit further:

Suppose you had a set of Rolodex cards that had peoples' names and addresses on them. Chances are you'd probably keep them in name order.

Now let's assume that they refer to patients in a medical clinic, each of which has a file stored in a filing cabinet.
By simply adding a file number to your Rolodex, assuming that you keep files in numeric order, you now know exactly where to find that file.
Furthermore, with a bit of thinking about your file numbering (for example birthdate (YYMMDD) + a sequence number) you could store your files in a different - but also useful - order to your Rolodex cards.

Indeed, I live in Belgium, and national ID numbers are precisely that format: YYMMDD-#####.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!