正数:
byte: 1字节 8位 范围字节计算
short:2字节 很少使用
int: 4字节 最常用
long:8字节 长整型
小数:
float:4字节
double:8字节
其他:
char:2字节 无符号
boolean:1字节 代表真假
“`
class Hello{
public static void main(String[] args){
//单独直接列出的数字默认就是int型
//直接一个数字,称为数字字面量,整数的字面量默认先是int类型
int a = 10;
int b = 36; /* 默认转换了(前提byte能够容纳)自动去掉了前面的0,只留下8位 */
//打印a的二进制 java默认把前面的0去掉
System.out.println(Integer.toBinaryString(a));
//0x开头代表十六进制
int c = 0x23;
//0开头八进制
int d = 036;
//打印十六进制
System.out.println(Integer.toHexString(b));
//int 最大值
System.out.println(Integer.MAX_VALUE);
//直接结尾加上l
long e = 1000000000000000000l;
long time = System.currentTimeMillis();
//1970年1月1日 到现在的毫秒数
System.out.println(time);
//等号右边的小数默认是double类型
double f = 10.5;
//直接结尾加上f
float g = 20.5f;
//浮点数不能做精确运算,会有舍入误差
/*
* 类型转换
* 一般情况:
* 大范围往小范围需要强制转换
* double a = 10.5;
* int b = (int)a;
* float c = (float)a;
*
* 小范围往大范围可默认转换
*
* */
char n = 'n';
System.out.println(n);
}
}