• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Question from K&B

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following statement from K&B -
What statements are true about properly overridden hashCode() and equals() methods?
For the above question, one of the following statement is given as the correct answer.
equals() can be true even if it`s comparing different objects.

Can anyone explain how can equals() return true if its comparing different objects??
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test{
public static void main(String r[]){
String x = new String("ABC");
String y = new String("ABC");
if(x.equals(y)){
System.out.println("true");
}
}//end of main
}//end of Test

The result is true. Note that we are comparing two different objects even though they are both of type String containing the value "ABC";

Fes
 
author
Posts: 9000
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys -

I hope that somewhere in there we say something like: "equals() should be overridden to create your own definition of what it means for two objects of your class to be 'meaningfully equivalent'. In other words, == is about comparing bits, the equals method is about comparing concepts.

HTH,

Bert
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope the following example explains it clearly. The statement might have been made to show the difference between == and equals(). Here == evaluates to true only when both are same objects, whereas equals() evaluate to true for different objects with same value. I am just restating the already made reply.

class Test {
public static void main(String args[]){
String s = "Hello";
String s2 = "Hello";
if (s==s2){
System.out.println("Equal without new operator");
}
if (s.equals(s2)) {
System.out.println("Equal without new (+ equals) operator");
}
String t = new String("Hello");
String u = new String("Hello");
if (t==u){
System.out.println("Equal with new operator");
}
if (t.equals(u)) {
System.out.println("Equal with new (+ equals) operator");
}
}
}

Thanks,
Gokul.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by shetal bansal:

equals() can be true even if it`s comparing different objects.

Can anyone explain how can equals() return true if its comparing different objects??



Heh. Look:


The main idea: equals can always return true. It's not good, but ... it can .
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mr.Bolyuba,
You may be correct.
But in that book the authors meant two different objects of the same class.
And please note the term "properly overridden".
As Mr. Bates said if equals() is meaningfully overridden it can return true even if the objects are different.
Remember for a properly overridden equals() method the instanceof check will be there.
reply
    Bookmark Topic Watch Topic
  • New Topic