This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Object and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Object and equals " Watch "Question on Object and equals " New topic

Question on Object and equals

Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Can someone explain the working of the code below

Output is c
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"if (obj3.equals(obj4)) //why is this false "

The statement evauates to false because Object.equals() (by default) returns the equality of the object references. Since obj3 and obj4 reference distinct objects in memory, the references themselves are not equal.

"if (obj1.equals(obj2)) //why is this true "

The Boolean.equals() method overrides the Object.equals(). According to the documentation:

public boolean equals(Object obj)
Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

Overrides: equals in class Object

Parameters: obj - the object to compare with.

Returns: true if the Boolean objects represent the same value; false otherwise.
I agree. Here's the link:
subject: Question on Object and equals