学习java的童鞋都知道,在java里有八大基本类型,byte、char、short、int、long、float、double、boolean。这里我对这八大基本类型进行了一次整理,加深对这些基本类型的认识。
byte
byte为字节类型,我们知道1个字节占8位,有符号数可表示 -128~127,而0~127对应于ascii码表
char
char为字符类型,java默认采用Unicode标准,每个字符占用2个字节(16位),无符号数可表示 0~65535,即定义的总字符数有65536个,而中文里的有些繁体字,无法使用char来定义。
short
short为短整型,占用2个字节(16位),有符号数可表示 -32768到32767
int
int为整型,是我们最常用的基本类型,占用4个字节(32位),有符号数可表示-2147483648到2147483647
long
long为长整型,占用8个字节(64位),有符号数可表示 -9223372036854774808到9223372036854774807
float
float是单精度的浮点数类型,占用4个字节(32位),注意浮点数数据是不完全精确的,不能用于精确计算的场合,精确计算使用BigDecimal
double
double是双精度的浮点数类型,占用8个字节(64位),跟float一样不能用于精确计算
boolean
boolean类型没有准确定义占用字节数,取决于虚拟机
代码样例
/**
* Description: EightBasicType
*
* 该类用于测试java的8种基本类型
*/
public class EightBasicType {
public static void main(String[] args) {
//1. byte
testByte();
//2. char
testChar();
//3. short
testShort();
//4. int
testInt();
//5. long
testLong();
//6. float
testFloat();
//7. double
testDouble();
//8. boolean
testBoolean();
}
/**
* 测试字节类型byte 一个字节为8位,可以表示-128到127
*/
private static void testByte() {
//字符
byte a = 'A';
//8进制
byte b = 030;
//10进制
byte c = -128;
//16进制
byte d = 0x11;
System.out.println("==================== byte ====================");
System.out.println("字符A的ascii码值:" + a);
System.out.println("8进制数030的ascii码值:" + b);
System.out.println("10进制数-128的值:" + c);
System.out.println("16进制数0x11的ascii码值:" + d);
}
/**
* 测试字符类型char
* java默认采用unicode编码(UTF-8、UTF-16、UTF-32是unicode的不同实现):1个字符占用2个字节
* 2个字节,无符号16位可以表示从0到65535,可表示65536个字符,所以有的繁体字无法用char类型定义。
*/
private static void testChar() {
//字符
char a = '说';
//8进制
char b = 046;
//十进制
char c = 65535;
//16进制
char d = 0x21;
System.out.println("==================== char ====================");
System.out.println("字符A: " + a);
System.out.println("8进制046的字符表示: " + b);
System.out.println("10进制65535的字符表示: " + c);
System.out.println("16进制0x21的字符表示: " + d);
}
/**
* 测试整数类型short
* short类型占用2个字节,有符号位16位可表示从-32768到32767
*/
private static void testShort() {
short a = -32768;
short b = 32767;
short c = '来';
System.out.println("==================== short ====================");
System.out.println("short类型最小值:" + a);
System.out.println("short类型最大值:" + b);
System.out.println("来字的unicode编码值:" + c);
}
/**
* 测试整数类型int
* int类型占用4个字节,有符号位32位可表示从-2147483648到2147483647
*/
private static void testInt() {
int a = -2147483648;
int b = 2147483647;
int c = '謽';
System.out.println("==================== int ====================");
System.out.println("int类型最小值:" + a);
System.out.println("int类型最大值:" + b);
System.out.println("謽字的unicode编码值:" + c);
}
/**
* 测试整数类型long
* long类型占用8个字节,有符号位64位可表示从-9223372036854774808到9223372036854774807
*/
private static void testLong() {
long a = -9223372036854774808L;
long b = 9223372036854774807L;
System.out.println("==================== long ====================");
System.out.println("long类型最小值:" + a);
System.out.println("long类型最大值:" + b);
}
/**
* 测试浮点数类型float
* float类型占用4个字节,单精度
* 浮点型的数据是不能完全精确的,所以不应该被用于要求精确结果的场合,商业计算应使用BigDecimal(String)
*/
private static void testFloat() {
float a = 0.5345234f;
float b = -1.2f;
System.out.println("==================== float ====================");
System.out.println("浮点数0.50乘以2:" + a * 2);
System.out.println("浮点数-1.2除以3:" + b / 3);
}
/**
* 测试浮点数类型double
* double类型占用8个字节,双精度,默认
*/
private static void testDouble() {
double a = 0.50;
double b = 1.2;
System.out.println("==================== double ====================");
System.out.println("浮点数0.50乘以2:" + a * 2);
System.out.println("浮点数-1.2除以3:" + b / 3);
}
/**
* 测试布尔类型boolean
* boolean类型没有精确定义占用几个字节,取决于它的虚拟机,一般是占用1个字节
*/
private static void testBoolean() {
boolean flag = true;
System.out.println("==================== boolean ====================");
System.out.println("flag:" + flag);
}
}
网友评论