yesterday(April 5th 2012) i'am trying comparing string which is in environment:
computer 1
computer 2
computer 3
This is the code i'am trying
public class TComp{
public static void main(String[] args){
String a = "arif";
String b = "arif";
if(a==b){
System.out.println("match!");
}
}
}
As far as i know, to compare string in java we should using .equal() function and '==' will do interning in this case. But with those all computer with different OS, why intern work fine in computer 1, while i got error in computer 2 and computer 3?
please correct if any kind of word i've wrong. thank you.
.equal
Travis J 2012-04-05 00:04
equals()
and he is looking for an explanation on how different OS give different results - Eng.Fouad 2012-04-05 00:21
In the same class, all string constants are folded into the .class
file constant pool by the compiler (at compile time). This means the compiler will only store one copy of the string (because who needs two identical constants in the pool?).
This means that within a class, ==
comparison of strings often works; however, before you get too excited, there is a good reason you should never use ==
comparison of strings. There is no guarantee that the two strings you compare both came from the in-class constant pool.
So,
"foo" == new String("foo")
is entirely likely to fail, while
"foo" == "foo"
might work. That might depends heavily on the implementation, and if you code to the implementation instead of the specification, you could find yourself in for a very nasty surprise if the implementation changes because the specification doesn't actually require that implementation.
In short, use .equals(...)
for Object
comparison, every time. Reserve ==
for primitive comparison and "this is the same object instance" comparison only. Even if you think that the two Strings
might be interned (or the same object), as you never know when you will be running under a different classloader, on a different JVM implementation, or in a machine that simply decided to not intern everything.
"foo" == "foo"
should always return true -- by Java spec, not as an implementation detail. Meanwhile, "foo" == new String("foo")
should always be false (though "foo" = new String("foo").intern()
should be true). If a JVM isn't doing that, I'd say it's a bug - yshavit 2012-04-05 05:02
.equals(Object)
as that is the content equality operator. If you decide to do otherwise, you had better really structure your code around instance equality. Prefering instance equality isn't a bad thing, until you treat it like content equality. Preferring instance equality inconsistently is a sign of premature optimization - Edwin Buck 2012-04-05 14:58
if (this == other) return true;
- yshavit 2012-04-05 15:39
On one computer they were the same object on the other they weren't. The rules for the language don't specify whether they're the same object or not, so it can happen either way.
String interning is entirely up to the compiler. == does not "intern" anything; it simply compares object identity. In some cases, a and b can point to the same object. In other cases, they don't. Both are legal, so you should indeed use .equals().
See also http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5 .
Moreover, a string literal always refers to the same instance of class String. This is because 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.
yshavit 2012-04-05 04:58
This is because Java do String interning whenever you create a compile-time constant string.
JLS 15.28. Constant Expressions
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.
That's why you get a true when use "==" to compare, because they ARE actually the same object., String.valueOf() work the same way as string constants.
String x = "a";
String y = "a";
System.out.println(x == y); // true
String w = new String("b");
String z = "b";
System.out.println(w == z); // false
The use of == to compare objects is simply not reliable. You should never use == to compare Objects for equality unless you are truly looking for the exact same instance.
The == operator determines whether the two objects references are referring to the same instance.
On the other hand, the .equals() method compares the actual characters within the object.
These should be irrelevant with regards to which computer you are on.
The best thing is always to use .equals to compare objects. But with String if you need for some strange reason using == operator you need to be sure to compare the results of .intern method. It returns always the interned value and the doc tells that it is unique. The doc say that all the consts are interned and unique too.