Java基本数据类型总结整理串讲
#一、Java基本数据类型概述
#Java语言提供了8种基本数据类型,它们可以分为4大类:
- 整数类型:byte、short、int、long
- 浮点类型:float、double
- 字符类型:char
- 布尔类型:boolean
1.1 整数类型
#byte(字节型)
#- 大小:1字节(8位)
- 取值范围:-128 到 127
- 默认值:0
- 用途:主要用于节省内存空间,处理字节流数据
示例代码:
1
2
3
4
5
6
7
8
9
| public class ByteExample {
public static void main(String[] args) {
byte b1 = 100;
byte b2 = -50;
// byte b3 = 200; // 编译错误:超出范围
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
}
}
|
short(短整型)
#- 大小:2字节(16位)
- 取值范围:-32,768 到 32,767
- 默认值:0
- 用途:用于节省内存,处理较小的整数
示例代码:
1
2
3
4
5
6
7
8
| public class ShortExample {
public static void main(String[] args) {
short s1 = 1000;
short s2 = -2000;
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
}
}
|
int(整型)
#- 大小:4字节(32位)
- 取值范围:-2,147,483,648 到 2,147,483,647
- 默认值:0
- 用途:最常用的整数类型,Java中整数字面量默认为int类型
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
| public class IntExample {
public static void main(String[] args) {
int i1 = 100000;
int i2 = -50000;
int i3 = 0x1A; // 十六进制:26
int i4 = 012; // 八进制:10
System.out.println("i1 = " + i1);
System.out.println("i2 = " + i2);
System.out.println("i3 = " + i3);
System.out.println("i4 = " + i4);
}
}
|
long(长整型)
#- 大小:8字节(64位)
- 取值范围:-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
- 默认值:0L
- 用途:处理大整数,需要在数字后加L或l
示例代码:
1
2
3
4
5
6
7
8
9
10
| public class LongExample {
public static void main(String[] args) {
long l1 = 10000000000L; // 必须加L
long l2 = -5000000000L;
long l3 = 100L;
System.out.println("l1 = " + l1);
System.out.println("l2 = " + l2);
System.out.println("l3 = " + l3);
}
}
|
1.2 浮点类型
#float(单精度浮点型)
#- 大小:4字节(32位)
- 取值范围:约 ±3.40282347E+38F
- 精度:约7位有效数字
- 默认值:0.0f
- 用途:节省内存的浮点数,需要在数字后加F或f
示例代码:
1
2
3
4
5
6
7
8
9
10
| public class FloatExample {
public static void main(String[] args) {
float f1 = 3.14f; // 必须加f
float f2 = 2.5F;
float f3 = 1.23e4f; // 科学计数法:12300.0
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
System.out.println("f3 = " + f3);
}
}
|
double(双精度浮点型)
#- 大小:8字节(64位)
- 取值范围:约 ±1.79769313486231570E+308
- 精度:约15-17位有效数字
- 默认值:0.0d
- 用途:最常用的浮点类型,Java中浮点数字面量默认为double类型
示例代码:
1
2
3
4
5
6
7
8
9
10
| public class DoubleExample {
public static void main(String[] args) {
double d1 = 3.141592653589793;
double d2 = 2.5;
double d3 = 1.23e10; // 科学计数法
System.out.println("d1 = " + d1);
System.out.println("d2 = " + d2);
System.out.println("d3 = " + d3);
}
}
|
1.3 字符类型
#char(字符型)
#- 大小:2字节(16位)
- 取值范围:’\u0000’(0)到 ‘\uffff’(65,535)
- 默认值:’\u0000’
- 用途:存储单个Unicode字符
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
| public class CharExample {
public static void main(String[] args) {
char c1 = 'A';
char c2 = '中';
char c3 = '\u0041'; // Unicode编码:'A'
char c4 = 65; // ASCII码:'A'
System.out.println("c1 = " + c1);
System.out.println("c2 = " + c2);
System.out.println("c3 = " + c3);
System.out.println("c4 = " + c4);
}
}
|
1.4 布尔类型
#boolean(布尔型)
#- 大小:JVM规范未明确规定,通常为1字节
- 取值范围:true 或 false
- 默认值:false
- 用途:表示逻辑值,不能与整数类型相互转换
示例代码:
1
2
3
4
5
6
7
8
9
10
| public class BooleanExample {
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = (10 > 5);
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
System.out.println("b3 = " + b3);
}
}
|
二、练习题
#练习题1:数据类型声明与赋值
#请指出以下代码中的错误并修正:
1
2
3
4
5
6
7
8
9
10
11
| public class Exercise1 {
public static void main(String[] args) {
byte b = 200; // 错误1
short s = 50000; // 错误2
int i = 3.14; // 错误3
long l = 10000000000; // 错误4
float f = 3.14; // 错误5
char c = "A"; // 错误6
boolean bool = 1; // 错误7
}
}
|
参考答案:
1
2
3
4
5
6
7
8
9
10
11
| public class Exercise1 {
public static void main(String[] args) {
byte b = 127; // 修正:byte范围是-128到127
short s = 5000; // 修正:short范围是-32768到32767
int i = 3; // 修正:int是整数类型
long l = 10000000000L; // 修正:long类型需要加L
float f = 3.14f; // 修正:float类型需要加f
char c = 'A'; // 修正:char使用单引号
boolean bool = true; // 修正:boolean只能是true或false
}
}
|
练习题2:数据类型转换
#请分析以下代码的输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| public class Exercise2 {
public static void main(String[] args) {
byte b = 10;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
System.out.println("b = " + b);
System.out.println("s = " + s);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("f = " + f);
System.out.println("d = " + d);
}
}
|
参考答案:
1
2
3
4
5
6
| b = 10
s = 10
i = 10
l = 10
f = 10.0
d = 10.0
|
练习题3:字符类型操作
#请编写代码,实现以下功能:
- 声明一个char变量,存储字符’A'
- 将其转换为对应的ASCII码值
- 将ASCII码值加1后,再转换回字符
参考答案:
1
2
3
4
5
6
7
8
9
10
11
| public class Exercise3 {
public static void main(String[] args) {
char c = 'A';
int ascii = (int) c; // 字符转ASCII码
System.out.println("字符 " + c + " 的ASCII码是: " + ascii);
ascii = ascii + 1;
char nextChar = (char) ascii; // ASCII码转字符
System.out.println("ASCII码 " + ascii + " 对应的字符是: " + nextChar);
}
}
|
练习题4:浮点数精度问题
#请分析以下代码的输出,并解释原因:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| public class Exercise4 {
public static void main(String[] args) {
float f1 = 0.1f;
float f2 = 0.2f;
float sum = f1 + f2;
System.out.println("f1 + f2 = " + sum);
System.out.println("sum == 0.3? " + (sum == 0.3f));
double d1 = 0.1;
double d2 = 0.2;
double sum2 = d1 + d2;
System.out.println("d1 + d2 = " + sum2);
System.out.println("sum2 == 0.3? " + (sum2 == 0.3));
}
}
|
参考答案:
1
2
3
4
| f1 + f2 = 0.3
sum == 0.3? true
d1 + d2 = 0.30000000000000004
sum2 == 0.3? false
|
解释: 浮点数在计算机中采用二进制表示,某些十进制小数无法精确表示,导致精度问题。float精度约7位,double精度约15-17位,但都不能完全避免精度误差。
三、基本数据类型对比分析表
#| 数据类型 | 关键字 | 大小 | 取值范围 | 默认值 | 包装类 | 字面量后缀 | 常用场景 |
|---|
| 整数类型 | | | | | | | |
| 字节型 | byte | 1字节 (8位) | -128 ~ 127 | 0 | Byte | 无 | 字节流处理、节省内存 |
| 短整型 | short | 2字节 (16位) | -32,768 ~ 32,767 | 0 | Short | 无 | 节省内存的小整数 |
| 整型 | int | 4字节 (32位) | -2,147,483,648 ~ 2,147,483,647 | 0 | Integer | 无 | 最常用的整数类型 |
| 长整型 | long | 8字节 (64位) | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 | 0L | Long | L 或 l | 大整数处理 |
| 浮点类型 | | | | | | | |
| 单精度 | float | 4字节 (32位) | ±3.40282347E+38F | 0.0f | Float | F 或 f | 节省内存的浮点数 |
| 双精度 | double | 8字节 (64位) | ±1.79769313486231570E+308 | 0.0d | Double | 无(默认) | 最常用的浮点类型 |
| 字符类型 | | | | | | | |
| 字符型 | char | 2字节 (16位) | ‘\u0000’ ~ ‘\uffff’ (0 ~ 65,535) | ‘\u0000’ | Character | 无 | Unicode字符存储 |
| 布尔类型 | | | | | | | |
| 布尔型 | boolean | 未明确规定(通常1字节) | true 或 false | false | Boolean | 无 | 逻辑判断 |
3.1 内存占用对比
#1
2
3
4
5
6
7
| byte < short < int < long
↓
float < double
↓
char (2字节)
↓
boolean (通常1字节)
|
3.2 使用建议
#整数类型选择:
- 一般情况使用
int - 需要节省内存且数值范围小使用
byte 或 short - 需要处理大整数使用
long
浮点类型选择:
- 一般情况使用
double(精度更高) - 需要节省内存且精度要求不高使用
float
字符类型:
- 使用
char 存储单个字符 - 使用
String 存储字符串
布尔类型:
- 只能使用
true 或 false,不能与数字相互转换
四、拓展:基本数据类型之间的转换
#4.1 类型转换概述
#Java中的数据类型转换分为两种:
- 自动类型转换(隐式转换):小类型向大类型转换
- 强制类型转换(显式转换):大类型向小类型转换
4.2 自动类型转换(隐式转换)
#自动类型转换遵循以下规则:
- 转换方向:小类型 → 大类型
- 转换顺序:byte → short → int → long → float → double
- char类型可以自动转换为int及以上类型
转换规则图:
1
2
3
| byte → short → int → long → float → double
↑
char
|
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public class AutoConversion {
public static void main(String[] args) {
// 整数类型之间的自动转换
byte b = 10;
short s = b; // byte自动转换为short
int i = s; // short自动转换为int
long l = i; // int自动转换为long
// 整数到浮点数的自动转换
float f = l; // long自动转换为float
double d = f; // float自动转换为double
// char到int的自动转换
char c = 'A';
int charToInt = c; // char自动转换为int(ASCII码值)
System.out.println("b = " + b);
System.out.println("s = " + s);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("f = " + f);
System.out.println("d = " + d);
System.out.println("c = " + c);
System.out.println("charToInt = " + charToInt); // 输出:65
}
}
|
4.3 强制类型转换(显式转换)
#强制类型转换需要使用类型转换运算符 (类型),可能导致数据丢失或精度损失。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| public class ForceConversion {
public static void main(String[] args) {
// 大类型向小类型转换
int i = 300;
byte b = (byte) i; // 强制转换,可能丢失数据
System.out.println("i = " + i); // 输出:300
System.out.println("b = " + b); // 输出:44(数据溢出)
// 浮点数向整数转换
double d = 3.14;
int intValue = (int) d; // 强制转换,丢失小数部分
System.out.println("d = " + d); // 输出:3.14
System.out.println("intValue = " + intValue); // 输出:3
// 整数向字符转换
int ascii = 65;
char c = (char) ascii; // 强制转换
System.out.println("ascii = " + ascii); // 输出:65
System.out.println("c = " + c); // 输出:A
// 字符向整数转换(可以自动转换,也可以显式转换)
char ch = 'B';
int num = (int) ch;
System.out.println("ch = " + ch); // 输出:B
System.out.println("num = " + num); // 输出:66
}
}
|
4.4 特殊情况
#4.4.1 整数运算中的类型提升
#在表达式中,小于int的类型(byte、short、char)会自动提升为int类型参与运算。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
| public class TypePromotion {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = 20;
// byte b3 = b1 + b2; // 编译错误:b1 + b2的结果是int类型
byte b3 = (byte)(b1 + b2); // 需要强制转换
int result = b1 + b2; // 正确:使用int接收
System.out.println("b3 = " + b3);
System.out.println("result = " + result);
}
}
|
4.4.2 浮点数运算
#浮点数运算中,float会自动提升为double。
示例代码:
1
2
3
4
5
6
7
8
9
10
| public class FloatOperation {
public static void main(String[] args) {
float f1 = 1.5f;
float f2 = 2.5f;
// float f3 = f1 + f2; // 正确:两个float相加结果仍是float
double d = f1 + f2; // 正确:float可以自动转换为double
System.out.println("d = " + d);
}
}
|
4.4.3 boolean类型转换
#重要: boolean类型不能与其他任何基本数据类型相互转换!
1
2
3
4
5
6
7
8
| public class BooleanConversion {
public static void main(String[] args) {
boolean b = true;
// int i = (int) b; // 编译错误:boolean不能转换为int
// boolean b2 = 1; // 编译错误:int不能转换为boolean
// boolean b3 = 0; // 编译错误:int不能转换为boolean
}
}
|
4.5 字符串与基本数据类型的转换
#4.5.1 基本数据类型 → String
#方法1:使用String.valueOf()
1
2
| int i = 100;
String str1 = String.valueOf(i); // "100"
|
方法2:使用包装类的toString()方法
1
2
| int i = 100;
String str2 = Integer.toString(i); // "100"
|
方法3:使用字符串拼接
1
2
| int i = 100;
String str3 = "" + i; // "100"
|
4.5.2 String → 基本数据类型
#使用包装类的parseXxx()方法或valueOf()方法。
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public class StringConversion {
public static void main(String[] args) {
// String转int
String str1 = "100";
int i = Integer.parseInt(str1);
// 或
int i2 = Integer.valueOf(str1);
// String转double
String str2 = "3.14";
double d = Double.parseDouble(str2);
// String转boolean
String str3 = "true";
boolean b = Boolean.parseBoolean(str3);
// String转char(取第一个字符)
String str4 = "A";
char c = str4.charAt(0);
System.out.println("i = " + i);
System.out.println("d = " + d);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
|
五、类型转换练习题
#练习题1:自动类型转换
#请分析以下代码的输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class ConversionExercise1 {
public static void main(String[] args) {
byte b = 10;
short s = b;
int i = s;
long l = i;
float f = l;
double d = f;
char c = 'A';
int charInt = c;
System.out.println("b = " + b);
System.out.println("s = " + s);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("f = " + f);
System.out.println("d = " + d);
System.out.println("c = " + c);
System.out.println("charInt = " + charInt);
}
}
|
参考答案:
1
2
3
4
5
6
7
8
| b = 10
s = 10
i = 10
l = 10
f = 10.0
d = 10.0
c = A
charInt = 65
|
练习题2:强制类型转换
#请分析以下代码的输出结果,并解释原因:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| public class ConversionExercise2 {
public static void main(String[] args) {
int i1 = 300;
byte b1 = (byte) i1;
System.out.println("i1 = " + i1 + ", b1 = " + b1);
double d1 = 3.9;
int i2 = (int) d1;
System.out.println("d1 = " + d1 + ", i2 = " + i2);
long l1 = 1234567890123L;
int i3 = (int) l1;
System.out.println("l1 = " + l1 + ", i3 = " + i3);
float f1 = 3.14f;
int i4 = (int) f1;
System.out.println("f1 = " + f1 + ", i4 = " + i4);
}
}
|
参考答案:
1
2
3
4
| i1 = 300, b1 = 44
d1 = 3.9, i2 = 3
l1 = 1234567890123, i3 = -539222987
f1 = 3.14, i4 = 3
|
解释:
b1 = 44:300超出byte范围,发生溢出i2 = 3:强制转换截断小数部分i3 = -539222987:long值超出int范围,发生溢出i4 = 3:强制转换截断小数部分
练习题3:类型提升
#请修正以下代码中的错误:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| public class ConversionExercise3 {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = 20;
byte b3 = b1 + b2; // 错误1
short s1 = 100;
short s2 = 200;
short s3 = s1 + s2; // 错误2
char c1 = 'A';
char c2 = 'B';
char c3 = c1 + c2; // 错误3
}
}
|
参考答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| public class ConversionExercise3 {
public static void main(String[] args) {
byte b1 = 10;
byte b2 = 20;
byte b3 = (byte)(b1 + b2); // 修正:需要强制转换
// 或
int result1 = b1 + b2; // 修正:使用int接收
short s1 = 100;
short s2 = 200;
short s3 = (short)(s1 + s2); // 修正:需要强制转换
// 或
int result2 = s1 + s2; // 修正:使用int接收
char c1 = 'A';
char c2 = 'B';
char c3 = (char)(c1 + c2); // 修正:需要强制转换
// 或
int result3 = c1 + c2; // 修正:使用int接收
System.out.println("b3 = " + b3);
System.out.println("s3 = " + s3);
System.out.println("c3 = " + c3);
}
}
|
练习题4:字符串转换
#请编写代码实现以下功能:
- 将整数100转换为字符串
- 将字符串"200"转换为整数
- 将浮点数3.14转换为字符串
- 将字符串"2.5"转换为浮点数
参考答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| public class ConversionExercise4 {
public static void main(String[] args) {
// 1. 整数转字符串
int i = 100;
String str1 = String.valueOf(i);
// 或 String str1 = Integer.toString(i);
// 或 String str1 = "" + i;
System.out.println("整数100转字符串: " + str1);
// 2. 字符串转整数
String str2 = "200";
int num = Integer.parseInt(str2);
// 或 int num = Integer.valueOf(str2);
System.out.println("字符串\"200\"转整数: " + num);
// 3. 浮点数转字符串
double d = 3.14;
String str3 = String.valueOf(d);
// 或 String str3 = Double.toString(d);
System.out.println("浮点数3.14转字符串: " + str3);
// 4. 字符串转浮点数
String str4 = "2.5";
double d2 = Double.parseDouble(str4);
// 或 double d2 = Double.valueOf(str4);
System.out.println("字符串\"2.5\"转浮点数: " + d2);
}
}
|
练习题5:综合应用
#请编写一个程序,实现以下功能:
- 声明一个int变量,值为100
- 将其转换为long、float、double
- 将double值强制转换为int,观察结果
- 将int值转换为String,再转换回int
参考答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public class ConversionExercise5 {
public static void main(String[] args) {
// 1. 声明int变量
int i = 100;
System.out.println("原始int值: " + i);
// 2. 自动转换为long、float、double
long l = i;
float f = i;
double d = i;
System.out.println("转换为long: " + l);
System.out.println("转换为float: " + f);
System.out.println("转换为double: " + d);
// 3. double强制转换为int
double d2 = 3.9;
int i2 = (int) d2;
System.out.println("double " + d2 + " 强制转换为int: " + i2);
// 4. int转String再转回int
String str = String.valueOf(i);
System.out.println("int转String: " + str);
int i3 = Integer.parseInt(str);
System.out.println("String转回int: " + i3);
}
}
|
六、类型转换对比分析表
#6.1 自动类型转换规则表
#| 源类型 | 可自动转换的目标类型 | 转换方向 | 是否安全 |
|---|
| byte | short, int, long, float, double | 小→大 | 安全 |
| short | int, long, float, double | 小→大 | 安全 |
| int | long, float, double | 小→大 | 安全 |
| long | float, double | 小→大 | 可能精度损失 |
| float | double | 小→大 | 安全 |
| char | int, long, float, double | 小→大 | 安全 |
| boolean | 无 | - | 不能转换 |
6.2 强制类型转换规则表
#| 源类型 | 可强制转换的目标类型 | 转换方向 | 风险说明 |
|---|
| short | byte | 大→小 | 可能溢出 |
| int | byte, short | 大→小 | 可能溢出 |
| long | byte, short, int | 大→小 | 可能溢出 |
| float | byte, short, int, long | 大→小 | 截断小数部分 |
| double | byte, short, int, long, float | 大→小 | 截断小数部分,可能精度损失 |
| int | char | - | 安全(在有效范围内) |
| char | byte, short | - | 可能溢出 |
| boolean | 任何类型 | - | 不能转换 |
6.3 类型转换优先级与顺序
#1
2
3
4
5
6
7
8
9
| 转换优先级(从低到高):
byte → short → int → long → float → double
↑
char
说明:
1. 箭头方向表示可以自动转换
2. 反向转换需要强制类型转换
3. boolean类型不参与数值转换
|
6.4 常见转换场景对比
#| 转换场景 | 转换方式 | 示例代码 | 注意事项 |
|---|
| 整数→整数(小→大) | 自动转换 | int i = byteValue; | 安全,无数据丢失 |
| 整数→整数(大→小) | 强制转换 | byte b = (byte)intValue; | 可能溢出,需检查范围 |
| 整数→浮点数 | 自动转换 | float f = intValue; | 安全,但可能精度问题 |
| 浮点数→整数 | 强制转换 | int i = (int)doubleValue; | 截断小数部分 |
| 字符→整数 | 自动转换 | int i = charValue; | 转换为ASCII码值 |
| 整数→字符 | 强制转换 | char c = (char)intValue; | 需在有效范围内(0-65535) |
| 基本类型→String | 方法调用 | String s = String.valueOf(i); | 多种方法可选 |
| String→基本类型 | 方法调用 | int i = Integer.parseInt(s); | 需处理NumberFormatException |
6.5 类型转换最佳实践
#| 实践建议 | 说明 | 示例 |
|---|
| 避免不必要的强制转换 | 优先使用自动转换 | int i = byteValue; 而非 int i = (int)byteValue; |
| 检查范围后再转换 | 大类型转小类型前检查 | if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) |
| 浮点数比较使用误差范围 | 避免直接相等比较 | Math.abs(f1 - f2) < 0.0001 |
| 字符串转换处理异常 | 使用try-catch | try { int i = Integer.parseInt(s); } catch(NumberFormatException e) {} |
| 明确使用字面量后缀 | long用L,float用F | long l = 100L; float f = 3.14f; |