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

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

Moderator: admin

Post Reply
Matheus

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Matheus

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

Post by Matheus »

You're right, I misunderstood this one. Thanks

troydh53
Posts: 5
Joined: Fri Sep 11, 2015 8:06 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

xc2016
Posts: 4
Joined: Wed Aug 10, 2016 11:05 am
Contact:

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

Post 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!

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

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

Post by admin »

The question clearly says they are pointing to the same object. Null is not an object.
If you like our products and services, please help us by posting your review here.

xc2016
Posts: 4
Joined: Wed Aug 10, 2016 11:05 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Lunarkiran
Posts: 4
Joined: Mon Feb 26, 2018 3:41 am
Contact:

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

Post 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?

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

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

Post 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.
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 23 guests