Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding question from K&B mock test  RSS feed

 
Aaroosh Pandoh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




can anyone explain this code.
specially Punt[] go () method
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaroosh Pandoh wrote:specially Punt[] go () method

The go() method creates a Punt array which contains only one element, the parameter p.
 
Aaroosh Pandoh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36406
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punt doesn't define equals() so it uses the behavior of ==. Are they the same object reference?
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant the reference variable p and p2, will be pointing to the same object Punt, and p2[0] will also point to Punt
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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]));"
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelvin Okornoe
Ranch Hand
Posts: 30
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Marshal
Posts: 10995
161
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!