Page 1 of 1

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

Posted: Wed May 28, 2014 10:26 am
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.

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

Posted: Wed May 28, 2014 8:32 pm
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

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

Posted: Thu May 29, 2014 9:51 am
by seanboyblue
Excellent thanks for the feedback :D :D

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

Posted: Tue Nov 10, 2015 4:33 pm
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.

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

Posted: Tue Nov 10, 2015 6:54 pm
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.

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

Posted: Tue Nov 10, 2015 8:54 pm
by RAZER-KIEV
Oh, of course! sorry, my question wasn't very smart)

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

Posted: Mon Sep 19, 2016 8:57 am
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.

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

Posted: Mon Sep 19, 2016 10:27 am
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.

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

Posted: Wed Feb 08, 2017 12:23 pm
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?

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

Posted: Wed Feb 08, 2017 8:06 pm
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:

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

Posted: Sat Oct 28, 2017 2:40 pm
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.

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

Posted: Sun Oct 29, 2017 12:42 am
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.

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

Posted: Fri Dec 22, 2017 11:53 pm
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.

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

Posted: Sat Dec 23, 2017 12:54 am
by admin
It is not clear what you are asking. Can you be a bit more clear?

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

Posted: Sun Dec 31, 2017 12:04 am
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."

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

Posted: Sun Dec 31, 2017 12:06 am
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."

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

Posted: Sun Dec 31, 2017 12:15 am
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.