-3

I was wondering: Why does this code result in false?
Coz == operator should return true when it's the same memory point.

public static void main(String[] args) {
    String a = new String("hello");

    System.out.println(a == "hello");
}
  • Can you describe why this happens.
tkr
  • 1,331
  • 1
  • 9
  • 27
user2374612
  • 115
  • 1
  • 4
  • You answered your own question: "It should return true when it's the same memory point". Clearly it isn't the same memory point. – MarioDS May 12 '13 at 10:22
  • This question is asked around 100 times a day. There should be an auto answer for that. – Maroun May 12 '13 at 10:29

4 Answers4

5

Java compares references when you compare (==) objects. Since you create two new objects, they do have different addresses in memory regardless of their content.

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • 2
    It's not quite that simple. In the first instance the OP creates a `new String`. In the second instance the OP uses a `String` literal. So `System.out.println("hello" == "hello");` would in fact return `true`. Might be worth mentioning `String` interning. – Boris the Spider May 12 '13 at 10:23
  • @BoristheSpider that is correct but this is not the case here. He intetionally creates a new string everytime. – Michael-O May 12 '13 at 10:29
  • It's the statement `System.out.println(a == "hello");` I had in mind. – Boris the Spider May 12 '13 at 10:30
3

This happens because there are two String objects here - first is the literal and the other is the one created with new String. When you create explicitly a new String object it the String from pool is not reused. It is well described in details http://theopentutorials.com/tutorials/java/strings/string-literal-pool/

javadeveloper
  • 321
  • 1
  • 8
3

String literals are created on what is called: Pool Memory.

However, when one creates a String with explicitly new keyword, he actually creates a new String object, independent of the pool memory's one. Meaning that both references are not the same at all.

In your sample, you could solve the case by both ways:

_ use java.lang.String.intern method: placing your explicitly created String into the pool memory.

_ use only literals to create/reuse String.

For information, Pool Memory was created in order to avoid some useless creation of common/redundant literal Strings, thus optimizing memory space.

Similar concept exists for Integer: Integer caching in Java

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • 2
    Actually, from Java 7 onwards, the string pool is in the main heap: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6962931, http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html#jdk7changes – Stephen C May 12 '13 at 10:35
  • @Stephen C Oh :) I didn't know. – Mik378 May 12 '13 at 10:45
0

The command String a = new String("hello"); creates a String object in the non-pool part of the memory, and then a refers to it. And then "hello" is placed in the String pool. Whereas while comparing, a == "hello" the "hello", is in the String pool. So, the reference of both the objects are different, although the content of both the objects is same. Had to declared a, in the following manner, the comparison would had returned true

String a = "hello";
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48