Results 1 to 9 of 9
  1. #1
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default compare object value by equals()

    Java Code:
    import java.util.*;
    class Stringer{
    
    private String s;
    public Stringer(String s){this.s=s;}
    public static void main(String[] args){
    
    
    Stringer s1=new Stringer("abc");
    Stringer s2=new Stringer("abc");
    String s3="abc";
    String s4="abc";
    System.out.println("s1 equals s2 "+(s1.equals(s2)));   // Why the result is "false" but not "true"?
    System.out.println("s3==s4 "+(s3==s4));
    }
    
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: compare object value by equals()

    Because your Stringer class has not overridden the equals method. So it defaults to the one declared in the Object class and only
    compares the references. Read the Object class documentation in the Java API and check out the tutorials in my signature for more information.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default Re: compare object value by equals()

    Quote Originally Posted by jim829 View Post
    Because your Stringer class has not overridden the equals method. So it defaults to the one declared in the Object class and only
    compares the references. Read the Object class documentation in the Java API and check out the tutorials in my signature for more information.

    Regards,
    Jim


    Thanks Jim so much.
    another question below seeking for your advise pls.

    Java Code:
    String s3="abc";
    String s4="abc";
    System.out.println("s3==s4: "+(s3==s4));      //can I compare if s3 and s4 referring to the same object?  or there are no such concept allowed?
    System.out.println("s3 equals s4: "+(s3.equals(s4)));

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: compare object value by equals()

    Absolutely! If you use == then you will be checking for the same object. In fact, a well written equals() method will check for the same references first and then check object state if the objects references are different.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default Re: compare object value by equals()

    Thanks for your prompt reply, Jim.

  6. #6
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default Re: compare object value by equals()

    sorry, i have a question suddenly...

    Java Code:
    String s3="abc";
    String s4="abc";
    System.out.println("s1 equals s2 "+(s1.equals(s2)));   
    System.out.println("s3==s4 "+(s3==s4));                   // Why the result is "true" but not "false"?

  7. #7
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default Re: compare object value by equals()

    I got the answer here Java String Comparison | Wideskills

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: compare object value by equals()

    They sort of explained the answer but they also skirted around it. When you use a String literal, that literal is interned in a cache of String objects. You can also add a String object to this pool by invoking the String.intern() method. Since Strings are immutable, there is no danger of internally changing the value of a cached string. And it is inadvisable to do the following:
    Java Code:
    String str = new String("abc");
    Assuming first use of "abc", this creates two strings. First it creates and interns "abc", then it creates a new and different object (but same String value). This is rather inefficient and unnecessary. In fact, since Strings are immutable and cached, one rarely needs to use the new operator for String creation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    35
    Rep Power
    0

    Default Re: compare object value by equals()

    You are awesome!!!
    Thanks Jim.

Similar Threads

  1. Trouble with Object.equals()
    By summersab in forum New To Java
    Replies: 3
    Last Post: 06-14-2011, 01:07 AM
  2. equals method to compare two objects?
    By Yuvarajsinh in forum New To Java
    Replies: 5
    Last Post: 12-29-2010, 10:24 AM
  3. equals(object obj)
    By hamed in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 07:08 AM
  4. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •