Java中的基本数据类型:
整数类型:
????????byte:1字节,默认值为0;
????????short:2字节,默认值为0;
????????int:4字节,默认值为0;
????????long:8字节,Java中对long类型赋值时可以加也可以不加l或L,默认值为0;
浮点类型:
????????float:4字节,Java中对float类型赋值时可以加也可以不加f或F,默认值为0.0;
????????double:8字节,默认值为0.0;
字符型:
??????? char:2字节,用单引号赋值,默认值为null;
布尔型:
????????boolean:只有true和false两个取值,默认值为false;
易错点:String不是基本数据类型,而是属于引用类型。
package Basic;
public class BasicOperationd {
public static void main(String[] args) {
// TODO Auto-generated method stub
// byte by = 134;//报错,因为超出范围:-127~128
byte by = 125;
short sh1 = 1;
short sh2 = 32211;
// short sh3 = 32789;//报错,因为超出范围:-32768~32767
int a = 15;
int b = 4;
long l1 = 10;
long l2 = 10l;
float f1 = 10;
float f2 = 10f;
//float f3 = 10.0;//会报错
double d1 = 10;
double d2 = 10.0;
double d3 = 10.00;
//addition
int result1 = a+b;
System.out.println("a "+"+"+" b"+" = "+result1);
//Subtraction
int result2 = a-b;
System.out.println("a "+"-"+" b"+" = "+result2);
//Multiplication
int result3 = a*b;
System.out.println("a "+"x"+" b"+" = "+result3);
//Division
int result4 = a/b;
System.out.println("a "+"/"+" b"+" = "+result4);//因为result4是int类型的,所以只会取整数部分
//取余
int result5 = a % b;
System.out.println("a "+"%"+" b"+" = "+result5);
}
}
|