Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > does String.equals breaks the equals contract?

Reply
Thread Tools

does String.equals breaks the equals contract?

 
 
Edward A Thompson
Guest
Posts: n/a
 
      02-11-2004
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?
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      02-11-2004
Edward A Thompson wrote:
> 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
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      02-11-2004
Edward A Thompson wrote:

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

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
gmcdanie
Guest
Posts: n/a
 
      02-11-2004
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.


"Edward A Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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?



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      02-11-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does equals method work on a String Buffer object! javabeginner29 Java 0 04-02-2012 10:14 AM
different asp.net server tags (less than Percentage symbol hash or equals) fake ID ASP .Net 1 09-29-2009 08:13 PM
Why Does <equals()> Method on Arrays Always Return False? kvnsmnsn@hotmail.com Java 5 04-26-2006 09:54 PM
different asp.net server tags (less than Percentage symbol hash or equals) fake ID ASP .Net 0 02-10-2006 03:02 AM
Difference? myDate.Equals(Date.MinValue) -or- myDate = Date.MinValue Andreas Klemt ASP .Net 3 08-10-2003 03:43 AM



Advertisments