does String.equals breaks the equals contract?

Discussion in 'Java' started by Edward A Thompson, Feb 11, 2004.

  1. 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?
    Edward A Thompson, Feb 11, 2004
    #1
    1. Advertising

  2. Edward A Thompson

    Chris Smith Guest

    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
    Chris Smith, Feb 11, 2004
    #2
    1. Advertising

  3. 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
    Christophe Vanfleteren, Feb 11, 2004
    #3
  4. Edward A Thompson

    gmcdanie Guest

    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" <> wrote in message
    news:...
    > 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?
    gmcdanie, Feb 11, 2004
    #4
  5. Edward A Thompson

    Tony Morris Guest

    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
    Tony Morris, Feb 11, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ant...
    Replies:
    6
    Views:
    378
    Ant...
    Oct 28, 2003
  2. stixwix

    equals method contract in JLS

    stixwix, Dec 12, 2005, in forum: Java
    Replies:
    2
    Views:
    1,001
    Chris Smith
    Dec 12, 2005
  3. Specialist Verilog Engineers Roles
    Replies:
    0
    Views:
    461
    Specialist Verilog Engineers Roles
    Jun 27, 2007
  4. Smithers
    Replies:
    12
    Views:
    1,171
    Ben Voigt [C++ MVP]
    Jul 7, 2009
  5. javabeginner29
    Replies:
    0
    Views:
    397
    javabeginner29
    Apr 2, 2012
Loading...

Share This Page