java中的数据类型
在java源代码中,每个变量都必须声明一种类型(type)。有两种类型:primitive type和reference type。引用类型引用对象(reference to object),而基本类型直接包含值(directly contain value)。因此,Java数据类型(type)可以分为两大类:基本类型(primitive types)和引用类型(reference types)。primitive types 包括boolean类型以及数值类型(numeric types)。numeric types又分为整型(integer types)和浮点型(floating-point type)。整型有5种:byte short int long char(char本质上是一种特殊的int)。浮点类型有float和double。
public class Test1 {
public static void main(String[] args) {
//1.类型转换
byte b = 127;//-128~127 256
int i = 12;
//b = i;//类型转换 自动从小升级大类型
byte b2 = 127; //初始化
//默认为整型
//b2 = b2+1;//类型 2 默认为int
b2++ ;//等价于b2 = b2+1;
System.out.println(b2);//01111111 -- 10000000
int ii = 0b10000000;
System.out.println(ii);
byte b11 = 12;
//b11=b11+2;
b11+=2;
// System.out.println(Byte.toBinaryString(-128));
double b3 = 11;
System.out.println(b3>12?13.0:11);
//浮点型 默认double
float f = 1.0F;
double d = 1.2;
}
}