Difference between static and dynamic programming languages [duplicate]
Asked Answered
S

6

63

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.

Shouse answered 13/12, 2013 at 9:50 Comment(2)
see alsoFrigid
Can you change your question to "Difference between statically and dynamically typed programming languages?". There is actually a definition of static and dynamic programming languages that has nothing to do with type checking.Gird
B
89

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.

Blameful answered 11/12, 2015 at 19:48 Comment(0)
R
6

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.

Raynard answered 17/11, 2018 at 17:14 Comment(0)
R
-3
  • 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);
Resistless answered 1/12, 2020 at 6:29 Comment(2)
What about JavaScript? There's var i = 21; i = 'X'Lisabeth
Isn't it the other way around?Jakejakes
A
-4

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.

Acapulco answered 29/5, 2018 at 3:14 Comment(0)
B
-4

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.

Boardman answered 22/5, 2019 at 12:57 Comment(1)
This definition is incorrect: C and C++ also allow memory allocation at runtime using malloc.Builtup
J
-4

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.

Jakejakes answered 13/10, 2021 at 22:36 Comment(2)
The are several problems. 1) The OP's original question conflates "static vs. dynamic programming languages" and "static vs. dynamic typing": two completely different things. 2) There are many reasons for "static typing", including protection against runtime errors ("reliability"). "Performance" isn't necessarily the most important factor, and "extra CPU utilization" is a negligible aspect of "performance". Please consider deleting this reply.Sump
I never found the error detection mechanism to provide substantial benefit when the code is clear, short and tested. Plus those potential benefits can be rather attributed to the code being compiled instead of interpreted. On the other hand static types allow to better optimize compilation and data structures, simply because types are more predictable than them being able to be anything and any size in memory. The simpler case making it easier for the code to be compiled.Jakejakes

© 2022 - 2024 — McMap. All rights reserved.