• Post Reply Bookmark Topic Watch Topic
  • New Topic

What happens when you set objRef2 = objRef1 ?  RSS feed

 
Ivan Brennan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a reference variable,
Car c1 = new Car();

when we create another variable and set it equal to the first,
Car c2 = c1;

we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car).
So if we have code like,

Car c1 = new Car();
Car[] cA = {c1, c1, c1, c1};

are we doing the same?
Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)?
I think so, but want to make sure I'm understanding this correctly.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.

A reference variable can only point to an object. It can't point to another variable. So it's important to keep in mind (as you have) the difference between variables and objects. When you see posts from beginners on this forum you will often see people referring to an object via the name of a variable which refers to it; that's okay if you understand the difference between variables and objects but just leads to confusion if you don't. Especially in those certification exam questions which delight in showing you bad code and asking you questions about it. But it looks like you have understood the difference correctly.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Brennan wrote:So if we have code like [...] Are we creating four *new* reference variables, each of which points at the same car?

Yup.

(again as opposed to pointing them at c1 itself)

Not quite sure what you're getting at here (but I suspect it's similar to my musings when I was a lad).

What do you think "pointing at c1" means?

In language terms, it means "find the object (or the "chunk of memory") I called 'c1'". And that's ALL it means.

Unfortunately, earlier languages like C called this a "pointer" (ie, a direct address to a piece of memory), so it was perfectly possible to say:
  c1 = 0;
Which means what? 'c1' now equals 0 (the first location in memory)?

Even the C compiler was smart enough to work out that that probably wasn't a good idea, so they gave it a new name: NULL. and that meant that 'c1' isn't actually pointing to anything.

Trouble is: C also allows you to do arithmetic on (and make expressions from) "pointers"; so ++c1, and (c1 + 1) are both perfectly valid. So now you can have a "pointer" that points to NULL+1. What the hell is that?

Java solved most of these problems - and believe me, they needed solving - by redefining the notion of "pointers" as references.
Yes, a reference "points to" an object (or it contains null); but you cannot change it; and any expressions involving a reference use the object it points to, not the pointer itself.

Thus, in Java, you use references. You NEVER access an object directly.

HIH. If there's anything you don't follow, or more you'd like to know, please let us know.

Winston
 
Ivan Brennan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification. I think I've got it now
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!