It will store into Heap. If you will use intern method then it will be pushed to String pool.
public static void main(String[] args) {
String strLiteral="Hello";
String str=new String("Hello");
System.out.println(str==strLiteral);
String str2=new String("Hello").intern();
System.out.println(str2==strLiteral);
}
Output:-
false
true
1) Look at the code above, I have created a String Literal Hello, which is stored in String Pool.
2) Now I have created a String object str
, if I check the references str and strLiteral they are not equal i.e. they are pointing to two different objects one in heap and other one in String pool.
3) Now I have created a String object str2
and called an intern method on it, JVM now will search the for "Hello" in String pool and return the reference to it. So now if I am checking strLiteral and str2 are equal i.e. pointing to the same object in String pool.