1:节本类型
Java中8中基本类型,分为4类
整形: byte 、short、 int、 long
浮点型: float、double
字符型: char
布尔型: boolean
2:基本类型长度及默认值等:
名称 | 长度(字节) | 二进制位 | 包装类 | 取值范围 | 默认值 |
byte | 1 | 8 | Byte | -128~127 | 0 |
short | 2 | 16 | Short | -32768~32767 | 0 |
int | 4 | 32 | Integer | -21474836~2147483647 | 0 |
long | 8 | 64 | Long | -9223372036854774808~9223372036854774807 | 0 |
float | 4 | 32 | Float | 3.402823e+38~1.401298e-45(e+38 表示乘以10的38次方,而e-45 表示乘以10的负45次方) | 0.0 |
double | 8 | 64 | Double | 1.797693e+308~4.9000000e-324 | 0.0 |
char | 2 | 16 | Character | 0~65535 | |
boolean | Boolean | true false | flase |
代码:
public class main {
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;
static short sh;
static String str;
public static void main(String[] args) {
//byte
System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE + "包装类:Byte" +"取值范围" + Byte.MIN_VALUE + "~" + Byte.MAX_VALUE + "默认值:" + by + "\n");
// short
System.out.println("基本类型:short 二进制位数:" + Short.SIZE + "包装类:Short" + "取值范围" + Short.MIN_VALUE + "~" + Short.MAX_VALUE + "默认值:" + sh + "\n");
// intSystem.out.println("基本类型:int 二进制位数:" + Integer.SIZE + "包装类:Integer" +"取值范围" + Integer.MIN_VALUE + "~" + Integer.MAX_VALUE + "默认值:" + i + "\n");
// long
System.out.println("基本类型:long 二进制位数:" + Long.SIZE + "包装类:Long" + "取值范围" + Long.MIN_VALUE + "~" + Long.MAX_VALUE + "默认值:" + l + "\n");
// floatSystem.out.println("基本类型:float 二进制位数:" + Float.SIZE + "包装类:Float" +"取值范围" + Float.MIN_VALUE + "~" + Float.MAX_VALUE + "默认值:" + f + "\n");
// double
System.out.println("基本类型:double 二进制位数:" + Double.SIZE + "包装类:Double" + "取值范围" + Double.MIN_VALUE + "~" + Double.MAX_VALUE + "默认值:" + d + "\n");
// char
System.out.println("基本类型:char 二进制位数:" + Character.SIZE + "包装类:Character" +"取值范围" + (int) Character.MIN_VALUE + "~" + (int) Character.MAX_VALUE + "默认值:" + ch + "\n");
//boolean
System.out.println("基本类型:boolean " + "默认值:" + bool + "\n");
//String
System.out.println("引用类型:String " + "默认值:" + str + "\n");
}
3:拆箱与装箱
拆箱:把包装类型转为基本类型
装箱:把基本类型转为装箱