java String对象的创建(jvm).

时间:2023-06-09 20:20:20

  本人目前也开始学习虚拟机,在java中,有很多种类型的虚拟机,其中就以sum公司(当然现在已经是oracle了)的虚拟机为例,介绍可能在面试的时候用到的,同时对自己了解String有很大帮助,这里仅仅是笔记的整理(张龙老师的).

 class StringDemo {
public static void main(String[] args) {
//String pool is in the 'Stack. //Retrieve whether there exist an instance "a"(In the String pool),found no(and create in the 'String pool').
String s = "a";
//Retrieve whether there exist an instance "a"(In the String pool),found yes(and not create).
String s2 = "a";
System.out.println(s == s2); //outputs 'true'. //Retrieve first in the String pool(found no,and create in the String pool),
//and then create one in the 'Heap',return the reference of the instance(in the heap).
String s3 = new String("b");
//Retrieve first in the String pool(found yes,and not create in the String pool),
//and then create one in the 'Heap'(each new create an instance),return the reference of the instance(int the heap).
String s4 = new String("b");
System.out.println(s3 == s4); //outputs 'false'. //Retrieve whether there exist an instance "c"(In the String pool),found no(and create in the 'String pool').
//Return the reference of the instance in the 'String pool'.
String s5 = "c";
//Retrieve first in the String pool(found yes,and not create in the String pool),
//and then create one in the 'Heap',return the reference of the instance(int the heap).
String s6 = new String("c");
System.out.println(s5 == s6); //outputs 'false'.
}
}