MIT 6.031 Reading 1: Static Checking

申屠健
2023-12-01

MIT 6.031

Reading 1: Static Checking

Types

  • primitive types:
    • int
    • long
    • boolean
    • double
    • char
  • object types:
    • String
    • BigInteger

Static Typing

Java is a statically-typed language

static checking

Support for static typing in dynamically-typed languages

  • after Python 3.5, it allows you to declare type hints
# Python function declared with type hints
def hello(name: str)->str:
	return 'Hi, ' + n

Static checking, dynamic checking, no checking

  • static checking: the bug is found automatically before the program even runs
    • syntax errors
    • wrong number of arguments: Math.sin(30, 20)
    • wrong argument types: Math.sin("30")
    • wrong return types
  • dynamica checking: the bug is found automatically when the code is executed
    • illegal argument values: divided-by-zero is a dynamic error
    • illegal conversions
    • out-of-range indices
    • calling a method on a null object reference
  • no checking: the language does not help you find the error at all.

Static checking can detect errors related to type of a variables. Static checking guarantees that a variable will have some value from its type, but we don’t know until runtime exactly which value it has. So if the error would be caused by certain values, like divided-by-zero or index-out-of-range, then the compiler won’t raise a static error about it

Surprise: primitive types are not true numbers

  • integer division: int x = 5 / 2; or double y = 5 / 2 will not return a fraction, it returns a truncated integer 2 and 2.0 => wrong answers, no errors
  • integer overflow: the computation quietly overflows, and returns an integer from somewhere in the legal range but not right answers
  • special values in floating-point types: when you apply certain operations to a double that you’d expect to produce dynamic errors, like dividing by zero or taking square root of a negative number, you will get one of these special values instead -> bad final answers

Exercises

int n = - 5;
if (n) {
   System.out.println("n is a positive number");
}

answer: static error, because if statement requires an expression of boolean type, but n has int type. Changing the code to if (n > 0) ... would fix it
2.

int big = 200000;
big = big * big; // 40 bn now

answer: no error, wrong answer, this is an integer overflow, because an int value can’t represent a number bigger than 2^31 (2 bn), it isn’t caught statically and caught dynamically, it overflows quietly produce the wrong answer
3.

double x = 5.0 / 2;  // x = 2.5
double y = 5 / 2; // y = 2.0
int sum = 0;
int n = 0;
int avg = sum / n;

answer: dynamic error, divided-by-zero

double sum = 7;
double n = 0;
double avg = sum / n;

answer: no error, but wrong answer, division by zero can’t produce a real number either, but double has a special value for POSITIVE_INFINITY, so that’s what it returns when you divide a positive integer by zero

sqrt() can handle numbers with fractional parts

Arrays and Collections

Arrays

arrays are fixed-length sequences of types
int[] a = new int[100]

  • indexing: a[2]
  • assignment: a[2] = 0;
  • length: a.length
Buffer overflow

Lists

lists are variable-length sequences of another type
List< Integer> list = new ArrayList< Integer>();

  • indexing: list.get(2)
  • assignment: list.set(2, 0)
  • length: list.size()

List is an interface, a type that can’t be constructed directly, but that instead just specifies the operations that a List must provide.
ArrayList is a class, a concrete type that provides implementations of those oeprations

Mutating Values vs Reassigning Variables

  • the string type is immutable in both Python and Java
  • immutable references: final int n = 5;
    • if Java compiler isn’t convinced that your final variable will only be assigned once at runtime, then it will produce a compiler error => final gives you static checking for unreassignable references
    • we can make a List< Integer> list a final

Documenting assumptions

Programs have to be written with two goals in mind:

  • communicating with the computer
  • communicating with other people

Hacking vs. Engineering

 类似资料:

相关阅读

相关文章

相关问答