Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static and dynamic programming languages

What is the different between static and dynamic programming languages? I know that it is all about type systems but I’m looking for more clear clarifications.

like image 418
Balaji Reddy Avatar asked Dec 13 '13 09:12

Balaji Reddy


People also ask

What are static programming languages?

Statically Typed Languages This means that before source code is compiled, the type associated with each and every single variable must be known. Some common examples of programming languages that belong to this category are Java, Haskell, C, C++, C#, Scala, Kotlin, Fortran, Go, Pascal, and Swift.

What does static and dynamic mean in programming?

In computer terminology, dynamic usually means capable of action and/or change, while static means fixed.

What is the difference between static and dynamic in Python?

Python is a dynamically typed language which means checking of the variable is done at the runtime. Whereas in the Statically typed language the checking of the variables or any other is done at the compile time.

Is Java a static or dynamic language?

Some examples of statically-typed languages are Java, C, C++, C#, Swift, Scala, Kotlin, Fortran, Pascal, Rust, Go, COBOL, etc.


2 Answers

Static Typing

Static typing means that types are known and checked for correctness before running your program. This is often done by the language's compiler. For example, the following Java method would cause a compile-error, before you run your program:

public void foo() {     int x = 5;     boolean b = x; } 

Dynamic Typing

Dynamic typing means that types are only known as your program is running. For example, the following Python (3, if it matters) script can be run without problems:

def erroneous():     s = 'cat' - 1  print('hi!') 

It will indeed output hi!. But if we call erroneous:

def erroneous():     s = 'cat' - 1  erroneous() print('hi!') 

A TypeError will be raised at run-time when erroneous is called.

like image 166
sleeparrow Avatar answered Sep 22 '22 08:09

sleeparrow


Difference between static and dynamic is that before running the program if the data type of each variable is checked and verified then it's static type programming language (e.g:- in case of C++ it's done by the compiler). In Dynamic programming language during runtime, if there is an invalid assignment of a variable that violates its data type then an error is given for that.

Summary- Static type language check any violation before running the program whereas in the dynamic type language the error is given when the program is running and goes to the part were violation has been done.

like image 21
Nishant Dwivedi Avatar answered Sep 24 '22 08:09

Nishant Dwivedi