why is it possible using "==" when comparing string, java 1.6.0_29, osx?

Go To StackoverFlow.com

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.

2012-04-05 00:01
by Arif Setyawan
Short answer: You got (un)lucky - Mysticial 2012-04-05 00:02
possible duplicate of In JDK 1.6, can String equals operation can be replaced with ==?Adam Mihalcin 2012-04-05 00:03
Shorter answer: use .equalTravis J 2012-04-05 00:04
But it's a valid question that should have a reasonable explanation (which I don't have - Bohemian 2012-04-05 00:05
@TravisJ .equals - assylias 2012-04-05 00:10
@assylias - indeed ;) a.equals(b). sorry for the syntax erro - Travis J 2012-04-05 00:18
@TravisJ Not sure where you are located but it's getting late here - could be the reason for the typo - assylias 2012-04-05 00:19
@TravisJ The OP knows equals() and he is looking for an explanation on how different OS give different results - Eng.Fouad 2012-04-05 00:21
@Arif BTW, I used the code you provided, and it works well on windows 7 - Eng.Fouad 2012-04-05 00:25
@Eng.Fouad - The inconsistent results are caused by using an inconsistent approach - Travis J 2012-04-05 00:26
@Mystical how can it be so (un)lucky - Arif Setyawan 2012-04-05 01:16
i just found the similar case in hereArif Setyawan 2012-04-05 01:18


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.

2012-04-05 00:21
by Edwin Buck
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 2012-04-05 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 2012-04-05 14:58
No argument here, especially since the first check in String (and indeed in most classes) is if (this == other) return true; - yshavit 2012-04-05 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.

2012-04-05 00:04
by David Schwartz


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 .

2012-04-05 00:04
by freeone3000
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 2012-04-05 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
2012-04-05 00:51
by Rangi Lin
Thanks, the method of checking was helpful. : - Arif Setyawan 2012-04-05 01:25


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.

2012-04-05 00:05
by kasavbere
@TravisJ how so - Eng.Fouad 2012-04-05 00:12
@TravisJ Java has no such thin - David Schwartz 2012-04-05 00:14
@Eng.Fouad - Guess not - Travis J 2012-04-05 00:20
@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 2012-04-05 00:21
People are lame for down-voting, as you are dead on correct - James 2012-04-05 00:23


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.

2012-04-05 00:06
by Sam
I find it amusing that people willing to downvote you are not competent enough to understand Java's == vs. equals semantics - James 2012-04-05 00:24
Thanks James...as do I. To each his own I suppose - Sam 2012-04-05 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.

2012-04-05 00:42
by dash1e
Ads