Java看书笔记
程序员文章站
2022-05-10 14:29:43
...
这一篇专用于一些日常的Java读书笔记
先写一点关于String的,
- String对象直接赋值比较好,因为Java里面有一个String常量池的玩意,可以节省内存空间
- String对象是immutable的,不能被修改
- String.subString(0) 其实会返回自己(==判断为true,对象地址为同一个)
- String.intern() 在某些情况下会返回一样的常量池对象(当String为常量池对象的时候)
public static void main(String[] args) { String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); String s4 = new String(s1); String s5 = s1.intern(); String s6 = s3.substring(0); String s7 = new String("d"); String s8 = s7.intern(); System.out.println("s1 == s2 " + (s1 == s2)); System.out.println("s1 == s3 " + (s1 == s3)); System.out.println("s1 == s4 " + (s1 == s4)); System.out.println("s1 == s5 " + (s1 == s5)); System.out.println("s3 == s6 " + (s3 == s6)); System.out.println("s7 == s8 " + (s7 == s8)); }
结果:
写道
s1 == s2 true
s1 == s3 false
s1 == s4 false
s1 == s5 true
s3 == s6 true
s7 == s8 false
s1 == s3 false
s1 == s4 false
s1 == s5 true
s3 == s6 true
s7 == s8 false