Java中的字符串

来源:互联网 发布:centos 7 锁屏 编辑:程序博客网 时间:2024/05/29 17:42

Java中的字符串常量

Java中的字符串是不可改变的,当然,它与字符串常量,比如:

public static final String a="123";

是不同的,此处指的是a只能指向字符串123,而我们今天指的不可改变指的是字符串在内存中不可变,现在我们看一个例子:

package com.test;/** * Created by siege on 2015-08-02. */public class TestString {    public static void main(String[] args) {        String a="0";        for (int i = 1; i <10 ; i++) {            a+=i;            System.out.println(a);        }    }}

其输出结果为:

01
012
0123
01234
012345
0123456
01234567
012345678
0123456789

当然,结果并不奇怪,表面上看起来,a的值一直在变,好像a最初指向的“0”变成了"0123456789",其实不是这样的,实际上上述的字符串都存在内存中,只不过a的引用一直在变。

实际上,在java中存在一个字符串池(String pool,由JVM维护),当我们给变量a赋值一个字符串时(比如”0”),首先,JVM会在String pool中找是否有”0”这个字符串,如果有,那么直接将该字符串在String pool中的引用返回,使a指向这个引用,如果没有的话,那么创建该字符串,然后将该字符串的引用返回给a,举例说明:

package com.test;/** * Created by siege on 2015-08-02. */public class TestString {    public static void main(String[] args) {       String a="hello";        String b="hello";        System.out.println(a==b);    }}

其结果为true,这就说明了a和b指向的是同一个字符串的引用,但是如果我们这样创建:

package com.test;/** * Created by siege on 2015-08-02. */public class TestString {    public static void main(String[] args) {       String a="hello";        String b=new String("hello");        System.out.println(a==b);    }}

其结果为false,究其原因,使用new创建的字符串对象是存在堆中的,故他们的地址不同。继续看:

public class TestString {    public static void main(String[] args) {        String a="abc";        String b="def";        String c=a+b;        System.out.println(c=="abcdef");    }}

其结果为false,这说明了c指向的是堆内存中的”abcdef”,不信我们继续看:

public class TestString {    public static void main(String[] args) {        String a="abc";        String b="def";        String c=a+b;        String d="abcdef";        System.out.println(c==d);    }}

其结果也为false,这样就证明了c指向的并不是String pool中的常量”abcdef”,那么它必然是指向堆中的”abcdef”,进一步深入,实际上 String a="abc" 在编译期间JVM就已经将a变量的值”abc”放入String pool中了,String c=a+b只有在运行期间才能知道c的值,故其是在堆中创建的,在堆中创建的还有两个String对象,a和b,他们是将String pool中的a,b的值赋值到堆中去的,在堆中创建两个对象,然后建立对象c,将"abcdef" 的堆地址赋给c。

public class TestString {    public static void main(String[] args) {        String a="abc"+"def";        System.out.println(a=="abcdef");    }}

输出结果为true,说明JVM是将"abcdef" 放入String pool中的。

public class TestString {    public static void main(String[] args) {        String a="abc";        String b=a+"def";        System.out.println(b=="abcdef");    }}

其结果为false,这也说明了b是在堆中创建的。

public class TestString {    public static void main(String[] args) {        String a=new String("abc");        String b=a.intern();        String c="abc";        System.out.println(b==c);    }}

intern()方法是将堆中创建的a对象的字符串放入到String pool,不过在放入之前先检查是否有该字符串,有的话就无需放入,没有的话就将其放入String pool中,并将其引用返回给b。故上述结果为true

关于设置String pool’的意义在于减少相同内容字符串的创建,节省内存空间,缺点就是在存放字符串到String pool中是需要进行计算该String pool中是否已经有该字符串了。

1 0