0

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 ?

1 Answers1

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
  • Edited above with more clarifications – Mohan Raj Apr 27 '15 at 18:10