0

What is difference between ?

Allocating a variable string literal By: String a="string"; and String a =new String("string");

18th Dec 2019, 9:47 PM
Raj Shekhar
Raj Shekhar - avatar
3 Réponses
+ 4
In the 1st case it is a string literal and is directly stored in the string constant pool which is a small area on the heap. While in the other case, an instance of String is created on the heap with the value "string" and it is pointed by "a" which is stored in the stack.
19th Dec 2019, 1:28 AM
Avinesh
Avinesh - avatar
+ 1
"string" is faster.
18th Dec 2019, 9:58 PM
Seb TheS
Seb TheS - avatar