4

yesterday(April 5th 2012) i'am trying comparing string which is in environment:

computer 1

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. OS X 10.7.3

computer 2

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. Window 7

computer 3

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. Linux Ubuntu 11.10

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.

Arif Setyawan
  • 187
  • 1
  • 6

7 Answers7

5

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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • According to JLS 3.10.5, `"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 Apr 05 '12 at 05:02
  • @yshavit I am not arguing that you are wrong, or that "foo" == "foo" should not return true; however, "foo" == "foo" is _almost always_ the wrong comparison. If you are comparing objects for content equality (and Strings _are_ Objects), then you should use `.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 Apr 05 '12 at 14:58
  • No argument here, especially since the first check in String (and indeed in most classes) is `if (this == other) return true;`. – yshavit Apr 05 '12 at 15:39
1

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

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 .

freeone3000
  • 387
  • 4
  • 10
  • The very link you reference refutes your claim that interning is up to the compiler. `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 Apr 05 '12 at 04:58
1

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
Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
0

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.

kasavbere
  • 5,873
  • 14
  • 49
  • 72
  • @TravisJ Java has no such thing – David Schwartz Apr 05 '12 at 00:14
  • @DavidSchwartz - You are correct, I actually thought that it was a pretty common functionality to offer operator overloading. This is a good discussion of the matter: http://javarevisited.blogspot.com/2011/08/why-java-does-not-support-operator.html , thanks for the correction. – Travis J Apr 05 '12 at 00:21
0

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.

Sam
  • 11
  • I find it amusing that people willing to downvote you are not competent enough to understand Java's == vs. equals semantics. – James Apr 05 '12 at 00:24
  • Thanks James...as do I. To each his own I suppose? – Sam Apr 05 '12 at 18:08
0

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.

dash1e
  • 7,677
  • 1
  • 30
  • 35