• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

page 38 OCA 8 Guide - object references and objects

 
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On page 38 of the OCA 8 Guide I considered two possible schemes (one was the correct) because the entire time I was not sure about the following:
We are saying that when the reference one now points to object "b".
My Question: But what forbids me to think in reverse and instead say reference two now points to object "a"? I assume there is a simple rule I'm just ignorant about.

Aside those schemes I wondered about other interpretations, one that i could scrap off by compiling the code and another I'm not sure what to think about
- a possibility that java with read one = two as assigning two objects to one another (and p36 explains an object cannot be assigned to another object, so in this case I'd expect no compile - but it did, so I could scrap this possibility off)
- a possibility that java would read one = two as a reference being assigned to another reference (however, that would create a conlifct as a given reference would be pointing to different objects at the same time)


Thanks
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain how either of those references could point to object a. I think that will help answer your question.
 
Pedro Esgueira
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The code of the exercise:


Please explain how either of those references could point to object a. I think that will help answer your question.


What do you mean "how they could point to object a"?
reference one is pointing to object "a" on line 4
referencce two is pointing to object "b" on line 5
but then on line 6,

the variable one changes to point to object "b"

(authors).
And here is my doubt: when do I interpret a reference - as a reference, or as the underlying object? In this case we are interpretingone as a reference, and two as the underlying object "b".
What forbids us to interpretone as the underlying object "a" and two as a reference? (which would make then two point to object "a", to answer your question)
(or what forbids us to interpreting both one and two as references? or both as underlying objects?)












 
Rancher
Posts: 4117
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're both references.

one points to a String object with a value of "a".
two points to a String object with a value of "b".

When you do:
one = two;
you are setting the reference of one equal to that of two.
In other words, you are setting one to point to the same obect as two.
 
Pedro Esgueira
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

Thanks for your feedback.

I understand that part. That is what the authors say, and it is one of the possibilities I considered. But it still doen's answer my question.
In this case we are looking at one = two; and saying "reference one is now pointing at object "b" ". Implicitly, you are verbalizing one as a reference, and two as an underlying object.
Why can't I then consider the following readings?
ex1) "object "a" is now being pointed by reference two"
ex2) "reference one is now being reassigned to two" (like you said, they are both references; and references can be reassigned to other references)
ex3) "object "a" is now assigned to object "b"

I have the explanation for ex3): since Java doesn't allow an object to be assigned to another object, Java will automatically not consider that interpretation.
I have an idea for ex2): maybe Java knows that both references are taken, and maybe it has a default interpretation the programer's will that he/she is meaning to reassign not one reference to another reference, but simply redirect one of those references to the other object.
I simply don't know the explanation for ex1) How does Java behavior decide between reassigning one to object "b", instead of reassing object "a" to reference two ?
 
Saloon Keeper
Posts: 1217
85
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here one and two are reference variables of type String. These variables stores the reference ( address of an object in memory where it's located or stored after creation).

In our case on line 4 an object of type String having value "a" is created, assume whose reference value is 1020 in memory which gets stored in reference variable one. Now one = 1020.

Same way on line 5 another String object having value "b" is created, assume it's address is 1150 in memory that reference value is stored in reference variable two.

Now we have one = 1020 and two = 1150. Therefore ( before line 6 ) we say reference variable one points or refers to an object of String type having value "a" and reference variable two points or refers to an object of String type having value "b".

On line 6 one = two; here = is an assignment operator syntactically right-associative which assign right side value to left side variable, it's like one = 1150 so now reference variable one as well as two having reference value 1150 i.e. one = 1150 and two = 1150 which is the address of String object having value "b" hence both reference variable one and two points or refers to String object having value "b" (before line 8).

See if that helps.
 
Pedro Esgueira
Greenhorn
Posts: 19
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ganesh, that was it!

Actually, I was coming to update my post, because I'm now on page 60 of the OCA 8 Guide book, and the authors say "An assignment operator is a binary operator that modifies, or assigns, the variable on the left-hand side of the operator, with the result of the value on the right-hand side of the equation."

And then I get it confirmed with your feedback

On line 6 one = two; here = is an assignment operator syntactically right-associative which assign right side value to left side variable



So this is basically how Java "decides" in my ex1). It's just the nature of the assignment operator. Java reads one as a reference variable, and two as the underlying value - the object "b".

Thank you so much for your help!
 
Ganesh Patekar
Saloon Keeper
Posts: 1217
85
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pedro Esgueira wrote:. . .
reference one is pointing to object "a" on line 4
referencce two is pointing to object "b" on line 5. . .

But that is all new information which wasn't available in your original question. You only supplied one line, which line made it impossible for one to equal a.

And here is my doubt: when do I interpret a reference - as a reference, or as the underlying object?

reference is a reference It is neer an object.

In this case we are interpretingone as a reference, and two as the underlying object "b".

No, you are not. That is like interpreting my address, 123 Campbell Street, as my house. The address tells you where the house is, but doesn't tell you anything about the house there.

What forbids us to interpretone as the underlying object "a" and two as a reference? . . .

The structure of the language. If you know my address, you don't know that my door is green. It is possible to repaint the door blue, but that takes time. It is very quick to reassign something to a different reference. All you have to do is write
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!