java,string详解
- Constant Pool常量池的概念:
-
- 在讲到String的一些特殊状况时,总会提到String Pool或者Constant Pool,可是我想不少人都不太
- 明白Constant Pool究竟是个怎么样的东西,运行的时候存储在哪里,因此在这里先说一下Constant Pool的内容.
- String Pool是对应于在Constant Pool中存储String常量的区域.习惯称为String Pool,也有人称为
- String Constant Pool.好像没有正式的命名??
-
- 在java编译好的class文件中,有个区域称为Constant Pool,他是一个由数组组成的表,类型
- 为cp_info constant_pool[],用来存储程序中使用的各类常量,包括Class/String/Integer等各
- 种基本Java数据类型,详情参见The Java Virtual Machine Specification 4.4章节.
-
-
- 对于Constant Pool,表的基本通用结构为:
- cp_info {
- u1 tag;
- u1 info[];
- }
-
-
- tag是一个数字,用来表示存储的常量的类型,例如8表示String类型,5表示Long类型,info[]根据
- 类型码tag的不一样会发生相应变化.
-
- 对于String类型,表的结构为:
- CONSTANT_String_info {
- u1 tag;
- u2 string_index;
- }
-
- tag固定为8,string_index是字符串内容信息,类型为:
- CONSTANT_Utf8_info {
- u1 tag;
- u2 length;
- u1 bytes[length];
- }
-
- tag固定为1,length为字符串的长度,bytes[length]为字符串的内容.
-
- (如下代码在jdk6中编译)
- 为了详细理解Constant Pool的结构,咱们参看一些代码:
- String s1 = "sss111";
- String s2 = "sss222";
- System.out.println(s1 + " " + s2);
-
- 因为"sss111"和"sss222"都是字符串常量,在编译期就已经建立好了存储在class文件中.
- 在编译后的class文件中会存在这2个常量的对应表示:
- 08 00 11 01 00 06 73 73 73 31 31 31 08 00 13 01 ; ......sss111....
- 00 06 73 73 73 32 32 32 ; ..sss222
-
- 根据上面说的String常量结构,咱们分析一下
- 开始的08为CONSTANT_String_info结构中的tag,而11应该是它的相对引用,01为
- CONSTANT_Utf8_info的tag,06为对应字符串的长度,73 73 73 31 31 31为字符串对
- 应的编码,接着分析,会发现后面的是对应"sss222"的存储结构.
-
-
- 通过上面分析,咱们知道了11和13是两个字符串的相对引用,就能够修改class文件
- 来修改打印的内容,把class文件中的
- 00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 12 4D
- 改为
- 00 6E 00 04 00 03 00 00 00 24 12 10 4C 12 10 4D
- 程序就会输出sss111 sss111,而不是和原程序同样输出sss111 sss222,由于我
- 们把对"sss222"的相对引用12改为了对"sss111"的相对引用10.
-
-
- ------------分割线
- public class Test {
- public static void main(String[] args) {
- String s1 = "sss111";
- String s2 = "sss111";
- }
- }
-
- 在上面程序中存在2个相同的常量"sss111",对于n个值相同的String常量,在Constant Pool中
- 只会建立一个,因此在编译好的class文件中,咱们只能找到一个对"sss111"的表示:
- 000000abh: 08 00 11 01 00 06 73 73 73 31 31 31 ; ......sss111
-
-
- 在程序执行的时候,Constant Pool会储存在Method Area,而不是heap中.
-
- 另外,对于""内容为空的字符串常量,会建立一个长度为0,内容为空的字符串放到Constant Pool中,
- 并且Constant Pool在运行期是能够动态扩展的.
-
-
- 关于String类的说明
- 1.String使用private final char value[]来实现字符串的存储,也就是说String对象建立以后,就不能
- 再修改此对象中存储的字符串内容,就是由于如此,才说String类型是不可变的(immutable).
-
- 2.String类有一个特殊的建立方法,就是使用""双引号来建立.例如new String("i am")实际建立了2个
- String对象,一个是"i am"经过""双引号建立的,另外一个是经过new建立的.只不过他们建立的时期不一样,
- 一个是编译期,一个是运行期!
-
- 3.java对String类型重载了+操做符,能够直接使用+对两个字符串进行链接.
-
- 4.运行期调用String类的intern()方法能够向String Pool中动态添加对象.
-
- String的建立方法通常有以下几种
- 1.直接使用""引号建立.
- 2.使用new String()建立.
- 3.使用new String("someString")建立以及其余的一些重载构造函数建立.
- 4.使用重载的字符串链接操做符+建立.
-
- 例1
-
- String s1 = "sss111";
-
-
- String s2 = "sss111";
-
-
- System.out.println(s1 == s2);
-
-
- 例2
-
- String s1 = new String("sss111");
-
-
- String s2 = "sss111";
-
-
- System.out.println(s1 == s2);
-
-
- 例3
- String s1 = new String("sss111");
-
- s1 = s1.intern();
-
- String s2 = "sss111";
-
-
- System.out.println(s1 == s2);
-
-
- 例4
- String s1 = new String("111");
- String s2 = "sss111";
-
-
- String s3 = "sss" + "111";
-
-
- String s4 = "sss" + s1;
-
- System.out.println(s2 == s3);
- System.out.println(s2 == s4);
- System.out.println(s2 == s4.intern());
-
-
- 例5
- 这个是The Java Language Specification中3.10.5节的例子,有了上面的说明,这个应该不难理解了
- package testPackage;
- class Test {
- public static void main(String[] args) {
- String hello = "Hello", lo = "lo";
- System.out.print((hello == "Hello") + " ");
- System.out.print((Other.hello == hello) + " ");
- System.out.print((other.Other.hello == hello) + " ");
- System.out.print((hello == ("Hel"+"lo")) + " ");
- System.out.print((hello == ("Hel"+lo)) + " ");
- System.out.println(hello == ("Hel"+lo).intern());
- }
- }
- class Other { static String hello = "Hello"; }
-
- package other;
- public class Other { static String hello = "Hello"; }
-
- 输出结果为true true true true false true,请自行分析!
-
-
- 结果上面分析,总结以下:
- 1.单独使用""引号建立的字符串都是常量,编译期就已经肯定存储到String Pool中.
- 2.使用new String("")建立的对象会存储到heap中,是运行期新建立的.
- 3.使用只包含常量的字符串链接符如"aa" + "aa"建立的也是常量,编译期就能肯定,已经肯定存储到String Pool中.
- 4.使用包含变量的字符串链接符如"aa" + s1建立的对象是运行期才建立的,存储在heap中.
- 6.使用"aa" + s1以及new String("aa" + s1)形式建立的对象是否加入到String Pool中我不太肯定,多是必须
- 调用intern()方法才会加入,但愿高手能回答 @_@
-
-
- 还有几个常常考的面试题:
-
- 1.
- String s1 = new String("s1") ;
- String s2 = new String("s1") ;
- 上面建立了几个String对象?
- 答案:3个 ,编译期Constant Pool中建立1个,运行期heap中建立2个.
-
-
- 2.
- String s1 = "s1";
- String s2 = s1;
- s2 = "s2";
- s1指向的对象中的字符串是什么?
- 答案: "s1"
- 综上:
- String str1 = "a";
- String str2 = "b";
- String str3 = new String("a");
-
- System.out.println(str1 == str3);
-
- String str4 = "a"+str2;
- String str5 = "a"+"b";
- String str6 = new String("ab");
- String str7 = "ab";
- System.out.println(str4 == str5);
- System.out.println(str4.intern() == str5);
- System.out.println(str4 == str6);
- System.out.println(str5 == str7);
-
- String str8 = str6;
- System.out.println(str6 == str8);
-
- String str9 = new String("ab");
- System.out.println(str6 == str9);
欢迎关注本站公众号,获取更多信息