A type specifies a set of values that have certain similarities. With any type there is an associated set of operations that could (or should) be applied to values of that type.
One of the main differences between high-level and low-level programming languages is a low level language with no type system: such language has only bytes and words.
The type system of a programming language has the following features :
Safety – On the basis of types, a compiler (or interpreter) identifies certain faults (called type errors). The extent to which the compiler-based typing errors can be detected depends on the type of system: strong typing offers more security than weak typing.
Optimization – If the types of expressions are already known at compile time (static typing), the compiler can often apply certain optimizations.
Documentation – When the types of used variables and function parameters are specified in a program they offer the reader of the source code clues about the nature and use of these variables and parameters.
When a compiler or interpreter compiles a program, it checks whether each operation type is correct. This is called type checking.
If the compiler or interpreter encounters an operation that is not type correctly, such as multiplying a string containing a boolean, the execution is aborted and an error message is printed.
If a programming language is statically typed, type checking occurs at compile time, and not performing during (run-time) of the program.
If the compiler finds an error, the compilation is aborted and the programmer will be informed of the problem. Examples of statically typed languages are C, Java, Scala and Haskell.
Static typing has a few advantages:
Because all types of checks have been performed at compile time, a compiled program is always type – correct. During the execution of the program thus have to find more.
Although it takes a little longer to compile, the resulting program will be faster (and smaller).
In static typing, errors are discovered by the compiler during the development of the program. So static typing provides a kind of basic automatic program verification.
The compiler has more opportunities for optimization.
Not all statically typed languages require the programmer to specify what type they have.
For languages in which this is not the case (such as Haskell and OCaml), the compiler determines the types of variables and parameters (in general: all expressions). This is called type inference.
A programming language is dynamically typed named as type checking takes place during the execution of the program, rather than at compile time. This is also called late – binding.
Unlike static typing, dynamic typing gives the programmer more freedom. The result is that programs can be written quickly, and (often) are shorter.
Examples of dynamically typed languages: Perl , PHP and Lisp. Errors, however, would manifest themselves during the actual execution of the program.
Some statically typed languages offer the possibility to make use of dynamic typing by means of a mechanism that is called casting . Use When casting is an expression at run-time converted to a different type.
In this case no type check takes place during compilation. Casting can lead to unexpected errors during execution, and therefore called unsafe. Most statically typed languages offer some form of casting.
There are programming languages that offer the ability to turn off. Static typing for parts of the program Examples are Ada and haXe.