String / StringBuffer / StringBuilder之间的区别

 

点击打开链接--------android培训java培训、期待与您交流!-----------

 

(1)String:不变类其内容不可改变

 

(2)StringBuffer:可变类其内容可改变,线程安全的

 

(3)StringBuilder:可变类,非线程安全

 

String s = "11";

s+="22";

s+="33";

 

StringBuffer sb = new StringBuffer("11");

sb.append("22");

sb.append("33");

String s = new String(sb);

 

 

----------------------

String常量池

----------------------

String s1 = "hello";  //取自常量池

String s2 = "hello";  //取自常量池

System.out.println(s1==s2);   //true

 

String s3 = new String("hello");  //new出新的字符串

String s4 = new String("hello");  //new出新的字符串

System.out.println(s3==s4);       //false

 

String s5 = "hello";                //取自常量池

String s6 = new String("hello");  //new出新的字符串

System.out.println(s5==s6);       //false

 

扩展:直接定义方式和new方式的区别?

String s7 = "hello";                //取自常量池

String s8 = new String("hello");  //new出新的字符串

System.out.println(s7==s8);    //false

System.out.println(s7.equals(s8)); //true,因为String类复写了object类中equals方法,该方法、、//用于判断字符串是否相同。

s7s8的区别:

s7代表一个对象

s8代表两个对象

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值