Comparing java strings with ==

Possible duplicate:
Java String.equals vs ==

Is it possible to compare Java strings using the == operator?

Why do I often see that the equals () method is used instead?

Is this because comparing with literal strings (like "Hello") using == does not imply invoking equals ()?

-3
source share
11 answers

there is no user operator overload in java. [so you cannot overload it to call equals ()]

equals () ensures that you check if 2 objects are identical, and == checks if it is the same object. [so no, using == does not call equals ()].

+5
source

== checks whether both objects belong to the same instance of the object, while equals() checks whether these two objects are really equivalent, even if they are not the same instance.

+2
source

No, this is not possible, because with == you are comparing object references, not the contents of the string (for which you need to use equals).

+2
source

In Java, you cannot overload operators. The == operator performs identity equality. Alternatively, the equals(...) method can be overridden to compare types.

Here is a snippet of code to demonstrate:

 String a = "abcdef"; String b = a; String c = new String(a); println(a == b); // true println(a.equals(b)); // true println(a == c); // false println(a.equals(c)); // true 

One complication with equals(...) you also need to take care of a null value. So the correct zero idiom is:

 (a == null ? b == null : a.equals(b)) 

This is a loop you shouldn't jump into, e.g. C #

+2
source

To extend the answer to @amit, the == operator should only be used for value types (int, double, etc.). String is a reference type and should therefore be compared with the .equals () method. Using the == operator in a reference type checks for reference equality in java (which means that both object references point to the same memory location.)

+1
source

String is a class. Therefore, if you try to compare String with an object containing a string value, you cannot use == because it is looking for an object. To compare the contents of the object you should use is

+1
source

The == operator compares references to string objects, while the String.equals method checks both references to objects and object values. In addition, the String.equals inturn method uses the == operator inside its implementation.

+1
source

From what I know, the operator '==' is used to check whether objects are identical or not.
The supposed compared strings can have the same value (nr characters, etc.), but actually be two completely different objects, which makes the comparison false.

+1
source

== returns true if the memory address is equal on both sides, with the exception of primitive types.

equals should be used in everything that is not primitive. classes for the main part.

+1
source

As already mentioned, == checks if the same object is, while equals() checks the same content (normal, the base implementation is ==, but String overrides this).

Note:

 "Hello" == "Hello" //most probably would be true "Hello".equals( "Hello" ) //will be true String s1, s2; //initialize with something different than a literal, eg loading from a file, both should contain the same string s1 == s2 //most probably will NOT be true s1.equals( s2) //will be true, if both contain the same string, eg "Hello" 

In addition, the same is true for wrappers of objects of primitives, for example

  Long l1 = 1L; Long l2 = 1L; l1 == l2 //will most likely be true for small numbers, since those literals map to cached instances l1.equals(l2) //will be true new Long(1) == new Long(1) //will NOT be true new Long(1).equals(new Long(1)) //will be true 
0
source
Operator

== checks the structure of bits of objects, not the contents of these objects, but is equal to the function of comparing the contents of objects.

 String str1=new String("abc"); String str2=new String("abc"); 

System.out.println (str1 == str2); will return false because str1 and str2 are another object created using "new". System.out.println (str1.equals (str2)) will return true because equals () checks the contents of the object.

0
source

All Articles