-3

What exactly is the difference between the two statement

String s1="abc";
String s2=new String("abc");

From what i Know is that the first statement will create a object in String pool i.e and s1 will reefer it. In Second statement the it will create two object because I used new keyword and s2 will refer the object in String pool

Now if I execute both statement one after another .Since will the first statement the object "abc" will be in string pool and with the execution of second statement s2 will refer to object which is alreday there in string pool is if i do s1==s2 it should return true however its returning false.

can you please explain why?

user728907
  • 33
  • 6

2 Answers2

5

The fact is String s1="abc" allocates the string inside the string pool, which is a special place in which immutable strings are kept. You won't be able to modify directly "abc" but just the reference (s1) which points to it.

In the second case String s2=new String("abc") you are allocating a real string object that internally has a char[] buffer in which the string data is stored. It's immutable like the first one but it's an object onto the heap.

When you compare s1 == s2, since you are comparing references, they are different because one points to the string in the string pool (I'm actually unsure if a wrapper object is created or a direct reference to the object in the pool is used) while the second one points to the object that you created explicitly (which wraps a char[] buffer in which the data is stored).

Jack
  • 131,802
  • 30
  • 241
  • 343
  • The second statement will create two objects one in real heap and another in string constant pool....correct me if I am wrong since abc would already be there(because of String s1="abc") s2 will just refer string which is already...so basically both s1 and s2 will refer two same string ...plaese correct me where ia m missing the point – user728907 Feb 19 '13 at 18:26
  • 1
    no, `s2` refers to an object that refers to a string in the string pool. Not to the string directly. – Jack Feb 19 '13 at 18:29
  • so I get it does it means that s1 refer to abc which is in string pool while s2 refer to an object which to an string in string pool....is it ok?? – user728907 Feb 19 '13 at 18:34
  • Just checked implementation of openjdk: the `String(String str)` constructor even creates an internal buffer `char[]` in which it copies the string that is passed, so it doesn't neither refer to the string pool, is a completely different object. (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.%3Cinit%3E%28java.lang.String%29) – Jack Feb 19 '13 at 18:39
  • oh thanks it really helped me now i can proceed with collections – user728907 Feb 19 '13 at 18:44
1

you are creating two different objects. s1 has its own memory to hold its reference address. Same with s2. Though both of these objects point to the same string in the string pool, the objects themselves are distinct. Therefore, s1==s2 will fail.

s1.equals(s2), however, will work since you are comparing the string contents

75inchpianist
  • 4,112
  • 1
  • 21
  • 39
  • correct me if i am wrong but if we don't override the equals method its default behavior is that of == so why it will return true.(Dont know whether this is a logical question) – user728907 Feb 19 '13 at 18:18
  • 1
    String implementation of equals overrides the Object.equals. it checks the contents of the string, rather than the object itself – 75inchpianist Feb 19 '13 at 18:19