二.变量、运算符、程序控制结构
- 八大基本数据类型:整型(byte,short,int,long)、浮点型(float,double)、字符型(char)、布尔型(boolean)
- 自动类型转换:
- char->int->long->float->double
- byte->short->int->long->float->double
- 当把精度大的数据类型赋值给精度小的数据类型时,就会报错,繁殖会自动进行类型转换‘
- (byte,short)和char之间不会互相自动转换
- boolean不参与转换
- 强制类型转换:
- 当数据精度从大->小,就需要强制转换
- int i = (int)1.9;
- string类型和基本数据类型的转换:
-
基本类型转string:基本数据类型+""即可
-
string转基本类型:通过基本类型的包装类调用parseXX方法即可
//基本数据类型->String
int n1 = 100;
boolean b1 = true;
String str1 = n1 +“”;
String str2 = b1 +“”;//String->基本数据类型
Integer.parseInt(“123”);
Float.parseFloat(“123.65”);
Boolean.parseBoolean(“true”);
-
- 三元运算符:int max = (x > y) ? x : y;