String s = "a" + "b" + "c" + "d" + "
来源:互联网 发布:企业协作平台软件 编辑:程序博客网 时间:2024/04/29 08:37
最近看到许多类似的帖子,大致意思如下:
问题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();
8.
我们先看看StringBuilder的构造器
1. public StringBuilder() {
2. super(16);
3. }
看下去
1. AbstractStringBuilder(int capacity) {
2. value = new char[capacity];
3. }
可见,分配了一个16自己长度的char数组
我们看看append的整个过程(注意,源代码我从各个类进行了整合,他们实际上不在一个类里面的)
1.
2. public StringBuilder append(String str) {
3. super.append(str);
4. return this;
5. }
6.
7. public AbstractStringBuilder append(String str) {
8. if (str == null)
9. str = "null";
10. int len = str.length();
11. if (len == 0)
12. return this;
13. int newCount = count + len;
14. if (newCount > value.length)
15. expandCapacity(newCount);
16. str.getChars(0, len, value, count);
17. count = newCount;
18. return this;
19. }
20.
21. public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
22. if (srcBegin < 0) {
23. throw new StringIndexOutOfBoundsException(srcBegin);
24. }
25. if (srcEnd > count) {
26. throw new StringIndexOutOfBoundsException(srcEnd);
27. }
28. if (srcBegin > srcEnd) {
29. throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
30. }
31. System
32. .arraycopy(value, offset + srcBegin, dst, dstBegin, srcEnd - srcBegin);
33. }
可见,我们的代码不会超过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.
11. public static char[] copyOf(char[] original, int newLength) {
12. char[] copy = new char[newLength];
13. System
14. .arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
15. return copy;
16. }
可见,expand容量时,增加为当前(长度+1)*2。
注意这里用了Arrays的方法,注意不是前面的 System.arraycopy方法哦。这里产生了一个新的
copy的char数组,长度为新的长度
总结:三个对象分别为
1 StringBuilder
2 new char[capacity]
3 new String(value,0,count);
如果说String对象,则为1个。
- String s = "a" + "b" + "c" + "d" + "
- " " ' '
- extern "c"
- 你知道在C#中,string="aa"+"bb"+"cc"+"dd"共
- char *s="string"和char s[]="string"的区别
- about"spaceused"
- 说好"but"
- "CACHE-CONTROL" content="NO-CACHE"
- here."
- 程序设计 "
- "基于对象" 和 "面向对象"
- "git rm" 和 "rm" 的区别
- "转义
- 关于".set push"和".set pop"
- $("#id").val()取值textarea是""
- 何为"IOE"、"去IOE"
- 何为"IOE"、"去IOE"
- 何为"IOE"、"去IOE"
- 数据结构之双链表
- 女生适合干【长期第一线编程工作】 男生能干的女生照样可以做的更好
- error:1366 乱码问题(转)
- 学习Java
- 今日二级进行时
- String s = "a" + "b" + "c" + "d" + "
- ASDAS
- 自定义RCP外观
- 程序员不擅长沟通 ?——Leo网上答疑36
- 启动Mysql 错误 1067 进程意外终止的一种解决方法(转)
- 我的第一篇日志
- 形参为t[][N]与形参为*t有何区别
- 1
- 2