key word :
- data type
- mutability and mutable objects
- static and dynamic type checking
- Null references
Specific content as follows:
1 、Primitives&Object Reference Types P6
2、Boxed primitives(基本数据类型的包装类)
don’t use
包装类
Integer.valueOf()方法基于减少对象创建次数和节省内存的考虑,缓存了[-128,127]之间的数字。此数字范围内传参则直接返回缓存中的对象
解释valueOf缓存,必看
Integer i01 = 2;
int i02 =2;
Integer i03 = Integer.valueOf(2);
Integer i04 = new Integer (2);
System.out.println(i01==i03);//true i01的转换过程就是i03,所以不建议使用 基本数据包装类
System.out.println(i01==i02);//true
//等号一边存在基本类型所以编译器后会把另一边的Integer对象拆箱成int型
System.out.println(i03==i04);//false i03返回的是已缓存的对象的引用
System.out.println(i04==i02);//true 拆箱
上边代码的解释
上边文章中 补充对象引用
3、Mutability and Immutability P22
Mutability 修修补补
Immutability 另起炉灶 双圈 大量重复(for)
Risky example #1 P29
修复措施:复制性保护 P34
this.start=new Date(start.getTime());
this.end=new Date(end.getTime());
end.setyear(78)//设置年分成78年
4、Arrays and Collections
Array List Map
- 列表内容
We cannot create a collection of primitive types - Declaration
List<String> cities; // a List of Strings
Set<Integer> numbers; // a Set of Integers
Map<String, Turtle> turtles;
5、Mutation undermines an iterator P54
迭代器,删除元素的时候,删除完,下一个元素顶上去被删除的位置,接着就直接删除下一个,导致,顶包的那个得不到删除
Q:疑惑
6、Useful immutable types
基本类型都是不可变的
List, Set,Map — are all mutable: ArrayList, HashMap, etc.
但是可以使用 Collections.unmodifiableList
7、Null references
- Primitives cannot be null