Data Type and Type Checking

Data Type and Type Checking

Java中的数据类型

  • 基本数据类型,小写:byte short int long float double char boolean
  • 对象数据类型,开头字母大写:String BigInteger
  • Java中以上两种类型的区别:
PrimitivesObject reference types
只有值,没有ID既有ID,也有值
不可变有些可变,有些不可变
在栈中分配内存在堆中分配内存
代价低代价昂贵
  • 包装类型
    • 将基本类型包装为对象类型
    • Boolean, Integer, Short, Long, Character, Float, Double
    • 通常是在定义集合类型的时候使用它们,一般情况下,尽量避免使用
    • Language does autoboxing and auto-unboxing
  • java不支持运算符重载,只是在两个引用+时调用toString()

静态/动态类型检查

// 没有错误
int a = 2; // a = 2
double a = 2; // a = 2.0 (Implicit)
int a = (int) 18.7; // a = 18
double a = (double)2/3; // a = 0.6666…
double x = 0;
double y = x/x; //NaN
double z = 2/x; //Infinity

// 静态检查
int a = 18.7; // ERROR
String a = 1; // ERROR
double a = 2/3; // a = 0.0 
String aString = (String)5; // ERROR Cannot cast from int to String
// java 向上自动类型转换,向下需要强制转换,否则编译后会报错

// 动态检查
double a = 0/0;//Exception in thread "main" java.lang.ArithmeticException: / by zero
int x = 0/0;   //Exception in thread "main" java.lang.ArithmeticException: / by zero
  • Java is a statically-typed language. 静态类型语言
    • 在编译阶段进行类型检查,所有变量的类型在编译阶段被得知(程序运行之前),并且可以推断表达式的类型,e.g.如果a、b是int类型的,那么编译器a+b推断a+b也是int类型的
    • The Eclipse environment does this while you’re writing the code, in fact,
      so you find out about many errors while you’re still typing.
  • Python is a dynamically-typed language
    • this kind of checking is deferred until runtime (while the program is running).
    • 在运行阶段进行类型检查
  • better: 静态类型检查 > 动态 > 无检查
  • 静态类型检查的范围: 语法,名字,参数,返回值
    • Syntax errors 语法错误,(多余的标点符号)
    • Wrong names 类名/函数名错误, like Math.sine(2) . (The right name is sin)
    • Wrong number of arguments 参数数目错误 like Math.sin(30, 20)
    • Wrong argument types 参数类型错误 like Math.sin(“30”)
    • Wrong return types 返回值类型错误 like return “30”; from a function that’s declared to return an int .
    • 局部变量可能没有初始化
    • 局部变量作用错误
  • 动态类型检查的范围:
    • Illegal argument values 非法的参数值. For example, the integer expression x/y is only erroneous when y is actually zero; otherwise it works. So in this expression, divide-by-zero is not a static error, but a dynamic error.
    • Unrepresentable return values 非法的返回值, i.e., when the specific return value can’t be represented in the type.
    • Out-of-range indexes 越界, e.g., using a negative or too-large index on a string.
    • Calling a method on a null object reference. 空指针
Static CheckingDynamic Checking
tends to be about types, errors that are independent of the specific value that a variable hastends to be about errors caused by specific values
关于“类型”的检查,不考虑值关于“值”的检查

(静态类型检查时只知道类型,不知道值,所以类似于x/0的错误不会被编译器静态检查发现)

可变/不变的数据类型

  • Immutable types:一旦被创建,其值不能改变
  • immutable references:
    • 一旦确定其指向的对象,不能再被改变
    • 前面加上 final
    • 如果编译器无法确定final变量不会改变,就提示错误,这也是静态类型检查的一部分。
final类无法派生子类
final变量无法改变值/引用
final方法无法被子类重写
  • String is an example of an immutable type.
    在这里插入图片描述
  • StringBuilder is an example of a mutable type.
    在这里插入图片描述
    所以有什么区别?
    当只有一个引用指向该值,没有区别
    有多个引用的时候,差异就出现了
    在这里插入图片描述
immutablemutable
StringStringBuilder
LocalDateTime, InstantDate

Using immutable strings, this makes a lot of temporary copies 使用
不可变类型,对其频繁修改会产生大量的临时拷贝(需要垃圾回收)
StringBuilder is designed to minimize this copying. 可变类型最少化拷贝以提高效率
可变数据类型的优点:

  • 使用可变数据类型,可获得更好的性能
  • 适合于在多个模块之间共享数据
  • 全局变量
    不可变数据类型的优点:
  • 不可变类型更“安全”,在其他质量指标(理解性,可变性)上表现更好
  • immutable types are safer from bugs, easier to understand, and more ready for change

可变数据类型的危险性 理解别名使用

Safe from bugs? mutate对象后产生的错误非常难于跟踪和发现
Easy to understand? 对自己和其他程序员来说,也难以理解

  • 避免别名使用的危害
    • defensive copy,防御式拷贝
    • 大部分时候该拷贝不会被客户端修改,可能造成大量的内存浪费
    • 如果使用不可变类型,则节省了频繁复制的代价

不变数据类型的优越性 Use immutability to improve correctness, clarity and changeability

用Snapshot图理解数据类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

用集合类表达复杂数据类型

Arrays and Collections

  • Please use List rather than arrays
  • 使用iterator.next .hasNext .remove来遍历集合类

理解空引用的危害并学会避免

防御性拷贝

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值