Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I hear a lot that new programming languages are dynamically typed but what does it actually mean when we say a language is dynamically typed vs. statically typed?

like image 305
Rachel Avatar asked Oct 04 '09 22:10

Rachel


People also ask

What is statically-typed and dynamically typed languages give examples?

Statically typed programming languages do type checking at compile-time. Examples: Java, C, C++. Dynamically typed programming languages do type checking at run-time. Examples: Perl, Ruby, Python, PHP, JavaScript.

What is a statically-typed language?

A statically-typed language is a language (such as Java, C, or C++) where variable types are known at compile time. In most of these languages, types must be expressly indicated by the programmer; in other cases (such as OCaml), type inference allows the programmer to not indicate their variable types.

What is a dynamic typed language?

Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable's value at the time.

Is C++ statically or dynamically typed?

C++ is a statically-typed language. The static nature of the C++ type system provides a data integrity 'safety net'. The compiler is an indispensable runtime-surprise-prevention tool and the static nature of C++ provides runtime performance gain.


2 Answers

Statically typed programming languages do type checking (i.e. the process of verifying and enforcing the constraints of types) at compile-time as opposed to run-time.

Dynamically typed programming languages do type checking at run-time as opposed to compile-time.

Examples of statically typed languages are: Java, C, C++

Examples of dynamically typed languages are: Perl, Ruby, Python, PHP, JavaScript

like image 42
Christopher Tokar Avatar answered Dec 22 '22 01:12

Christopher Tokar


Statically typed languages

A language is statically typed if the type of a variable is known at compile time. For some languages this means that you as the programmer must specify what type each variable is; other languages (e.g.: Java, C, C++) offer some form of type inference, the capability of the type system to deduce the type of a variable (e.g.: OCaml, Haskell, Scala, Kotlin).

The main advantage here is that all kinds of checking can be done by the compiler, and therefore a lot of trivial bugs are caught at a very early stage.

Examples: C, C++, Java, Rust, Go, Scala

Dynamically typed languages

A language is dynamically typed if the type is associated with run-time values, and not named variables/fields/etc. This means that you as a programmer can write a little quicker because you do not have to specify types every time (unless using a statically-typed language with type inference).

Examples: Perl, Ruby, Python, PHP, JavaScript, Erlang

Most scripting languages have this feature as there is no compiler to do static type-checking anyway, but you may find yourself searching for a bug that is due to the interpreter misinterpreting the type of a variable. Luckily, scripts tend to be small so bugs have not so many places to hide.

Most dynamically typed languages do allow you to provide type information, but do not require it. One language that is currently being developed, Rascal, takes a hybrid approach allowing dynamic typing within functions but enforcing static typing for the function signature.

like image 66
NomeN Avatar answered Dec 22 '22 02:12

NomeN