Jan 18, 2011

The Type System of a Language

In this article, we are going to discuss how a programming language determines the Type system and categorize them accordingly:

Strongly/Weakly Typed languages:
Strongly typed programming language is the one that has put some restrictions for operations involving hybrid data types like int and string addition.
On the other hand, Weakly typed language is the onen which will not give any error if hybrid operands (operands having different data types) are used on an operator and will try to perform the operation by its interpretation. (which may be different for various languages)
Examples for a hypothetical method append:
append(10.12,"hi"); will result in error in strongly typed language (which again can be compiler error or run time exception) e.g. Java (Implicit or Explicit cast may be required to make this function work which is the reason for Java being partially Strong typed)
append(10.12,"hi"); will not result in any kind of error in a weakly typed language e.g. JavaScript




Statically vs Dynamically typed languages:
A static type means data type of variables is determined at compile time e.g. Java
A weak type means type of variables is determined at run time e.g. JavaScript


We have following examples for various cases:
  • Strongly and Statically typed: Java
  • Weakly and Dynamically typed: JavaScript
  • Strongly and Dynamically typed: Python
  • Weakly and Statically typed: It doesn't make sense to have a language which does determines the types of variables at compile time but doesn't make use of them in various operations. Right now, I am not aware of any language of this type.

The following statement is false:

A Strongly/Weakly typed language can not be statically/dynamically typed language and vice versa.

Never mix weakly/strongly, statically/dynamically, manifestly/implicitly typed languages with each other as any programming language can be a mix of these types.

A myth that needs to be removed is that static type means type of variable has to be specified while declaring it.
A static type means that the type is determined at compile time and remains the same until the variable is not destroyed.
But if the variable type must be specified while declaring the variable means that language is known as manifestly typed language.
The opposite of manifestly typed language is implicitly typed language where it is not required to have any data type for variables.

Some related links on this topic are:
http://www.coderanch.com/t/522430/java/java/Strongly-typed-Weakly-typed-languages
Strong Typing
Type System
Java and Python

5 comments:

  1. Thanks for sharing information dude. nice article .

    Javin
    deadlock in java

    ReplyDelete
  2. The definitive guide is "What to know before debating type systems": http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

    ReplyDelete
  3. Yes Hamlet, your link and the links to wikipedia are also good source about this one of the most mis-understood topic.

    ReplyDelete
  4. I think C is static and weak.

    ReplyDelete
  5. No language is absolute static typed or weak typed. We can only compare the languages to be more or less typed with each other.

    ReplyDelete