String s = a+b+c+d+e;创建了几个对象的详细分析?

问题1: String s = "a" + "b" + "c" + "d" + "e"; 
问此语句共创建了几个对象, 

答案是 
就创建了一个 
String s = "a" + "b" + "c" + "d" + "e"; 
赋值符号右边的"a"、"b"、"c"、"d"、"e"都是常量 
对于常量,编译时就直接存储它们的字面值而不是它们的引用 
在编译时就直接讲它们连接的结果提取出来变成了"abcde" 
该语句在class文件中就相当于String s = "abcde" 
然后当JVM执行到这一句的时候, 就在String pool里找 
如果没有这个字符串,就会产生一个 


问题2:但是如果改成 String s = a+b+c+d+e; 
呢 又是几个了。 

就是说上面是一个是因为 "a"、"b"、"c"、"d"、"e"都是常量 
但如果是变量呢? 
我的答案是3个对象,但只有一个String对象:

由于编译器的优化,最终代码为通过StringBuilder完成:
  1. StringBuilder builder = new StringBuilder(); 
  2. builder.append(a); 
  3. builder.append(b); 
  4. builder.append(c); 
  5. builder.append(d); 
  6. builder.append(e); 
  7. String s = builder.toString(); 
我们先看看StringBuilder的构造器
  1.     public StringBuilder() {
  2.      super(16);
  3.     }
看下去
  1.     AbstractStringBuilder(int capacity) {
  2.         value = new char[capacity];
  3.     }
可见,分配了一个16自己长度的char数组 我们看看append的整个过程 (注意,源代码我从各个类进行了整合,他们实际上不在一个类里面的)
  1.   public StringBuilder append(String str) {
  2.     super.append(str);
  3.     return this;
  4.   }
  5.   public AbstractStringBuilder append(String str) {
  6.     if (str == null)
  7.       str = "null";
  8.     int len = str.length();
  9.     if (len == 0)
  10.       return this;
  11.     int newCount = count + len;
  12.     if (newCount > value.length)
  13.       expandCapacity(newCount);
  14.     str.getChars(0, len, value, count);
  15.     count = newCount;
  16.     return this;
  17.   }
  18.   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  19.     if (srcBegin < 0) {
  20.       throw new StringIndexOutOfBoundsException(srcBegin);
  21.     }
  22.     if (srcEnd > count) {
  23.       throw new StringIndexOutOfBoundsException(srcEnd);
  24.     }
  25.     if (srcBegin > srcEnd) {
  26.       throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
  27.     }
  28.     System
  29.         .arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
  30.   }
可见,我们的代码不会超过16个,所以不会出现扩展value的情况。 而append里面使用了arraycopy的复制方式,也没有产生新的对象。 最后,我们再看StringBuilder的 toString()方法:
  1. public String toString() {
  2.     // Create a copy, don't share the array
  3.     return new String(value, 0, count);
  4.   }
这里通过前面的数组生成了一个新的String。 大家注意那个默认的16容量,如果题目出现了总长度超过16,则会出现如下的再次分配的情况
  1.   void expandCapacity(int minimumCapacity) {
  2.     int newCapacity = (value.length + 1) * 2;
  3.     if (newCapacity < 0) {
  4.       newCapacity = Integer.MAX_VALUE;
  5.     } else if (minimumCapacity > newCapacity) {
  6.       newCapacity = minimumCapacity;
  7.     }
  8.     value = Arrays.copyOf(value, newCapacity);
  9.   }

  10.   public static char[] copyOf(char[] original, int newLength) {
  11.     char[] copy = new char[newLength];
  12.     System
  13.         .arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
  14.     return copy;
  15.   }
可见,expand容量时,增加为当前(长度+1)*2。
注意这里用了Arrays的方法,注意不是前面的 System.arraycopy方法哦。这里产生了一个新的
copy的char数组,长度为新的长度

总结:三个对象分别为
1 StringBuilder
2 new char[capacity]
3 new String(value,0,count);

如果说String对象,则为1个。

参考:http://blog.csdn.net/java2000_net/article/details/3681385

String a="hello world"; //在java中有一个常量池,当创建String 类型的引用变量给它赋值时,java会到它的常量池中找"hello world"是不是在常量池中已存在。如果已经存在则返回这个常量池中的"hello world"的地址(在java中叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址值,而这个值是指向一个字符串对象的。在程序中凡是以"hello world"这种常量似的形式给出的都被放在常量池中。 String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么会出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址值,只是java为了安全不允许我们对想c语言中的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存中的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆中开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值