Java中String对象创建


在开始之前我们了解一下什么叫做字符串驻留

当同时创建多个相同的字符串直接量是,只有一个字符串常量被存储在运行时常量池中。

如:

String a="abc";
String b="abc";
//String c=new String("abc");

此时同时创建了两个String对象(用双引号的方式),此时在运行时常量池中只有一个"abc"。对于String c=new String(“abc”),可以通过c.intern()获取c在运行时常量池里面的字符串驻留对象"abc",所以

String a=new String("abc");
String b=new String("abc");
System.out.println(a==b);//false
System.out.println(a.intern()==b.intern());//true

1 String a="abc"与String b=new String(“abc”)一样吗,难道也是语法糖???

首先是不一样的,不是语法糖。我们看一下以下代码:

 public static void main(String[] args) {
    	String a="abc";
    	String b="abc";
    	String c=new String("Hello world");
    	String d=new String("abc");
    	int length="abc".length();
    }

字节码:
在这里插入图片描述

从字节码中可以看到,String a=“abc"只创建了一个对象(就是”abc”,在运行时常量池)。然而String a=new String(“abc”)会创建额外的对象,看见new没有(意思是在堆上有创建了一个对象)。

a,b两个对象都是用ldc指令将常量"abc"(本身就是一个String类型)加载进操作数栈的,所以a==b是true。然而String d=new String(“abc”)会在堆中新new一个String对象,所以d==b为false。

通常创建String对象时,推荐使用双引号这种方式。因为new的方式会额外创建一个对象。

2 String a="abc"要创建几个对象??

通过上面的解释,现在我们就知道了String a=“abc"会创建一个对象(在运行时常量池),而String a=new String(“abc”)会创建两个对象(一个在运行时常量池的"abc”,一个在堆里面)。需要注意的是这是在String a="abc"与String a=new String(“abc”)不同时出现的情况下。

3 String a=“abc”;String b=new String(“abc”)又要创建几个对象???

其实理解上面的这个就好懂了。此时,String a="abc"与String b=new String(“abc”)是同时出现的。那么总共会创建2个对象。

首先String a="abc"在运行时常量池中创建了一个对象。而当String b=new String(“abc”)时,已经知道了运行时常量池中有一个对象"abc"了,那当然就不用在常量池中新创建一个“abc”咯!。只用在堆中创建一个对象就行了。

4 String a=“abc”;String b=“abc”;那么a==b吗???

那当然为true哦。正如前面所讲的,a与b都指向了运行时常量池中的"abc",所以那当然为true了。

5 String a=“hello”+“world”,创建几个对象?String b=“helloworld”,a==b吗??

大家先猜一下答案,是true还是false?

这里我们要扯到常量折叠这个概念。这是在早起编译(从java代码编译成字节码)的标注检查的一种优化。如:

int a=1+2;

经过常量折叠之后,“1”、“+”、“2”变成了字面量3,所以在代码中定义“int a=1+2"并不比”int a=3"增加程序运行期哪怕仅仅一个cpu指令的运算量。

相应的,我们的“hello"、”world"也是常量,经常常量折叠之后与“String b="helloworld"无异。所以a==b为true。所以String a=“hello”+"world"也只会产生一个对象

现在我们看一下代码

public class Main {

    public static void main(String[] args) {
	// write your code here
       String a="helloWorld";
       String b="hello"+"World";

       // String c=new String("hello"+"World");

        System.out.println(a==b);
    }
}

javap -verbose Main查看一下字节码:
在这里插入图片描述
就只有他,而没有hello和world。并且
在这里插入图片描述
这里用ldc加载常量也是加载的#2。也就是String a="helloworld"与String b=“hello”+"world"没有区别。

String c=new String(“hello”+“world”)创建了几个对象?

由于要经过常量折叠,所以"hello"+"world"与“helloworld"没有什么区别,所以

String c=new String("hello"+"world");

String c=new String("helloworld");

没什么区别,所以String c=new String(“hello”+“world”)也只产生两个对象
看代码

package com.company;

public class Main {

    public static void main(String[] args) {
         String c=new String("hello"+"World");
    }
}

javap -verbose Main
在这里插入图片描述
这样证实了产生了两个对象

6 下面程序创建几个对象?特别注意String b=new String(“hello”)+new String(“world”);产生几个对象?

String a="hello"+"world";

String b=new String("hello")+new String("world");

大家不妨猜一下?

首先对于String a=“hello”+“world”,那肯定只创建一个对象

而对于后一段代码,是创建7个对象。

  1. 首先”hello"与“world"两个在运行时常量池的两个对象。
  2. new String(“hello”)与new String(“world”)两个在堆中的对象。
  3. 由于java8中字符串拼接是用StringBuilder实现的,就需要一个StringBuilder对象。
  4. 而在StringBuilder对象里面有一个char[]数组。至于java中数组是不是一个对象我们先不讨论
  5. 最后调用StringBuilder对象的toString()方法里面又要new一个String对象

所以综上,要产生7个对象。现在我们看一下代码

package com.company;

public class Main {

    public static void main(String[] args) {
        String a="hello"+"world";
        String b=new String("hello")+new String("world");
    }
}

javap -verbose Main
在这里插入图片描述
从上图可以断定 String b=new String(“hello”)+new String(“world”);产生了5个对象。
现在我们看一下StringBuilder对象的append(String)方法

@Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

它调用了父类方法,于是进入 super.append(str)

 public AbstractStringBuilder append(String str) {
        if (str == null)
            return appendNull();
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);//这里的value是一个char[]数组
        count += len;
        return this;
    }

看到value没有,他是一个char[]数组,数组也算对象:

	/**
     * The value is used for character storage.
     */
    char[] value;

至此第6个对象我们找到了。
现在看一下第7个对象,由于随后要调用StringBuilder对象的toString()方法,所以我们看一下源码:

 @Override
    public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count);//这里产生了一个对象
    }

都这里第7个对象就出现了。

7 参考

字符串驻留

反编译工具jd-gui 链接,进去安装JD-GUI,JD-Eclipse与JD-IDEA可能不好用哦!!!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值