本人目前也开始学习虚拟机,在java中,有很多种类型的虚拟机,其中就以sum公司(当然现在已经是oracle了)的虚拟机为例,介绍可能在面试的时候用到的,同时对自己了解String有很大帮助,这里仅仅是笔记的整理(张龙老师的).
1 class StringDemo { 2 public static void main(String[] args) { 3 //String pool is in the ‘Stack. 4 5 //Retrieve whether there exist an instance "a"(In the String pool),found no(and create in the ‘String pool‘). 6 String s = "a"; 7 //Retrieve whether there exist an instance "a"(In the String pool),found yes(and not create). 8 String s2 = "a"; 9 System.out.println(s == s2); //outputs ‘true‘. 10 11 //Retrieve first in the String pool(found no,and create in the String pool), 12 //and then create one in the ‘Heap‘,return the reference of the instance(in the heap). 13 String s3 = new String("b"); 14 //Retrieve first in the String pool(found yes,and not create in the String pool), 15 //and then create one in the ‘Heap‘(each new create an instance),return the reference of the instance(int the heap). 16 String s4 = new String("b"); 17 System.out.println(s3 == s4); //outputs ‘false‘. 18 19 //Retrieve whether there exist an instance "c"(In the String pool),found no(and create in the ‘String pool‘). 20 //Return the reference of the instance in the ‘String pool‘. 21 String s5 = "c"; 22 //Retrieve first in the String pool(found yes,and not create in the String pool), 23 //and then create one in the ‘Heap‘,return the reference of the instance(int the heap). 24 String s6 = new String("c"); 25 System.out.println(s5 == s6); //outputs ‘false‘. 26 } 27 }