Comparing Java with == two strings is false?

The string parts are the string [6]:

 ["231", "CA-California", "Sacramento-155328", "aleee", "Customer Service Clerk", "Alegra Keith.doc.txt"]

But when I compare parts[0] with "231" :

 "231" == parts[0] 

the result is incorrect

I'm confused, so can someone tell me why?

+41
java string equals
Jun 15 '09 at 12:47
source share
12 answers

The == operator compares object references, not the String s value.

To compare String s values, use the String.equals method:

 "231".equals(parts[0]); 

This is true for any other object in Java - when comparing values, always use the equals method instead of using the == operator.

The equals method is part of Object and must be overridden by classes that will be compared one way or another.

+73
Jun 15 '09 at 12:50
source share

If the strings are not interned, then == checks the reference identifier. Using:

  "231".equals(parts[0]); 

instead.

+14
Jun 15 '09 at 12:50
source share

== in Java compares the address of objects (strings in this case).

You want parts[0].equals("231")

+13
Jun 15 '09 at 12:49
source share

The following prints out "true",

 String s = "231"; if(s == "231") { System.out.println("true"); } else { System.out.println("false"); } 

This is because the lines are not changed, and java will try to save as much space as possible, therefore it points to the same memory link.

However, the following produces false:

 String s = new String("231"); if(s == "231") { System.out.println("true"); } else { System.out.println("false"); } 

new will force it to save the string in a new memory location.

By the way, you should ALWAYS use .equals() to compare strings (for cases like this)

+8
Jun 15 '09 at 17:57
source share

Use the equals: parts [0] .equals ("231") method. == operator compares object references.

+7
Jun 15 '09 at 12:50
source share

"==" compares object references, in your case "231" is a different object than the parts [0].

You want to use String.equals .

 parts[0].equals("231") 
+5
Jun 15 '09 at 12:50
source share

The answer is very simple: when comparing strings using the == operator, you actually compare if two different variables refer to the same String object. And they don’t do it, the string in the array and the newly created β€œ231” are different String objects with the same contents.

The correct thing is to use the following expression: "231".equals(parts[0]) or "231".equalsIgnoreCase(parts[0]) . This will give you what you need and return true if these String objects contain the same values.

+4
Jun 15 '09 at 12:55
source share

I thought it would be useful to express the answer in a test example:

 public class String231Test extends TestCase { private String a; private String b; protected void setUp() throws Exception { a = "231"; StringBuffer sb = new StringBuffer(); sb.append("231"); b = sb.toString(); } public void testEquals() throws Exception { assertTrue(a.equals(b)); } public void testIdentity() throws Exception { assertFalse(a == b); } } 
+2
Jun 15 '09 at 17:53
source share

You can also use the compareTo (String) method :

 String str = "test"; if( str.compareTo("test") == 0) //the argument string is equal to str; else //the argument string is not equal to str; 
+2
Jan 09 '10 at 21:28
source share

Use the equals method to compare objects:

 String[] test = {"231", "CA-California", "Sacramento-155328", "aleee", "Customer Service Clerk", "Alegra Keith.doc.txt"}; System.out.println("231".equals(test[0])); 

Comparison '==' compares links, not values.

+1
Jun 15 '09 at 12:57
source share

Here is a really good example. The '==' statement with a string can be really complicated in Java.

 class Foo { public static void main(String[] args) { String a = "hello"; String b = "hello"; String c = "h"; c = c + "ello"; String operator = null; if(a == b) { operator = " == "; } else { operator = " != "; } System.out.println(a + operator + b); if(a == c) { operator = " == "; } else { operator = " != "; } System.out.println(a + operator + c); if(a == "hello") { operator = " == "; } else { operator = " != "; } System.out.println(a + operator + "hello"); if(c == "hello") { operator = " == "; } else { operator = " != "; } System.out.println(c + operator + "hello"); } } 

Which will produce the following output:

 hello == hello hello != hello hello == hello hello != hello 
+1
Jan 09 '10 at 21:19
source share

As many others have already explained, you are trying to compare with the equality operator, but then it will rely on Object.equals () instead of String.equals ().

So you can do the job by explicitly calling String.equals (), but instead of writing

 parts[0].equals("blahblah") 

I would prefer:

 "blahblah".equals(parts[0]) 

Since it avoids testing the potential invalidity of parts [0] (but be careful that the part variable itself can be null ...)

Another way is to use String.intern ():

 if (parts[0].intern() == "blahblah") ... 

See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern () for more details.

0
Jun 15 '09 at 19:03
source share



All Articles