Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sun Apr 15, 2012 7:47 pm
by Matheus
This question says that the equals method checks if two object references are pointing to the same object. This is not true. This method is responsible for check the content of the variable, and the operator responsible for the previous check is the '==' operator.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sun Apr 15, 2012 10:01 pm
by admin
The given explanation says, "Object class's equals() method just checks whether the two references are pointing to the same location or not. ", and it is correct.
It is not talking about equals methods of all classes but specifically about equals method of Object class.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Mon Apr 16, 2012 7:52 am
by Matheus
You're right, I misunderstood this one. Thanks
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Wed Sep 30, 2015 3:42 pm
by troydh53
Hmmm, so does String's .equals() method compare contents or location? I also thought all .equals() methods always compared contents and == operator always compared location.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Wed Sep 30, 2015 11:42 pm
by admin
troydh53 wrote:Hmmm, so does String's .equals() method compare contents or location?
It compares the contents.
I also thought all .equals() methods always compared contents and == operator always compared location.
No, that is not correct. equals is a regular method that can be overridden by any sub class (unless the class is final e.g. String) and the sub class can implement it however it wants to.
Also, a class hierarchy that does not override equals method anywhere in the chain gets the version provided by the root of all classes i.e. the Object class, which compares location.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sat Aug 13, 2016 12:07 pm
by xc2016
I did wrong, and I chose It will compile but throw an exception at run time. I know obj1 and obj2 point to the same object. Are they all pointing to null? when use a reference pointing to null to call a method, will it throw NullPointerException? Am I missing something here? please advise. Thank you!
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sat Aug 13, 2016 12:33 pm
by admin
The question clearly says they are pointing to the same object. Null is not an object.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sat Aug 13, 2016 1:26 pm
by xc2016
I am not sure if I understand correctly. When we use new keyword to create a new object, it will automatically have a default value, such as String object has null default value. I thought the newly created Object obj1 has a default value null.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sat Aug 13, 2016 9:11 pm
by admin
Your understanding is incorrect. You seem to be confused between a reference variable and an object.
String str; <- str is not an object. It is a reference variable. It is null right now.
str = new String(); <- str is now pointing to a String object.
I would suggest you to go through a book before attempting these mock exams otherwise you will not get full benefit.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sun Apr 29, 2018 7:39 am
by Lunarkiran
Code: Select all
Object a=new Object();
a="a";
Object b=new Object();
b="a";
System.out.println(a==b);
why this prints true.. are they not referring to dfferent locations?
Re: About Question enthuware.ocajp.i.v7.2.1014 :
Posted: Sun Apr 29, 2018 8:46 am
by admin
Lunarkiran wrote:Code: Select all
Object a=new Object();
a="a";
Object b=new Object();
b="a";
System.out.println(a==b);
why this prints true.. are they not referring to dfferent locations?
Why do you think they are referring to different locations?
Your question relates to String interning. You might want to read the chapter on Strings from a book again.