2

I'm wondering why the following code doesn't work:

String test = new String(new byte[] {92, 92, 92, 92, 92});
System.out.println(test);
String compare = "\\\\\\\\\\";
System.out.println(compare);
if (test == compare) {
System.out.println("Yes!");
}

The output is:

\\\\\
\\\\\

Where is a data type conversion happening that I'm not understanding?

Edit: /fail :(

2
  • Possible dup.: stackoverflow.com/questions/767372/java-string-equals-versus Commented Aug 27, 2009 at 16:57
  • its a dupe if you think i thought it was a problem with the equality i was using, yeah. if you read the title, i'm sure it will become obvious i had no idea this was a problem with equality Commented Aug 27, 2009 at 17:02

4 Answers 4

9

Strings are compared with .equals(), not with ==

The reason is that with references (as string variables are), == just checks equality in memory location, not in content.

The literal \\\ existed in one place in memory. the other one is created somewhere else where you build the string. They're not in the same location, so they don't return true when you do ==

You should do

if(test.equals(compare))
Sign up to request clarification or add additional context in comments.

1 Comment

That is correct. You are doing a reference comarison instead of a value comparision. They are not the same object, but they are equivalent. Use Equals and is should work.
5

Strings in Java are reference types, and == checks whether they are the same string, rather than equal strings. Confusing I know. Long story short you need to do this:

if( test.equals( compare ) ) {

For more you can see here: http://leepoint.net/notes-java/data/strings/12stringcomparison.html

Comments

3

You are testing to see if those are the same object, not if they are equal strings.

However the following test will be true:

test.intern() == compare.intern()

Comments

2

You are using identity comparison, rather than string comparison.

Try test.equals(compare). Then try test.intern() == compare. Both should work. The intern method is the only reliable way to perform object identity comparisons on String objects.

2 Comments

Just to raise a point about this answer, the "intern()" one only requires "test" to be intern()ed because "compare" is a literal in the source file (which means it's interned already). If it was created in the same way as "test", you'd need to use test.intern() == compare.intern() (and it's usually bad practice unless there's guaranteed to be a relatively small, or at least constrained, number of distinct strings).
Exactly! Thanks for pointing that out, Calum. Interning strings might be useful for an XML processor or something, where huge numbers of Strings could be created. It's sort of a Flyweight Pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.