Abstract


  • Type safety means ensuring that operations are only performed on variables of compatible Datatype, preventing errors and ensuring correct results

Statically Typed

  • Datatype checking is performed at compile time

Important

The datatype must be known during compilation, either by explicitly defining the data type or by assigning a value to the variable, which allows the language to infer the type.

Use type inference only when the assigned value clearly shows its type like a := "thisIsStr". Otherwise, explicitly declare the type (e.g., String a := foo()). This ensures code readability.

More stable codes & a more informative coding experience

Errors related to type mismatches can be caught before the program runs, offering early detection of potential problems.

Compiler also has more information to do more checks on the codes and enforce certain standards. Plus better code completion when coding. Refer to this video for more more details and example.

Dynamically Typed

  • Datatype checking is performed when we are running the program

Faster coding experience

We don’t need to think about what datatype each variable has, we can better focus on implementing the logic.

More runtime errors

We may run operation on variables that have incompatible datatypes during runtime, this can be avoided if the languages is Statically Typed.

Strongly Typed

  • Strongly typed means the Datatype checking is strict
  • For example, we can’t add int variable with string variable

Weakly Typed

  • Weakly typed means the Datatype checking ISN’T strict
  • For example, we can add int variable with string variable in Javascript

Type Systems and Safety: Static, Strong


Statically TypedStrongly TypedMeaningLanguage
Datatype checking is performed at runtime, with loose rulesJavascript, PHP
Datatype checking is performed at runtime, with strict rulesPython
Datatype checking is performed at compile time, with loose rulesC
Datatype checking is performed at compile time, with strict rulesJava
  • Javascript allows the addition between integer and string
  • Python prevents the addition between integer and string. However, the datatype checking only occurs when the code is executed. Change False to True on line 5 to observe the datatype checking process
  • C allows implicit conversions between numeric types and has a void pointer type that can point to any data type. b contains the Memory Address which is used in the addition with a
  • Java prevents the addition between integer and string, even if the code with type error isn’t going to get executed