数据类型扩展
public class Demo03 {
public static void main(String[] args) {
//整数扩展 二进制0b 十进制 八进制0 十六进制0x
int i = 10;//十进制
int i1 = 0100;//八进制4
int i2 = 0x17;//十六进制23
int i3 = 0b1001;//二进制9
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println("=========================");
//字符串的实质是数字
char a = 'a';
char b = '中';
System.out.println(a);
System.out.println((int)a);//强制转化
System.out.println(b);
System.out.println((int)b);
System.out.println("===========================");
/*
转义字符
\t 制表符
\n 换行
*/
System.out.println("Hello\tWorld");
System.out.println("Hello\nWorld");
boolean s = true;
if(s){}
if(s == true){}
}
}
类型转换
-
由小到大
byte–>short–>char–>int–>long–>float–>double(小数先于整数)
-
强制类型转换(由大转小)
-
自动类型转换(由小转大)
public class Demo04 {
public static void main(String[] args) {
int i = 128;//(21亿)
byte s = (byte)i;//强转 内存溢出(byte-128到127)
float d = i;//自动转换
System.out.println(s);
System.out.println(d);
System.out.println("=====================");
//精度问题
System.out.println((int)3.14);
System.out.println((int)3.66F);
//jdk7新特性,数字可以用_隔开
System.out.println("================");
int money = 10_0000_0000;
int years = 30;
int total = money*years;
long total2 = money*years;
System.out.println(total);//-64771072 内存溢出
System.out.println(total2);//-64771072 默认是int,转换之前已经存在问题
long total3 = money*((long)years);
System.out.println(total3);
}
}