About Question enthuware.ocajp.i.v7.2.1088 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
seanboyblue
Posts: 2
Joined: Wed May 28, 2014 10:18 am
Contact:

About Question enthuware.ocajp.i.v7.2.1088 :

Post by seanboyblue »

Can you explain why the following cannot be true:
o1 == o2 will always be false.
when we have the following :

Code: Select all

String blue = "blue";
String red = "blue";
System.out.println(blue.equals(red));
System.out.println(blue == red);
It prints :
true
true

The explanation seems to contradict this?
The == operator compares whether the two references are pointing to the same object or not. Here, they are not, so it returns false.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

Your example doesn't print false because Strings are interned in java. Blue and red are pointing to the same String object not two different objects as required by the problem statement. You might want to read more about this from a book. There are several articles online as well such as this.
Hth,
Paul
If you like our products and services, please help us by posting your review here.

seanboyblue
Posts: 2
Joined: Wed May 28, 2014 10:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by seanboyblue »

Excellent thanks for the feedback :D :D

RAZER-KIEV
Posts: 17
Joined: Thu Oct 01, 2015 4:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by RAZER-KIEV »

Hello! What does it means "hashCode() can be overridden and so the given statements is not true." ?
equals can be overridden too, isn't?
Tnx.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

Yes, equals can also be overridden. But this option is talking about hashCode, that is why the comment is talking about hashCode. Since a class can override the hashCode method and the overridden method can return same hash code value for two different objects, the given statement is not true.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

RAZER-KIEV
Posts: 17
Joined: Thu Oct 01, 2015 4:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by RAZER-KIEV »

Oh, of course! sorry, my question wasn't very smart)

newton
Posts: 1
Joined: Mon Sep 19, 2016 8:46 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by newton »

Hi, i was confused by this question. You can override hashCode() in this question, but you could also make o2 point to o1 (o2 = o1). Apparently you may override hashCode() in this question, but you may not make o2 object point to o1.

Can i expect these kinds of questions? And how will i know what i can and cannot do in a case like this one.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

newton wrote:Apparently you may override hashCode() in this question, but you may not make o2 object point to o1.
Well, the problem statement explicitly says, "o1 and o2 denote two object references to two different objects of the same class." So you cannot make o1 and o2 point to the same object as that is not what the question is asking.

On the other hand, the problem statement does not make any claim about the class code. That is why you have to consider all the possibilities for the class code.
Can i expect these kinds of questions? And how will i know what i can and cannot do in a case like this one.
Yes, it is valid question. Most of the time it is quite straight forward. You need to work with what ever is given in the problem statement.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by jamesmccreary »

For the record, I researched several articles and apparently two different objects can indeed have the same hashcode.

Hashcode is not the same thing as location in memory apparently. Hashcode seems to be a convenient way for the JVM to compartmentalize its objects for more optimized access.

Am I correct here Paul?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

jamesmccreary wrote: Hashcode is not the same thing as location in memory apparently. Hashcode seems to be a convenient way for the JVM to compartmentalize its objects for more optimized access.

Am I correct here Paul?
Correct :thumbup:
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by Sergey »

Hello. The part of explanation is not correct:
o1 and o2 denote two object references to two different objects of the same class.
o1.equals(o2) will always be false. It depends on how the equals method is overridden. If it is not overridden, then it will return false.

Code: Select all

String o1 = new String("123");
String o2 = new String("123");
System.out.println(o1.equals(o2)); // true

String o3 = new String("123");
String o4 = new String("1234");
System.out.println(o3.equals(o4)); // false
As you can see, if it is not overridden, it MAY return true.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

Explanation is correct. String class actually does override the equals method to compare the contents of the object instead comparing the reference. Object class's implementation of the equals method compares references and that is why it will always return false if the two references point to two different objects.
If you like our products and services, please help us by posting your review here.

tanushri
Posts: 5
Joined: Tue Dec 12, 2017 11:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by tanushri »

how about the two wrapper objects in the same class?

Double d = 10.0;
Integer i = 10;

in this case

Sysyem.print.ln(d == i) ; gives compile time error than false.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

It is not clear what you are asking. Can you be a bit more clear?
If you like our products and services, please help us by posting your review here.

tanushri
Posts: 5
Joined: Tue Dec 12, 2017 11:45 pm
Contact:

Re: About Question enthuware.".i.v7.2.1088 :

Post by tanushri »

what I meant is that for Wrapper class objects like
Integer i = 10 ;
Double d = 10.0;
"i== d" does not work, but gives Compile Time Error. In such case, how can the following option be correct?
"o1 == o2 will always be false."

tanushri
Posts: 5
Joined: Tue Dec 12, 2017 11:45 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by tanushri »

Lets be more particular
Integer o1 = 10 ;
Double o2 = 10.0;
"o1== o2" does not work, but gives Compile Time Error. In such case, how can the following option be correct?
"o1 == o2 will always be false."

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1088 :

Post by admin »

You need to read the question carefully. It clearly says o1 and o2 denote two object references to two different objects of the same class. In your example, that is not the case.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 86 guests