Encyclopedia > Static typing

  Article Content

Static typing

In programming languages, static typing is the practice of assigning types to sets of values based on a static analysis[?] of the program's source code. Static type systems usually assign a single type to each syntactic program entity (e.g., each bound variable name or expression). This is in contrast to dynamically typed systems, which do not require that syntactic entities be consistently typed.

Consider the following pseudocode example:

 var x;    // (1)
 x = 5;    // (2)
 x = "hi"; // (3) 

In this example, (1) declares the name x; (2) binds the integer value 5 to the name x; and (3) binds the string value "hi" to the name x. A typical static type discipline would require that the name x be assigned a single type, and hence that all values bound to x be of the same type. In such a system, the above code fragment would be illegal, because (2) and (3) bind x to values of inconsistent type (in most type systems, no value can be both an integer and a string). By contrast, a purely dynamically typed system would permit the above program to execute, because the name x would not be required to have a consistent type.

Static type disciplines operate on program source code rather than on the program execution. Therefore, they are able to detect certain kinds of errors without executing the program---this "early detection" of errors is one of the key software engineering benefits of statically typed systems.

The theoretical underpinnings of static typing are given by type theory, an interdisciplinary pursuit that lies at the intersection of mathematical logic and computer science. See type theory for further discussion.

Widely known programming languages with static typing include the following:

Note that some statically typed languages, notably C and its derivatives, have a "back door" in the language that enables programmers to write type-incorrect code by deliberately circumventing the static type system. Languages with back doors are called weakly typed; languages without back doors are called strongly typed.

Also, note that the presence of static typing in a programming language does not necessarily imply the absence of dynamic typing mechanisms. For example, Java is statically typed, but certain operations require the support of runtime type tests, which are a form of dynamic typing. See programming language for more discussion of the interactions between static and dynamic typing.

All Wikipedia text is available under the terms of the GNU Free Documentation License

  Search Encyclopedia

Search over one million articles, find something about almost anything!
  Featured Article
Wheatley Heights, New York

... every 100 females there are 95.1 males. For every 100 females age 18 and over, there are 89.3 males. The median income for a household in the town is $75,076, and th ...

This page was created in 24 ms