java数据类型
public class Demo01 {
public static void main(String[] args) {
//字符串
String a = "中国";
//整数
int a1 = 10;
byte aa = 127; //最大值到127
long a2 = 30L; //long类型要在数字后面加个L
//小数:浮点数
float a3 = 10.2F; //float类型要在数字后面加F
double a4 = 3.14159;
//字符
char sex = '男';
//布尔值
boolean zhen = true;
}
}
数据类型扩展
public class Demo2 {
public static void main(String[] args) {
//整数扩展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制0x
System.out.println(i);//10
System.out.println(i2);//8
System.out.println(i3);//16
//转义字符
// \t制表符
// \n换行
System.out.println("Hool\tword");
System.out.println("hoot\nword");
// == 判断是否相等
String xa = "Hallo";
String xb = "Hallo";
System.out.println(xa == xb);
//布尔值扩展
boolean foot = false;
if(foot == false){}//新手
if(foot){}//老手
//代码要精简易读
}
}
类型转换
public class Demo3 {
public static void main(String[] args) {
int aa = 127;
//强制转换,(类型)变量名 高--低
byte bb = (byte)aa;
//自动转换 低--高
double cc = aa;
System.out.println(aa);//127
System.out.println(bb);//127
System.out.println(cc);//127.0
//char类型输出是d的下一位e
System.out.println("=======================");
char a = 'd';
int b = a+1;
System.out.println(b);//101
System.out.println((char)b);//e
}
}
较大数字类型转换用long
public class Demo4 {
public static void main(String[] args) {
//操作比较大的数字时要转换为long类型
int money = 10_0000_0000;//数字较大时可以用下划线隔开
int years = 20;
int total1 = money * years;
System.out.println(total1);//-1474836480,计算的时候溢出
long total2 = money * years;
System.out.println(total2);//-1474836480,要强制类型转换
long total = money * ((long)years);
System.out.println(total);//20000000000
}
}