Arif Setyawan
Arif Setyawan

Reputation: 187

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

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.

Upvotes: 4

Views: 999

Answers (7)

Rangi Lin
Rangi Lin

Reputation: 9451

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

Upvotes: 1

dash1e
dash1e

Reputation: 7807

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.

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70909

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.

Upvotes: 5

Sam
Sam

Reputation: 11

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.

Upvotes: 0

kasavbere
kasavbere

Reputation: 6003

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.

Upvotes: 0

freeone3000
freeone3000

Reputation: 384

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 .

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182779

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.

Upvotes: 1

Related Questions