Type checking in programming languages

What is type checking?

Type checking is done either by the compiler (static type checking) or during the program execution (dynamic type checking). It ensures that the operands of an operator are of compatible types. Assume that you want to add an int and a float in Java. Those are obviously different types, but it's allowed because the int variable is coerced to a float, and a floating-point add is done. Coercion is just the automatic process when a variable is implicitly converted to a legal type by compiler-generated code, or the interpreter. Compatible types are types that are legal for an operator or is allowed to be coerced.

Static type checking

In a programming language where all of the bindings of variables to types are static, as in C, C++ or JAVA, then the type checking can almost always be done statically.

Dynamic type checking

In programming languages where the bindings are dynamic, as in JavaScript or PHP, the type checking is required to be done at run time.

The advantage of static type checking over dynamic type checking is that errors can be detected earlier, which is less costly. It also allows more effective program execution, i.e faster and/or reduced memory use. The disadvantage is the reduced programmer flexibility.

Author

authors profile photo

Articles with similar tags

thumbnail
Parameter Passing Methods in Programming Languages

Read about four different ways of Parameter Passing Methods

By Frogitecture