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

I hear a lot of new things that new programming languages ​​are dynamically typed, but what does it mean when we say that a language is dynamically typed or statically typed?

+875
computer-science programming-languages static-typing dynamic-typing
Oct 04 '09 at 22:36
source share
18 answers

Statically typed languages

The language is statically typed if the type of the variable is known at compile time. For some languages, this means that you, as a programmer, must indicate what type each variable belongs to (for example: Java, C, C ++); other languages ​​offer some form of type inference, the ability of the type system to determine the type of a variable (for example, OCaml, Haskell, Scala, Kotlin)

The main advantage here is that the compiler can perform all kinds of checks, and therefore many trivial errors are detected 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 with named variables / fields / etc. This means that you, as a programmer, can write a little faster, because you do not need to specify types every time (unless you use a statically typed language with type inference).

Examples: Perl, Ruby, Python, PHP, JavaScript

Most scripting languages ​​have this feature, because in any case there is no compiler that would perform static type checking, but you may find an error caused by the interpreter misinterpreting the type of the variable. Fortunately, scripts tend to be small, so there aren't many places to hide errors.

Most dynamically typed languages ​​allow you to provide type information, but do not require it. One language currently under development, Rascal , uses a hybrid approach that allows dynamic typing within functions, but provides static typing for the signature of a function.

+789
04 Oct '09 at 23:11
source share

Statically typed programming languages ​​check the type (i.e. the process of checking and enforcing type restrictions) at compile time, rather than at run time.

Dynamically typed programming languages ​​check type at run time, not compile time.

+356
Oct 05 '09 at 14:12
source share

Here is an example of how Python (dynamically typed) and Go (statically typed) handle a type error:

def silly(a): if a > 0: print 'Hi' else: print 5 + '3' 

Python checks the type at runtime, and therefore:

 silly(2) 

Works great and produces the expected Hi result. The error only occurs when the problem line is hit:

 silly(-1) 

Gives out

 TypeError: unsupported operand type(s) for +: 'int' and 'str' 

because the corresponding line was actually executed.

On the other hand, it checks the type at compile time:

 package main import ("fmt" ) func silly(a int) { if (a > 0) { fmt.Println("Hi") } else { fmt.Println("3" + 5) } } func main() { silly(2) } 

The above will not compile with the following error:

 invalid operation: "3" + 5 (mismatched types string and int) 
+291
Jan 6 '15 at 2:49
source share

To put it simply this way: in types of a statically typed language, variables are static, which means that after you set a type variable, you cannot change it. This is because typing is associated with a variable, not its value.

For example, in Java:

 String str = "Hello"; //variable str statically typed as string str = 5; //would throw an error since str is supposed to be a string only 

On the other hand: in dynamic types of dynamically typed languages, dynamic values, that is, after you set a type variable, you can change it. This is because typing is related to the value that it takes, and not to the variable itself.

For example, in Python:

 str = "Hello" # variable str is linked to a string value str = 5 # now it is linked to an integer value; perfectly OK 

So, it’s best to think of variables in dynamically typed languages ​​as simple pointers to typed values.

To summarize, the type describes (or should have described) variables in the language, not the language itself. It could be better used as a language with statically typed variables and a language with dynamically typed variables . IMHO.

Statically typed languages ​​are usually compiled languages, so compilers check types (they make sense correctly, because types cannot be changed later at run time).

Dynamically typed languages ​​are usually interpreted, so type checking (if any) occurs at runtime when they are used. This, of course, brings some performance and is one of the reasons why dynamic languages ​​(e.g. python, ruby, php) do not scale as well as typed ones (java, C #, etc.). Statically typed languages, on the other hand, have a large initial cost: you usually write more code, more complex code. But it is paid later.

It’s good that both sides borrow functions from the other side. Typed languages ​​include more dynamic functions, such as generics and dynamic libraries in C #, and dynamic languages ​​include more type checking, such as type annotations in python or the PHP HACK option, which are usually not the core of the language and can be used on demand.

When it comes to technology choices, neither side has internal superiority over the other. It is simply a question of whether you need more control to start with or flexibility. just choose the right tool for the job and make sure you check what is available from the perspective of the opposite before looking at the switch.

+145
Nov 30 '15 at 17:28
source share

http://en.wikipedia.org/wiki/Type_system

Static typing

It is said that the programming language static typing when checking type is performed at compile time as against run time. With static typing, types are associated with variables and not values. Statically typed languages ​​include Ada, C, C ++, C #, JADE, Java, Fortran, Haskell, ML, Pascal, Pearl (with respect to the difference between scalars, arrays, hashes, and routines) and Scala. Static typing is a limited form of verification program (see security type): accordingly, it allows many types of errors, which should be the development cycle. Static type checkers evaluate only the type of information that can be determined at compile time, but you can verify that the conditions being checked are met for all possible program execution, which eliminates the need to repeat type checks every time the program is executed. Program execution can also be more efficient (i.e., faster or lower memory) by eliminating runtime type checks and allowing other optimizations.

Because they evaluate type information at compile time, and because the drawback is the type of information that is only available at run time, the static type of checkers is conservative. They will reject some programs that may be well-behaved at runtime, but this cannot be statically determined as well-typed. For example, even if an expression always evaluates to true at run time, a program containing code

 if <complex test> then 42 else <type error> 

will be rejected as unassigned because static analysis cannot determine that the else branch will not be accepted. [1] The conservative behavior of static type controllers is beneficial when it incorrectly evaluates false: A static type of check can detect the type of error in rarely used codes. Without checking the static type, even code coverage tests with 100 percent code coverage may not be able to find such types. A code coverage test may not detect such type errors because a combination of all places where values ​​are created and all places where a specific value is used should be taken into account.

The most widely used statically typed languages ​​are not formally safe. They have “loopholes” in the specification of the programming language allowing programmers to write code that bypasses the checks performed by the static type controller and therefore address a wider range of problems. For example, Java and most C-style languages ​​are punning, and Haskell has features such as unsafePerformIO: such operations can be unsafe at runtime because they can cause unwanted behavior due to incorrect input of values ​​when the program is running.

Dynamic typing

It is said that a programming language is dynamically typed or simply "dynamic", when most of its types are checked, it is executed at run time, as opposed to at compile time. With dynamic typing, types are associated with values, not variables. Dynamically typed languages ​​include Groovy, JavaScript, Lisp, Lua, Objective-C, Perl (relatively custom types, but not built-in types), PHP, Prolog, Python, Ruby, Smalltalk, and Tcl. Compared to static typing, dynamic typing can be more flexible (for example, allowing programs to generate types and functionality from data at run time), although at the expense of fewer a priori guarantees. This is because a dynamically typed language accepts and tries to execute some programs, which may be an invalid static type of verification.

Dynamic typing can lead to runtime of a type, that is, at runtime, the value can have an unexpected type and an operation meaningless for this type is applied. This operation can occur long after the place where the programming error was made - that is, the place where the wrong data type went into the place where it should not have. This makes it difficult to find a mistake.

Dynamically typed language systems, compared to their statically typed cousins, make less "compilation time", checks the source code (but will check, for example, that the program is syntactically correct). At run time, checks can potentially be more complex because they can use dynamic information, as well as any information that was present during the compilation. On the other hand, run-time checks only claim to have conditions in a particular program execution, and these checks are repeated for each program run.

The development of dynamically typed languages ​​is often supported by programming practices such as unit testing. Testing is a key practice in professional software development and is especially important in dynamically typed languages. In practice, the testing carried out to ensure the correct operation of the program can have a much wider range of errors than static type checking, but, on the contrary, cannot search comprehensively for errors that are both testing and static type checking. Testing can be included in the software assembly cycle, in which case it can be considered as "compilation time", check that the user of the program will not need to manually run such tests.

References

  • Pierce, Benjamin (2002). Types and programming languages. MIT Press. ISBN 0-262-16209-1.
+37
04 Oct '09 at 22:37
source share

The terminology "dynamic typed", unfortunately, is misleading. All languages ​​are statically typed, and types are properties of expressions (and not values, as some think). However, some languages ​​have only one type. They are called unified languages. One example of such a language is the untyped lambda calculus.

In the untyped lambda calculus, all members are lambda members, and the only operation that can be performed on a member is applied to another member. Consequently, all operations always lead to infinite recursion or lambda term, but never signal an error.

However, if we increased the untyped lambda calculus with primitive numbers and arithmetic operations, then we could perform meaningless operations by adding two lambda members together: (λx.x) + (λy.y) . It can be argued that the only normal task is to signal an error when this happens, but in order to do this, each value should be marked with an indicator indicating whether the term is a lambda term or a number. Then the add statement checks to see if both arguments are really marked as numbers, and if they are not, report an error. Please note that these tags are not types, because types are properties of programs, not values ​​created by these programs.

The same language that does this is called dynamically typed.

All languages, such as JavaScript, Python, and Ruby, are all printed. Again, the typeof operator in JavaScript and the type function in Python have misleading names; they return tags associated with operands, not their types. Similarly, dynamic_cast in C ++ and instanceof in Java do not perform type checks.

+14
Oct 12 '15 at 16:57
source share

Compiled and interpreted

"When the source code is translated"

  • Source code : source code (usually entered by a person into a computer)
  • Translation Convert source code to what the computer can read (i.e. machine code).
  • Execution time : the period when the program executes commands (after compilation, if compiled)
  • Compiled language : code translated before execution
  • Interpreted language : code translated on the fly at runtime



Typing

"When types are checked"

5 + '3' is an example of a type error in strongly typed languages ​​such as Go and Python, because they do not allow "type coercion" -> the ability of a value to change type in certain contexts, such as merging Two types. Weakly typed languages ​​such as JavaScript will not cause a type error (results in '53' ).

  • Static : types checked before execution
  • Dynamic : types checked on the fly at runtime



The definitions of "Static and Compiled" and "Dynamic and Interpreted" are very similar ... but remember this "when types are checked" and "when the source code is translated."

You will get errors of the same type regardless of whether the language is compiled or interpreted ! You need to separate these terms conceptually.




Python example

Dynamic, interpretable

 def silly(a): if a > 0: print 'Hi' else: print 5 + '3' silly(2) 

Because Python is interpreted and dynamically typed, it only translates and executes the type checking code in which it is executed. The else block is never executed, so 5 + '3' doesn't even look!

What if it was statically injected?

Type input error before the code is run. It still does type checking before runtime, even if it is interpreted.

What if it was compiled?

The else block will be translated / scanned before execution, but due to the fact that it dynamically enters it, it will not cause an error! Dynamically typed languages ​​do not check types before execution, and this line is never executed.




Transition Example

Static, compiled

 package main import ("fmt" ) func silly(a int) { if (a > 0) { fmt.Println("Hi") } else { fmt.Println("3" + 5) } } func main() { silly(2) } 

Types are checked before starting (static), and the type error is fixed immediately! Types were still checked before runtime if they were interpreted with the same result. If it was dynamic, it would not produce any errors, even though the code would be considered at compile time.




Performance

A compiled language will have better run-time performance if it is statically injected (compared to dynamic); knowledge of types allows optimizing machine code.

Statically typed languages ​​have better run-time performance, essentially because there is no need to dynamically check types at runtime (it checks before starting).

Likewise, compiled languages ​​are faster at runtime because the code has already been translated, rather than “interpreting” / translating it on the fly.

Please note that both compiled and statically typed languages ​​will have a delay before starting to translate and type checking, respectively.




More differences

Static typing breaks errors earlier, instead of finding them at runtime (especially useful for long programs). It is more "strict" because it does not allow type errors anywhere in your program and often prevents the change of variable types, which further protects against unintentional errors.

 num = 2 num = '3' // ERROR 

Dynamic typing is more flexible, which some appreciate. This usually allows variables to change types, which can lead to unforeseen errors.

+7
Dec 08 '17 at 21:25
source share

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

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

Examples: C, C ++, Java

Dynamically typed languages : variables can receive different values ​​at runtime, and their type is determined at runtime.

( var a; a can take any values ​​at runtime)

Examples: Ruby, Python.

+6
01 Oct '17 at 14:35
source share

A statically typed type type check at compile time and the type cannot change. (Don't get nice type comments, create a new variable / link).

Dynamic typing of language types at runtime and the type of CAN variable can be changed at runtime.

+5
Jul 27 '16 at 6:56
source share

Sweet and simple definitions, but corresponding needs: Statically typed languages ​​associate a type with a variable in its entire area (Seg: SCALA) Dynamically typed languages ​​associate a type with the actual value that the variable refers to.

+4
Nov 29 '16 at 6:37
source share
  • In a statically typed language, a variable is associated with a type that is known at compile time, and that type remains unchanged at run time. Equivalent to a variable, only a value that is an instance of a known / specified type can be assigned.
  • In a dynamically typed language, a variable has no type, and its value at run time can be of any type and form.
+3
Oct 29 '16 at 20:18
source share

Statically typed languages ​​such as C ++, Java and dynamically typed languages ​​such as Python differ only in terms of variable type execution. Statically typed languages ​​have a static data type for a variable, here the data type is checked at compile time, so debugging is much easier ... whereas dynamically typed languages ​​do not do the same, the data type that the program executes is checked, and therefore debugging is a bit complicated.

Moreover, they have very little difference and can be associated with strongly typed and weakly typed languages. A strictly typed language does not allow one type to be used as another, for example. C and C ++ ... whereas weakly typed languages ​​allow eg.python

+2
Aug 17 '18 at 10:21
source share

Strong typing probably means that the variables are of a clearly defined type and that there are strict rules for combining variables of different types in expressions. For example, if A is an integer and B is a float, then a strict rule regarding A + B may be that A is passed in a float, and the result is returned as a float. If A is an integer and B is a string, then a strict rule may be that A + B is not valid.

Static typing probably means that types are assigned at compile time (or its equivalent for uncompiled languages) and cannot change at run time.

Please note that these classifications are not mutually exclusive, and I would expect them to occur frequently. Many strongly typed languages ​​are also statically typed.

, "", , . .

: , , , . , , .

, Java:

 String str = "Hello"; //statically typed as string str = 5; //would throw an error since java is statically typed 

, , , . , , .

, Python:

 str = "Hello" # it is a string str = 5 # now it is an integer; perfectly OK 

, / ( @Dario):

, Python:

 str = 5 + "hello" # would throw an error since it does not want to cast one type to the other implicitly. 

PHP:

 $str = 5 + "hello"; // equals 5 because "hello" is implicitly casted to 0 // PHP is weakly typed, thus is a very forgiving language. 

. , . .

+1
18 . '18 4:39
source share

, ( ).

0
14 . '18 16:34
source share

:

0
04 . '18 7:36
source share

( ):

  • IDE
  • , ,

(, ):

  • ()
0
16 . '19 6:09
source share

1. ( ) 2. . - , . ( null) .

, .

-5
02 . '15 12:05
source share

: , Java Scala, .

, .

ex. int x; x = 10;

System.out.println();

: Perl - .

.

= 10;

-fourteen
27 . '13 7:19
source share



All Articles