3.1Data Type and Type Checking (补充)

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值