I know in case of new String("Hi") it will create two objects one in heap and other in stringpool . I am curious about do these two objects share any linkage if yes then how ?
Asked
Active
Viewed 47 times
0
-
1No, there's no link between them other than that they have the same contents. That said, there's essentially never any good reason to write that code. – Louis Wasserman Apr 27 '15 at 16:14
-
1http://stackoverflow.com/questions/390703/what-is-the-purpose-of-the-expression-new-string-in-java – Jean-François Savard Apr 27 '15 at 16:17
1 Answers
0
new String("hi"). It will store Hi in the string constant pool and the reference is passed to the String variable referring this object.
public static void main(String[] args) {
// TODO Auto-generated method stub
String string=new String("Hi");
}
Traces for the above program.
TraceEntry
Entry : programIt/stringval.main([Ljava/lang/String;)V Bytecode static method
Entry : java/lang/String.<init>(Ljava/lang/String;)V Bytecode method, This = 0xfffb93c0
Entry : java/lang/Object.<init>()V Compiled method, This = 0xfffb93c0
Exit : java/lang/Object.<init>()V Compiled method
Exit : java/lang/String.<init>(Ljava/lang/String;)V Bytecode method
Exit : programIt/stringval.main([Ljava/lang/String;)V Bytecode static method
It has only one string Entry and Exit which obviously means it has one reference present. if 2 reference exist then there will be 2 internal String calls present for 2 references.

Mohan Raj
- 1,104
- 9
- 17
-
But I think there will be one object in heap which has value as "hi" and other string literal object in string pool ...Correct me if I am wrong ... can you share that source code snippet where this reference passing is done? – Rahul Shrivastava Apr 27 '15 at 17:34
-