Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects vs references in java  RSS feed

 
Hasanli Gulshan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.I am new here.If I do wrong something,sorry.I am preparing for OCA exam.I can't understand this sentences in Boyarsky's book
"All references are the same size,no matter what their size"
Why no matter???Why same size??
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasanli Gulshan wrote:hi.I am new here.If I do wrong something,sorry.I am preparing for OCA exam.I can't understand this sentences in Boyarsky's book
"All references are the same size,no matter what their size"
Why no matter???Why same size??

A reference is like a pointer, so what she's saying is that the size of the pointer never changes.

Nothing to do with what it points TO - which could definitely change (if you allow it).

Winston
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It means that a reference is an area in memory which contains instructions for finding an object. That will probably be a memory location which is probably 4 bytes on a 32 bit machine. What resides in that memory location can be any size.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that the book probably says:
"All references are the same size, no matter what their type"

As an analogy think of a street with different types of houses (they are our objects).
Let's say there is a small house, one bedroom, at 350 Main Street.
And let's say there is a big house, six bedrooms, at 780 Main Street.
Even though the houses themselves are different types/sizes their addresses (the way we reference them) will be the same size: "NNN Main Street", 15 characters long in this case.

(References in java are not character strings of course, this is just an analogy)

 
Hasanli Gulshan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:I suspect that the book probably says:
"All references are the same size, no matter what their type"

As an analogy think of a street with different types of houses (they are our objects).
Let's say there is a small house, one bedroom, at 350 Main Street.
And let's say there is a big house, six bedrooms, at 780 Main Street.
Even though the houses themselves are different types/sizes their addresses (the way we reference them) will be the same size: "NNN Main Street", 15 characters long in this case.

(References in java are not character strings of course, this is just an analogy)

Yes,I am sorry,there had to be "their type".I like this kind of examples very much.But I didn't understand fully.May be i have problem about reference.What is difference between reference and reference variable?I couldn't understand that why you say street is reference??are all references different?if this is stupid question,I am sorry
 
Hasanli Gulshan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

It means that a reference is an area in memory which contains instructions for finding an object. That will probably be a memory location which is probably 4 bytes on a 32 bit machine. What resides in that memory location can be any size.

Thanks then how many location are there?I know reference like this-->> MyClass myObject=new MyClass();
myObject is reference as for me.May be I dont know difference between reference and reference variable
 
Hasanli Gulshan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Hasanli Gulshan wrote:hi.I am new here.If I do wrong something,sorry.I am preparing for OCA exam.I can't understand this sentences in Boyarsky's book
"All references are the same size,no matter what their size"
Why no matter???Why same size??

A reference is like a pointer, so what she's saying is that the size of the pointer never changes.

Nothing to do with what it points TO - which could definitely change (if you allow it).

Winston
Sorry,I didn't understand what you mean absolutely
 
Paul Clapham
Sheriff
Posts: 22485
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasanli Gulshan wrote:I know reference like this-->> MyClass myObject=new MyClass();
myObject is reference as for me.May be I dont know difference between reference and reference variable


You have the right idea. In your example, myObject is a reference variable and its value is a reference to an object of type MyClass.

Compare this:



Here shoeSize is an int variable and its value is the integer 42.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hasanli Gulshan wrote:What is difference between reference and reference variable?

A reference is like a pointer to an object, or a magic number that uniquely identifies an object. We need a reference so we can tell java which object we want to do some action upon. A reference variable is just a variable we store the reference in.

Hasanli Gulshan wrote:I couldn't understand that why you say street is reference?

In my analogy I can reference each house using its street address. So if I say there is going to be a party at 350 Main Street you know exactly which house (object) I am talking about.

Hasanli Gulshan wrote:are all references different?

Yes, every object has a unique reference to identify it. (You will later discover that the same reference can be stored in more than one reference variable, but I wouldn't worry about that at this stage.)
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:. . .
In my analogy I can reference each house using its street address. So if I say there is going to be a party at 350 Main Street you know exactly which house (object) I am talking about.
. . .
An even better analogy is a postcode. The postcodes have (approximately) the same size e.g. M60 2AW but they all point to locations (in UK to streets buildings or parts of streets). The contents of those locations can vary, but the size of the postcode (reference) are the same. You can put the postcode into a sat‑nav an it will unerringly take you thither. The example I gave if it works at all will get you somewhere in central Manchester.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!