4 Data Type and Type Checking(数据类型与类型检验)

1 Data type in programming languages

2 Static vs. dynamic data type checking

静态类型语言: 在编译阶段进行类型检查

动态类型 语言:在运行阶段进行类型检查

静态类型检查 >> 动态 >> 无检查

Static checking

▪ Syntax errors 语法错误(动态类型检查的语言也会进行静态检查(除了类型外的其他 语法错误))

▪ Wrong names 类名/函数名错误

▪ Wrong number of arguments 参数数目错误

▪ Wrong argument types 参数类型错误

▪ Wrong return types 返回值类型错误(类型不匹配)

Dynamic checking

▪ Illegal argument values 非法的参数值(如x/0,除数不能为0)

▪ Unrepresentable return values 非法的返回值(不该有的的类型)

▪ Out-of-range indexes 越界

▪ Calling a method on a null object reference. 空指针

总结:

静态检查:关于“类型”的检查,不考虑值

动态检查:关于“值”的检查

3 Mutability and Immutability

使用final使引用不可变,之后会对此进行静态类型检查。

注:final类无法派生子类

        变量无法改变值引用

        方法无法被子类重写

String as an immutable type

 StringBuilder as a mutable type、

差异性

 可变类型的风险

不可变类型更安全,修改后占用新空间

可变类型Date用LocalDataTime代替

解决可变类型问题方法

1.防御式拷贝,给客户端返回新的可变类型的变量(Data),但可能会造成内存浪费(大部分时候不会被客户端修改)

使用不可变类型则无需使用防御式拷贝

2.使用局部变量,避免共享。

4 Snapshot diagram as a code-level, run-time, and moment view

不可变类型或不可变的引用,用双线

引用是不可变的,但指向的值是可以改变的

可变的引用,也可指向不可变的值

5 Complex data types: Arrays and Collections

Arrays are fixed-length sequences of another type T.  定长数组不可改变长度

▪ Operations on array types include:

        – indexing: a[2]

        – assignment: a[2]=0

        – length: a.length

Lists are variable-length sequences of another type T .

        List list = new ArrayList();

▪ Some of its operations:

        – indexing: list.get(2)

        – assignment: list.set(2, 0)

        – length: list.size()

▪ Note 1: List is an interface. ▪ Note 2: members in a List must be an object.

Iterating

▪ Iterating an array

int max = 0;

for (int i=0; i<array.length; i++) {

        max = Math.max(array[i], max);

}

▪ Iterating a List

int max = 0;

for (int x : list) {

        max = Math.max(x, max);

}

Set

– s1.contains(e) 测试集合是否包含元素
– s1.containsAll(s2)测试是否s1⊇ s2级
– s1.removeAll(s2) 从s1移除s2

Map

A Map is similar to a dictionary (key-value)

– map.put(key, val) add the mapping key → val

– map.get(key) get the value for a key

– map.containsKey(key) test whether the map has a key

– map.remove(key) delete a mapping

当向List, Set, and Map variables添加一个item时 ,编译器执行静态检查,确保只添加合适类型的item 

 Creating List, Set, and Map variables

 Iteration

▪ An iterator has two methods:

        – next() returns the next element in the collection --- this is a mutator method!

        – hasNext() tests whether the iterator has reached the end of the collection.

 

 Collections实用程序类具有用于获取这些可变集合的不可修改视图的方法:

        – Collections.unmodifiableList

        – Collections.unmodifiableSet

        – Collections.unmodifiableMap

注:这种“不可变”是在运行阶段获得的,编译阶段无法据此进行静态检查

 Unmodifiable Wrappers

▪ public static Collection unmodifiableCollection(Collection c);

▪ public static Set unmodifiableSet(Set s);

▪ public static List unmodifiableList(List list);

▪ public static Map unmodifiableMap(Map m);

▪ public static SortedSet unmodifiableSortedSet(SortedSet s);

▪ public static SortedMap unmodifiableSortedMap(SortedMap m);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值