String comparison with "==" is not reliable?

W

www

Hi,

I always thought I can use "==" to compare if two Strings are equal(with
same case too). I know there is a method .equals(Object o) or
equalsIgnoreCase(String s). But I always thought "==" is good enough.

This morning, I found out that I was wrong.

I have two Strings, str1 and str2. They received values from some where
else. When I print them out to screen, they look same.(I am aware of
some unvisible thing, so I used trim()

//str1 and str2
if(str1.trim() == str2.trim())
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}

Above code print out "Not equal". But the following code print out "equal".

if(str1.trim().equals(str2.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}

Can you give me some hint? Thank you.
 
M

maxnesler

Hi,

I always thought I can use "==" to compare if two Strings are equal(with
same case too). I know there is a method .equals(Object o) or
equalsIgnoreCase(String s). But I always thought "==" is good enough.

This morning, I found out that I was wrong.

I have two Strings, str1 and str2. They received values from some where
else. When I print them out to screen, they look same.(I am aware of
some unvisible thing, so I used trim()

//str1 and str2
if(str1.trim() == str2.trim())
{
System.out.println("equal");}

else
{
System.out.println("Not equal");

}

Above code print out "Not equal". But the following code print out "equal".

if(str1.trim().equals(str2.trim()))
{
System.out.println("equal");}

else
{
System.out.println("Not equal");

}

Can you give me some hint? Thank you.

"==" will test true if they are the same object.
 
W

www

I have just tested the following code:

String str1="Hello";
String str2="Hello";

if(str1 == str2)
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}

Guess what? It prints out "equal".
 
O

Owen Jacobson

I have just tested the following code:

String str1="Hello";
String str2="Hello";

if(str1 == str2)
{
System.out.println("equal");}

else
{
System.out.println("Not equal");

}

Guess what? It prints out "equal".

And if you took ten minutes to read the JLS, you'd understand why.

String constant expressions are interned as if by
String.intern(String), meaning that all identical string constants in
the source refer to the same object at runtime. Since == compares two
references and evaluates to true if they refer to the same object,
"foo" == "foo" will always evaluate to true.

The same can't be said if either or both strings did not come from
literals, since equal strings may be represented by non-identical
objects.
 
M

maxnesler

And if you took ten minutes to read the JLS, you'd understand why.

String constant expressions are interned as if by
String.intern(String), meaning that all identical string constants in
the source refer to the same object at runtime. Since == compares two
references and evaluates to true if they refer to the same object,
"foo" == "foo" will always evaluate to true.

The same can't be said if either or both strings did not come from
literals, since equal strings may be represented by non-identical
objects.- Hide quoted text -

- Show quoted text -

Just to make it easy:
Your test is "equal" because the strings contain the same content.
Java does this to save space.
 
P

Patricia Shanahan

www said:
I have just tested the following code:

String str1="Hello";
String str2="Hello";

if(str1 == str2)
{
System.out.println("equal");
}
else
{
System.out.println("Not equal");
}

Guess what? It prints out "equal".

"==" is absolutely reliable for testing whether two references are
either both null, or both point to the same object. There is only one
copy of each String constant expression, such as "Hello". Initializing
str1 and str2 by assignment of the same String literal ensures they
reference the same object.

"==" and ".equals" have the same behavior if the two expressions
reference the same object, or if they reference String objects with
different values. The important case for deciding which to use is what
behavior you want if str1 and str2 reference two distinct String objects
that have the same content. If you want that case to be "true" use
".equals". If you want it to be "false" use "==".

Patricia
 
J

Joe Attardi

Your test is "equal" because the strings contain the same content.
Java does this to save space.

Not quite... this is an oversimplification. Consider this:

String str1 = "Joe";
String str2 = "Joe";
System.out.println(str1 == str2); // prints "true"

String str1 = new String("Joe");
String str2 = new String("Joe");
System.out.println(str1 == str2); // prints "false"

In both cases the Strings contain the same content. But in the first
example, they are the exact same object (because, as Owen pointed out,
string constants are interned). So the == operator returns true.

In the second example, though, we're forcing the Strings to be two
separate objects. This time they do not refer to the same object.

Just tested on 1.5.0_13.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top