Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts

    when use .equals() and when ==

    Hi,

    I am knee-deep in Java at the moment, and want to know if it is better to use:
    if ( somestring.equals( someotherstring ))
    or
    if ( somestring == someotherstring )

    what is the difference and why is one better than the other
    I am the luckiest man in the world

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    they are the same just a small difference in execution time, and you probably wouldn't even notice it. The '==' might be faster for doing those simple operations in an 'if' statement since the .equals() will require looking into a library to find what that operator means. But again, they are the same.


    Jason

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    There is also another difference. If I remember correctly, == only references the location of an item stored in the memory and not always the actual value. .equals() is used with Strings so that you can actually check if the values are the same. This is useful for when you get input from the user and you want to compare it to a variable. I hope that helps a little bit.

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Antonio is right. They are definitely not the same thing. Using the equality operator (==) will only return true if the references are pointing at the same instance (ie, the same place in memory). It's like comparing pointers instead of using strcmp() in C. You should always use .equals() when comparing strings (unless you're doing something really strange).
    Code:
    String s1 = "hello";
    String s2 = "hello";
    String s3 = s1;
    String s4 = s2;
    
    if (s1 == s2) { // FALSE!!! s1 and s2 are different instances
      // this will never be executed
    }
    if (s1.equals(s2)) { // TRUE s1 and s2 contain the same string
      // this will always be executed
    }
    if (s3 == s1) { // TRUE s1 and s3 reference the same instance
      // this will always be executed
    }
    if (s3 == s2) { // FALSE!!! s3 and s2 are different instances
      // this will never be executed
    }
    if (s3.equals(s2)) { // TRUE s3 and s2 contain the same string
      // this will always be executed
    }
    if (s2 == s4) { // TRUE s2 and s4 reference the same instance
      // this will always be executed
    }
    shmoove

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    i am doing some inputvalidating with the == operator, didn't notice strage behaviour until now, but will change the code to use .equals() from now on. Better to be safe then ....

    Thanks for the reply's
    I am the luckiest man in the world

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    did the quick test today:
    Code:
           String s1 = "burubudur";
            String s2 = "burubudur";
            String s3 = s1;
            String s4 = s2;
            
            System.out.println(s1 == s1);
            System.out.println(s1 == s2);
            System.out.println(s1 == s3);
            System.out.println(s1 == s4);
    all returned true
    I am the luckiest man in the world

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Really, what JDK are you using (type java -version on the command line to find out)?

    shmoove

  • #8
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's because you dont use 'new String()'. If you dont and there is already a String with the same text it will just make a reference to that one.

    try this:

    Code:
    class test
    {
    	public static void main(String[] a)
    	{
    	       String s1 = new String("burubudur");
                        String s2 = new String("burubudur");
    	       String s3 = s1;
                        String s4 = s2;
    
    	        System.out.println(s1 == s1);
    	        System.out.println(s1 == s2);
    	        System.out.println(s1 == s3);
                        System.out.println(s1 == s4);
    	}
    }
    <JPM />

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You've probably figured it out by now, but here is a good reference that I googled up.
    [http://lists.bluej.org/pipermail/blu...st/000669.html]

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Antonio, your link was very useful, especially because the code i make has to work on an AIX machine, so in the future i definately will use the .equals() method to check if two string-objects contain the same text
    I am the luckiest man in the world

  • #11
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In java, the boolean operators are otherwise known as arithmetic operators due to the fact that you can't compare strings, only numeric values. The "arithmetic operators" will still yield a true of false value though.


  •  

    Posting Permissions

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