孕妇黄金素,中国游乐设备网,有恃无恐什么意思
封装char[] 字符数组,不可变(因为在底层代码中,值用final关键字修饰)
如果第一次用到一个字符串字面值,会在内存中“字符串常量池”内新建对象,当再次使用相同字面值时,
直接访问常量池中存在的实例,而不新建对象。
1 public class teststring { 2 3 public static void main(string[] args) { 4 char[] a = {'h','e','l','l','o'}; 5 string s1 = new string(a); //内存中新分配内存空间 6 string s2 = "hello"; //在常量池新建对象 7 string s3 = "hello"; //访问常量池存在的对象 8 9 system.out.println(s1); 10 system.out.println(s2); 11 system.out.println(s3); 12 13 system.out.println(s1 == s2); //输出false,内存地址不相等 14 system.out.println(s2 == s3); //输出true,内存地址相等,引用指向同一个对象 15 16 system.out.println(s1.equals(s2)); //比较字符内容 17 } 18 }
字符串一旦创建,字符串的内容不可变,任何对字符串修改的操作,都是在新建字符串。
接下来采用system.currenttimemillis()方法来观察用string拼接字符串的效率。先在拼接行为前记录当前时间,再在拼接行为后也记录一下时间,相减后即为拼接行为所花的时间。
1 public class test01 { 2 3 public static void main(string[] args) { 4 string s0 = "abcdefqhijklmnopqrstuvwxyz"; 5 string s = ""; 6 //系统当前时间点毫秒值 7 //毫秒值:1970-1-1 0点开始的毫秒值 8 long t = system.currenttimemillis(); 9 for(int i=0;i<100000;i++) { 10 s += s0; 11 } 12 t = system.currenttimemillis() - t; 13 system.out.println(t); 14 } 15 }
输出结果为140285,拼接行为花了140285毫秒,共创建100000个对象,显然效率很低,而且十分浪费空间。
stringbuilder、stringbuffer:
封装char[]字符数组,可变。(值没有用final修饰)
stringbuilder和stringbuffer的区别:
stringbuilder 线程不安全,效率高;stringbuffer 线程安全,效率低。通常都是用stringbuilder
采用stringbuilder进行字符串连接,通过在原字符数组基础上的数组扩容进行拼接,不会建立新对象,可以显著提高连接效率。
依旧采用system.currenttimemillis()方法来观察用stringbuilder拼接字符串的效率。
1 public class test02 { 2 3 public static void main(string[] args) { 4 string s0 = "abcdefqhijklmnopqrstuvwxyz"; 5 stringbuilder sb = new stringbuilder(""); 6 //系统当前时间点毫秒值 7 //毫秒值:1970-1-1 0点开始的毫秒值 8 long t = system.currenttimemillis(); 9 for(int i=0;i<100000;i++) { 10 sb.append(s0); 11 } 12 t = system.currenttimemillis() - t; 13 system.out.println(t); 14 } 15 }
输出结果为11,拼接行为只花了11毫秒,显然效率提高了很多很多。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论