does String.equals breaks the equals contract?

E

Edward A Thompson

The following code compiled fine - should it have?

public static void main(String[] args)
{
String a = "Edward";
String[] b = {"Edward"};
if(a.equals(b)) System.out.println(a + " equals " + b);
else System.out.println(a + " does not equal " + b);
}

The equals method in String calss looks for the object to be an
instance of String, which I guess String[] is . Then it does a
character by character compare of the characters in the string. It
fails (expected) but should a.equals(b)) even compile?
 
C

Chris Smith

Edward said:
The following code compiled fine - should it have?
Yes.

public static void main(String[] args)
{
String a = "Edward";
String[] b = {"Edward"};
if(a.equals(b)) System.out.println(a + " equals " + b);
else System.out.println(a + " does not equal " + b);
}

The equals method in String calss looks for the object to be an
instance of String, which I guess String[] is .

Nope, String[] is definitely *not* a subtype of String. In fact, it's
specifically because that test fails that equals is returning false.
Note that the description of equals does not specify that it throws an
exception if the other object is of the wrong type. In fact, it would
be difficult to define "wrong type" here.
Then it does a
character by character compare of the characters in the string.

No, it never does this, because the second object isn't even a String.
It couldn't possibly do this.

Regardless, none of this relates at all to whether the code compiles.
The signature for the equals method is:

boolean equals(Object)

Since the method call is given an Object (yes, String[] is a subtype of
Object), and the result is used as a boolean, the code will compile.
The compiler doesn't concern itself with anything except the method
invocation matching the signature.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Christophe Vanfleteren

Edward said:
The following code compiled fine - should it have?

public static void main(String[] args)
{
String a = "Edward";
String[] b = {"Edward"};
if(a.equals(b)) System.out.println(a + " equals " + b);
else System.out.println(a + " does not equal " + b);
}

The equals method in String calss looks for the object to be an
instance of String, which I guess String[] is .

No, String[] is not a String.
Then it does a
character by character compare of the characters in the string. It
fails (expected) but should a.equals(b)) even compile?

Of course it should compile:

equals is defined to accept an Object as argument, which String[] definitely
is.
 
G

gmcdanie

the method in the String class is set up to take any object not just another
string.
Since the Array is inherited from Object it will compile.
 
T

Tony Morris

the object to be an
instance of String, which I guess String[] is .

You guessed wrong !
:)


Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top