What is the difference between static and dynamic programming languages? I know that it is all about type systems, but I’m looking for more clear clarifications.
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.
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.
A static language is a language that works like a dynamic language but with less effort, and this effort is writing code.
In a static language, we have to write less code compare to a dynamic language.
The main point is:
- In a static language, we can write and use variables without declaring them:
# Example in Python
i = 12
print(i)
- In a dynamic language, if we have to use variable, we have to declare it:
// Example in C
int i;
int i = 21;
printf(i);
var i = 21; i = 'X'
–
Lisabeth All languages are designed to translate human-readable code into machine instructions. A dynamic language (Lisp, Perl, Python, Ruby) is designed to optimize programmer efficiency, so you can implement functionality with less code. A static language (C, C++, etc) is designed to optimize hardware efficiency, so that the code you write executes as quickly as possible.
If any Programming Language allows memory allocation is done at Compilation Time then that Programming Language is called as STATIC Programming Language. Examples: C,C++...etc.
If any Programming Language allows memory allocation is done at Run Time then that Programming Language is called as DYNAMIC Programming Language. Examples: Java, Python...etc.
STRUCTURE
Static variables have an immutable type, decided beforehand. They can only be operated after conversion.
int number = 1
string name = "joe"
string output = string(number) + name // = 1joe
Dynamic variables have their type decided automatically. And they can be operated anytime, as they are converted as needed:
number = 1
name = "joe"
output = number + name // = 1joe
ADVANTAGES
Programming with dynamic variables is way easier, faster and cleaner.
Yet all the type guessing takes CPU power, and tends to be orders of magnitude slower.
Hence the choice between the two depends on how CPU intensive your appliance is.
© 2022 - 2024 — McMap. All rights reserved.