静态类型语言
1、简介
编译前需要做类型检查,使用前需要声明变量。
2、常见静态类型语言
声明变量需要一些关键字如 int、char、float、double等。
C语言
// 整数
int a = 10;
int a = 10.6;
printf("%d %d", a, b); // 7 7 %d按整数型输出
//浮点数
float i = 7.123456789;
double j = 7.123456789123;
printf("%f %f", i, j); // 7.123457 7.123457
printf("%d %d", sizeof(i), sizeof(j)); // 4 8
// 字符
char ch = 'A';
printf("%c", ch); // A
C++
int a = 7;
char b = 'B';
float c= 7.123456789;
cout << a << endl; // 7
cout << b << endl; // B
cout << c << endl; // 7.12346
Java
public class test1 {
static int a = 0; // 类变量
String str="hello java"; // 实例变量
public void method(){
int i =0; // 局部变量
}
}
动态类型语言
1、简介
编译前不做类型检查,运行时,变量赋值后才会具有某种类型。
2、常见动态类型语言
Python
a, b, c, d = 77, 7.5, True, 1+7j
print(a, type(a)) // 77 <class 'int'>
print(b, type(b)) // 7.5 <class 'float'>
print(a, type(c)) // True <class 'bool'>
print(a, type(d)) // (1+7j) <class 'complex'>
Javascript
let a = 7;
let b = '';
let c = false;
let d = null;
let e;
let f = Symbol('abc');
console.log(a + " " + typeof(a)); // 7 number
console.log(b + " " + typeof(b)); // string
console.log(c + " " + typeof(c)); // false boolean
console.log(d + " " + Object.prototype.toString.call(d)); // null [object Null]
console.log(e + " " + typeof(e)); // undefined undefined
console.log(f.toString() + " " + typeof(f)); // Symbol(abc) symbol