This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Object Reference Variable vs Primitive variables

 
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.
 
Marshal
Posts: 67464
257
  • 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.
 
Marshal
Posts: 67341
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
lowercase baba
Posts: 12792
51
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.
 
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.





 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
What are you doing in my house? Get 'em tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!