文章目录
- 1. 变量、常量
- 2. 数据类型
1. 变量、常量
final
修饰常量
public class Variable {static final int YEAR = 365;// 常量使用 final 修饰, 不可修改,类似C++的 conststatic int day = 0;// 成员变量public static void main(String[] args){System.out.println("2年有:" + 2*YEAR + "天");for(int i = 0; i < 3; ++i){System.out.println(day++);}}
}
输出:
2年有:730天
0
1
2
2. 数据类型
byte a1 = Byte.MAX_VALUE, a2 = Byte.MIN_VALUE;
System.out.println(a1 + " " + a2);// 127 -128short b1 = Short.MAX_VALUE, b2 = Short.MIN_VALUE;
System.out.println(b1 + " " + b2);// 32767 -32768int c1 = Integer.MAX_VALUE, c2 = Integer.MIN_VALUE;
System.out.println(c1 + " " + c2); // 2147483647 -2147483648long d1 = Long.MAX_VALUE, d2 = Long.MIN_VALUE;
System.out.println(d1 + " " + d2);
// 9223372036854775807 -9223372036854775808float num = 3.0F;// 或者 3.0f
System.out.println(num*num);// 9.0double num2 = 5.2e52d;//没有d或者D也行
double num3 = 1.1E51;
System.out.println(num2+num3);// 5.31E52char ch1 = 97;
char ch2 = 'a';
System.out.println(ch1);// a
System.out.println(ch2);// aboolean flag = false;
if(flag)System.out.println("flag is " + flag);
elseSystem.out.println("flag is " + flag);// flag is falseint x = 100;
double y = 3.0;
System.out.println(x/y);// 33.333333333333336
// 类型转换// 强制转换 (type) var
int z = 3;
System.out.println(x/z); // 33
System.out.println((float)x/z);// 33.333332
System.out.println(x/(float)z);// 33.333332
System.out.println((float)x/(float)z);// 33.333332int A = (int)1e301;
System.out.println(A);// 2147483647