When should one compare String
s as objects and when should one use their equals
method? To make sure, I always use equals
, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2
is a safe to use?
String comparison and String interning in Java
Asked Answered
String.equals() is efficient. The first thing checked is if string1 == string2. (Actually Object.equals() checks this.) –
Burnie
But of course! How stupid of me. It's a very obvious thing to do. And highly efficient one as you say. Never though of it. Shame on me! Thanks! –
Fastening
You should almost always use equals
. You can be certain that string1 == string2
will work if:
- You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for some other reason)
- You know you're dealing with compile-time string constants
- You've manually interned the strings yourself
It really doesn't happen very often, in my experience.
Absolutely the right answer. Using '==' because equals() is inefficient is a great example of premature optimization. –
Withdrawn
And String.intern() is slow enough to be useless for performance in most cases. –
Guppy
From what I know of Java, string1==string2
will only be true if the references to those objects are the same. Take a look at the following case
String string1 = new String("Bob");
String string2 = new String("Bob");
string1 == string2; // false, they are seperate objects
string1 = string2; // asigning string1 to string2 object
string1 == string2; // true, they both refer to the same object
The first comparison will not be false because they are not separate objects. String literals are interned in Java. –
Looming
The first comparison should return false in this case- String string1 = new("Bob"); String string2 = new("Bob"); –
Savdeep
I'll edit my responce to make it more clearly defined. I thought I might run into this issue with Java. –
Eli
@stratwine: No, this is not true. See java.sun.com/docs/books/jls/third_edition/html/…: Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern. –
Earthly
The answer is correct, and the first comparison is 'false'. "new String(...)" creates (surprise surprise) a new String, which is not the same object as the interned "Bob". –
Withdrawn
@Earthly - Strings created at compile time are interned. Not the Strings that are created on the fly. new operator creates the String at runtime and there's no interning involved here. –
Savdeep
@stratwine: Seems I misread your comment. I thought that you'd tried to "expand" the poster's original formulation of the initial assignments instead of providing an alternative case to elaborate on. Apologies. –
Earthly
You can only use the ==
for comparison if you are sure the objects are the same.
For example, this could occur if you had a final static String variable. You could be certain that a comparison would be between the same object.
Stick with the equals
for string comparison.
© 2022 - 2024 — McMap. All rights reserved.