Java开发

String 不可变

String 源码,String 的修饰符是 final

 

String 采用的是共享模式,被放进常量池

 

String strA = "abc";

String strB = "abc";
System.out.println(strA == strB); true

 

需要说明一点的是,在object中,equals()是用来比较内存地址的,但是String重写了equals()方法,用来比较内容的,即使是不同地址,只要内容一致,也会返回true,这也就是为什么a.equals(c)返回true的原因了

 

参考 https://www.zhihu.com/question/20618891 

posted @ 2017-09-20 09:29  King Bruce  阅读(116)  评论(0编辑  收藏  举报