partychicken
Articles12
Tags3
Categories3
数据类型与类型检验

数据类型与类型检验

4 数据类型与类型检验

Data type in programming languages

Type and Variables

A type is a set of values, along with operations that can be performed on those values.

Variables: Named location that stores a value of one particular type.

Types in Java

Primitive types 基本数据类型: int, long, boolean

Object types 对象数据类型:

  • String represents a sequence of characters.

  • BigInteger represents an integer of arbitrary size

image-20220608171925801

对象类型形成层次结构

image-20220608175627059

If extends clause omitted, defaults to Object.

A class is an instance of all its superclasses.

  • Inherits visible fields and methods from its superclasses
  • Can override methods to change their behavior

Static vs. dynamic data type checking

类型转换

image-20220608175814876

Static Typing vs. Dynamic Typing

Java is a statically-typed language. 静态类型语言

  • 每个变量的类型在编译时确定
  • 同类型变量运算结果默认为该类型
  • 在编译阶段进行类型检查

In dynamically-typed languages like Python, this kind of checking is deferred until runtime (while the program is running). 动态类型语言

  • 在运行阶段进行类型检查

静态类型检查:可在编译阶段发现错误,避免了将错误带入到运行阶段,可提高程序正确性/健壮性

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

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

Mutability and Immutability

赋值

Use “=” to give variables a value

Changing a variable or its value

改变一个变量:将该变量指向另一个值的存储空间

改变一个变量的值:将该变量当前指向的值的存储空间中写入一个新的值。

尽可能避免变化,以避免副作用。

不变性:重要设计原则

不变数据类型:一旦被创建,其值不能改变

如果是引用类型,也可以是不变的:一旦确定其指向的对象,不能再被改变

使用关键字 final 修饰

如果编译器无法确定final变量不会改变,就提示错误,这也是静态类型检查的一部分。

尽量使用 final 变量作为方法的输入参数、作为局部变量。

  • final类无法派生子类
  • 变量无法改变值/引用
  • final方法无法被子类重写

String as an immutable type

  • String is an example of an immutable type.

  • A String object always represents the same string.

  • Since String is immutable, once created, a String object always has the same value.
  • To add something to the end of a String, you have to create a new String object:

StringBuilder as a mutable type

  • StringBuilder is an example of a mutable type.
  • It has methods to delete parts of the string, insert or replace characters, etc.
  • This class has methods that change the value of the object, rather than just returning new values

Advantages of mutable types

使用不可变类型,对其频繁修改会产生大量的临时拷贝(需要垃圾回收),可变类型最少化拷贝以提高效率。

使用可变数据类型,可获得更好的性能,也适合于在多个模块之间共享数据。

可以通过防御式拷贝,给客户端返回一个新的Date对象,防止对mutable变量的修改。如果使用不可变类型,则节省了频繁复制的代价。

安全的使用可变类型:局部变量,不会涉及共享;只有一个引用。

如果有多个引用(别名),使用可变类型就非常不安全。

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

Snapshot diagrams 用于描述程序运行时的内部状态,便于程序员之间的交流,便于刻画各类变量随时间变化,便于解释设计思路。

基本类型的值:image-20220608183053433

对象类型的值:image-20220608183109996

不可变对象:用双线椭圆

image-20220608183212333

image-20220608183242532

不可变的引用:用双线箭头 image-20220608183351932

Complex data types: Arrays and Collections

Array

Arrays are fixed-length sequences of another type T.

The int[] array type includes all possible array values, but a particular array value, once created, can never change its length.

List

Lists are variable-length sequences of another type T .

List is an interface.

Members in a List must be an object.

image-20220608202212446

Set

A Set is an unordered collection of zero or more unique objects.

Set is an abstract interface

image-20220608202253600

Map

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

Map is an abstract interface

image-20220608202326488

Iterator

image-20220608202446562

Useful immutable types

基本类型及其封装对象类型都是不可变的。

Don’t use mutable Date, use the appropriate immutable type from java.time based on the granularity of timekeeping you need.

The Collections utility class has methods for obtaining unmodifiable views of these mutable collections:

  • Collections.unmodifiableList
  • Collections.unmodifiableSet
  • Collections.unmodifiableMap

这种包装器得到的结果是不可变的:只能看。但是这种“不可变”是在运行阶段获得的,编译阶段无法据此进行静态检查

Author:partychicken
Link:http://partychicken.github.io/2022/06/03/4%20%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%B8%8E%E7%B1%BB%E5%9E%8B%E6%A3%80%E9%AA%8C/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×