Strongly typed versus weakly typed languages
Strongly typed language
In 1974, two American computer scientists Liskov Zilles proposed the concept of strongly typed languages.
In strongly typed languages, when an object is passed from a calling function to the called function, its type must be compatible with the type declared in the called function
function A(){
B(x)
}
functionB(y){// y can be assigned to x, the program works fine}Copy the code
This is a broad definition and does not set out specific rules. Later definitions of strongly typed languages will be more precise.
Strongly typed languages: It is not allowed to change the data type of a variable unless a cast is performed.
In Java, for example
public class HelloWorld {
public static void main(String []args) {
int x =1;
boolean y = true; x = y; System.out.println(x); }}Copy the code
Define x as a number and y as a Boolean. Assigning y to x will result in an error.
public class HelloWorld {
public static void main(String []args) {
int x =1;
char y = 'a'x = y; System.out.println(x); / / 97}}Copy the code
Define x as a number and y as a character, assign y to x, and the final value is 97. Java performs a cast here, assigning the ASCll code of character A to X, so that x is still an integer.
Weakly typed language
Weakly typed languages: Variables can be assigned different values
It’s the same example, running it in js in the browser.
In general, strongly typed languages impose strict limits on how variables can be converted to each other. Variables of different types cannot be assigned to each other unless cast, thus avoiding some low-level errors. Weakly-typed languages, on the other hand, have few constraints and can be very flexible with type conversions, but are also more prone to bugs.
Statically typed versus dynamically typed languages
Statically typed language: The types of all variables are determined at compile time. Dynamically typed language: The types of all variables are determined at execution time.
To illustrate:
class C { constructor(x, y) { this.x = x; this.y = y; }}function add(a) {
return a.x + a.y;
}
var c1 = new C(10, 20);
var c2 = new C('hello'.'world');
console.log(add(c1)); // 30
console.log(add(c2)); // helloworld
Copy the code
Define a class C, add two attributes x and y to it, and then define an add function to add all the attributes of an instance. When the JS compiler looks at this code, it has no idea what the type of parameter a is in the add function until it executes it. Finally c1 is the addition of numbers and C2 is the concatenation of strings.
The same code, with c++ implementation
class C {
public;
int x;
int y;
}
int add(C a) {
return a.x + a.y;
}
Copy the code
At compile time, we know that the attributes x and y in C are of type integer.
From a memory allocation point of view, the above example, since c++ defines the property type as an integer from the beginning, assumes that x is the base address and y is at the base address +4. Js is different. Since we do not know the attribute type at the beginning, we do not know how much address space each attribute occupies, so we need extra space to store the attribute name, and then dynamically calculate the address offset of the attribute when the program runs.
From this point of view, dynamically typed languages have some loss in space footprint.
Summarize the differences between the two
Statically typed language | Dynamically typed language |
---|---|
Be strict about type checking | Be relaxed about type checking |
Find errors immediately | Bugs are hard to spot |
Good running performance | Poor running performance |
Since the document | Poor readability |
But dynamically typed languages also have ways to remedy these shortcomings. For example,
- Performance can be improved (V8 engine) and language flexibility is essential
- Hidden errors can be found by unit testing
- Documents are generated by tools
So although JS is a dynamic weakly typed language, it is one of the most popular languages at present, because of its flexibility and wide application scenarios.
Finally, take a look at strong and weak typing and dynamic static typing for various languages.