What is the difference between statically typed and dynamically typed languages?

Viewed 561603

What does it mean when we say a language is dynamically typed versus statically typed?

18 Answers

Statically typed languages: each variable and expression is already known at compile time.

(int a; a can take only integer type values at runtime)

Examples: C, C++, Java

Dynamically typed languages: variables can receive different values at runtime and their type is defined at run time.

(var a; a can take any kind of values at runtime)

Examples: Ruby, Python.

Static typed languages (compiler resolves method calls and compile references):

  • usually better performance
  • faster compile error feedback
  • better IDE support
  • not suited for working with undefined data formats
  • harder to start a development when model is not defined when
  • longer compilation time
  • in many cases requires to write more code

Dynamic typed languages (decisions taken in running program):

  • lower performance
  • faster development
  • some bugs might be detected only later in run-time
  • good for undefined data formats (meta programming)

Static Type: Type checking performed at compile time.

What actually mean by static type language:

  • type of a variable must be specified
  • a variable can reference only a particular type of object*
  • type check for the value will be performed at the compile time and any type checking will be reported at that time
  • memory will be allocated at compile time to store the value of that particular type

Example of static type language are C, C++, Java.

Dynamic Type: Type checking performed at runtime.

What actually mean by dynamic type language:

  • no need to specify type of the variable
  • same variable can reference to different type of objects

Python, Ruby are examples of dynamic type language.


* Some objects can be assigned to different type of variables by typecasting it (a very common practice in languages like C and C++)

Statically typed languages like C++, Java and Dynamically typed languages like Python differ only in terms of the execution of the type of the variable. Statically typed languages have static data type for the variable, here the data type is checked during compiling so debugging is much simpler...whereas Dynamically typed languages don't do the same, the data type is checked which executing the program and hence the debugging is bit difficult.

Moreover they have a very small difference and can be related with strongly typed and weakly typed languages. A strongly typed language doesn't allow you to use one type as another eg. C and C++ ...whereas weakly typed languages allow eg.python

Statically Typed

The types are checked before run-time so mistakes can be caught earlier.

Examples = c++

Dynamically Typed

The types are checked during execution.

Examples = Python

Dynamically typed programming that allows the program to change the type of the variable at runtime.

dynamic typing languages : Perl, Ruby, Python, PHP, JavaScript, Erlang enter image description here

Statically typed, means if you try to store a string in an integer variable, it would not accept it.

Statically typed languages :C, C++, Java, Rust, Go, Scala, Dart enter image description here

Related