首页 > 其他 > 详细

java String对象的创建(jvm).

时间:2014-01-20 19:24:45      阅读:339      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

java String对象的创建(jvm).

原文:http://www.cnblogs.com/listened/p/3526541.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!