[Logo]
Forums Register Login
Coding question from K&B mock test




can anyone explain this code.
specially Punt[] go () method
 

Aaroosh Pandoh wrote:specially Punt[] go () method


The go() method creates a Punt array which contains only one element, the parameter p.
 

Roel De Nijs wrote:

Aaroosh Pandoh wrote:specially Punt[] go () method


The go() method creates a Punt array which contains only one element, the parameter p.



And why the output is
False false
Punt doesn't define equals() so it uses the behavior of ==. Are they the same object reference?
I want to understand the code very well....
Meaning the p reference to point to Punt object and p2 reference points to an array object of type Punt[].
can you explain your statement well please..   "Punt doesn't define equals() so it uses the behavior of ==. Are they the same object reference?"
 

Kelvin Okornoe wrote:Meaning the p reference to point to Punt object and p2 reference points to an array object of type Punt[].


The second part of your statement is incorrect! The reference variable p2 denotes (refers or points) to an array (object) of type Punt (not Punt[]). The reference variable p2[0] (which is used in the println statement) refers to the first element of the Punt array, which is (of course) also a Punt object (just like reference variable p).

In this topic you'll find a nice explanation with some code examples about the difference between == and .equals().

Hope it helps!
Kind regards,
Roel
 

Roel De Nijs wrote:

Kelvin Okornoe wrote:Meaning the p reference to point to Punt object and p2 reference points to an array object of type Punt[].


The second part of your statement is incorrect! The reference variable p2 denotes (refers or points) to an array (object) of type Punt (not Punt[]). The reference variable p2[0] (which is used in the println statement) refers to the first element of the Punt array, which is (of course) also a Punt object (just like reference variable p).

Hope it helps!
Kind regards,
Roel



Making both the references point to the same object..Is that right?
 

Kelvin Okornoe wrote:Making both the references point to the same object..Is that right?


That's a very ambiguous statement. you should explain your thoughts more clearly e.g. using the names of the reference variables you are refering (no pun intended ) to.
I meant the reference variable p and p2, will be pointing to the same object Punt, and p2[0] will also point to Punt
 

Kelvin Okornoe wrote:I meant the reference variable p and p2, will be pointing to the same object Punt


Definitely incorrect! As stated in one of my previous posts: reference variable p refers to a Punt object and reference variable p2 refers to an array (object) of type Punt. A Punt object is always a different object than an an array of type Punt, so p and p2 will never refer to the same object.

Kelvin Okornoe wrote:p2[0] will also point to Punt


The reference variable p2[0] definitely refers to a Punt object But is it the same Punt object reference variable p is refering to?
NO, the reference variable p2[0] and p do not point to the same object. p2[0] actually point to a new object Punt created by this code. Punt[] p2=(Punt[])o;
where, actually the p2[0] reference points to the element in the first location of the array.
Hope am right....
 

Kelvin Okornoe wrote:Hope am right....


You definitely are!

Do you now understand why false is printed twice if you run the code snippet from your first post?
 

Roel De Nijs wrote:Do you now understand why false is printed twice if you run the code snippet from your first post?



Yes please, I do have understand why false is printed twice.
The two references p and p2[0] do not point to the same object, so the equals method will print false and the == operator will print false because the values of the two references p and p2[0] are different.
 

Kelvin Okornoe wrote:the == operator will print false because the values of the two references p and p2[0] are different.


Spot-on!

Kelvin Okornoe wrote:The two references p and p2[0] do not point to the same object, so the equals method will print false


Based on the code snippet, the Punt class doesn't define an equals() method. So to which equals() method are you referring?

Roel De Nijs wrote:Based on the code snippet, the Punt class doesn't define an equals() method. So to which equals() method are you referring?



I meant the calling of the equals method in this code "System.out.println(p.equals(p2[0])+""+(p==p2[0]));"
 

Kelvin Okornoe wrote:I meant the calling of the equals method in this code "System.out.println(p.equals(p2[0])+""+(p==p2[0]));"


Yes, I know that. But my question is: how can you call the equals() method on a Punt reference variable, if the Punt class doesn't define an equals() method?

PS. I know the answer But it's a test to verify if you have a full understanding of the above code snippet.
 

Roel De Nijs wrote:Yes, I know that. But my question is: how can you call the equals() method on a Punt reference variable, if the Punt class doesn't define an equals() method? 



Am sorry Roel, I did not understand your question very well, But I think there is a equals method defined in the Object class, that is what gets called on the Punt reference.
Thank Roel you have been a great help to me.
Am grateful Roel.
 

Kelvin Okornoe wrote:But I think there is a equals method defined in the Object class, that is what gets called on the Punt reference.


You are spot-on! And do you know the implementation of the equals() method in the Object class?
Oh. Hi guys! Look at this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA


This thread has been viewed 989 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 21, 2018 20:53:52.