Java基本数据类型转换
-
- 一、Java 八大基本数据类型回顾
- 二、自动类型转换(隐式转换)
-
- 概念
- 自动转换顺序
- 示例代码
- 三、强制类型转换(显式转换)
-
- 概念
- 语法格式
- 示例代码
- 强制转换注意事项
- 四、算术表达式的自动类型提升
-
- 提升规则
- 案例 1:short 运算报错高频坑
- 案例 2:多类型混合运算
- 五、基本数据类型与 String 字符串的转换
-
- 1. 基本类型 → String 字符串
- 2. String 字符串 → 基本类型
- 六、面试重点总结
- 七、常见错题
本文讲解 Java 八大基本数据类型、自动类型转换、强制类型转换、表达式自动提升,以及基本类型与 String 字符串之间的相互转换,适合 Java 初学者学习。
一、Java 八大基本数据类型回顾
Java 中 8 种基本数据类型分为 4 大类:整型、浮点型、字符型、布尔型。
注意:boolean布尔类型不参与类型转换,只有 7 种数值类型可以互相转换。
表格
| byte | 1 字节 | 8bit | -128 ~ 127 | 最小整型 |
| short | 2 字节 | 16bit | -32768 ~ 32767 | 短整型 |
| int | 4 字节 | 32bit | -2³¹ ~ 2³¹‑1 | 默认整型 |
| long | 8 字节 | 64bit | -2⁶³ ~ 2⁶³‑1 | 长整型,字面量需要后缀L/l |
| float | 4 字节 | 32bit | 约 ±3.4E38 | 单精度浮点数,字面量后缀F/f |
| double | 8 字节 | 64bit | 约 ±1.7E308 | 双精度浮点数,浮点字面量默认 double |
| char | 2 字节 | 16bit | 0 ~ 65535 | 字符类型,存储 Unicode 编码 |
| boolean | – | – | true / false | 布尔,不能做类型转换 |
补充:
二、自动类型转换(隐式转换)
概念
取值范围小的类型 → 取值范围大的类型,不需要手动写代码,JVM 自动完成转换,不会丢失数据。 类比:小瓶子的水倒进大瓶子,不会溢出。
自动转换顺序
byte → short → int → long → float → double
char → int → long → float → double
注意:byte和char不会自动互相转换,short和char也不会自动转换。
示例代码
public class AutoConvert {
public static void main(String[] args) {
int a = 6;
long b = a; // int自动转long
float f = b; // long自动转float
double d = f; // float自动转double
System.out.println(b);
System.out.println(f);
System.out.println(d);
char ch = 'A';
int num = ch; // char自动转为int,拿到字符的Unicode编码
System.out.println(num); // 输出65
}
}
注意:虽然long占 8 字节,float占 4 字节,但是long可以自动转float。 原因:float 底层是科学计数法,存储数值范围比 long 更大,不是单纯看字节大小。
三、强制类型转换(显式转换)
概念
取值范围大的类型 → 取值范围小的类型,编译器不会自动转换,需要手动强制。 语法:(目标类型) 要转换的值/变量
类比:大瓶子往小瓶子倒水,水太多就会溢出,发生数据丢失、精度丢失。
语法格式
目标类型 变量 = (目标类型) 原变量/数值;
示例代码
public class ForceConvert {
public static void main(String[] args) {
double d = 3.99;
int i = (int) d; // double强制转int,直接舍弃小数部分,不是四舍五入
System.out.println(i); // 输出3
int num = 130;
byte b = (byte) num; // int强制转byte,超出byte范围,发生溢出
System.out.println(b); //输出 -126
}
}
强制转换注意事项
double x = 5.66;
int y = (int)x;
// x仍然是double类型,值还是5.66,只是把转换后的结果赋值给y
四、算术表达式的自动类型提升
当多个不同基本类型做运算,整个表达式会自动提升类型。
提升规则
优先级从小到大:byte、short、char < int < long < float < double
案例 1:short 运算报错高频坑
public class PromoteDemo {
public static void main(String[] args) {
short shortValue = 5;
// shortValue -2:short提升成int,运算结果是int
// shortValue = shortValue -2; // 编译报错!int不能直接赋值short
// 解决方案1:强制转回short
shortValue = (short)(shortValue -2);
System.out.println(shortValue);
}
}
案例 2:多类型混合运算
byte byteValue =4;
int intValue=1;
double doubleValue= 2.33;
// byte→int,最终最高类型double,结果是double
double result= byteValue + intValue + doubleValue;
System.out.println(result);
面试高频考点:byte b1 = 1; byte b2=2; byte b3 = b1+b2;编译报错。 解析:b1+b2 运算时提升为 int,结果 int,不能直接赋值 byte,需要byte b3=(byte)(b1+b2); 但是byte b4 =1+2;可以编译,因为 1+2 是编译器常量,编译期就计算完成,值在 byte 范围内。
五、基本数据类型与 String 字符串的转换
String 属于引用类型,不是基本类型,开发中非常常用。
1. 基本类型 → String 字符串
两种常用方式
int a = 6;
String s1 = a + ""; // 拼接空字符串转为字符串
String s2 = String.valueOf(a);
System.out.println(s1);
加号+运算坑点:
System.out.println(3+4+"LinkinPark"); //先算3+4=7,输出7LinkinPark
System.out.println("LinkinPark"+3+4); //字符串开头,后面全部拼接,输出LinkinPark34
运算规则:+遇到 String 就变成字符串拼接,从左向右依次运算。
2. String 字符串 → 基本类型
使用对应包装类的parseXxx()静态方法。
包装类:byte→Byte,short→Short,int→Integer,long→Long,float→Float,double→Double,boolean→Boolean
String str = "123";
int num = Integer.parseInt(str); //字符串转int
double d = Double.parseDouble("3.14");//字符串转double
boolean flag = Boolean.parseBoolean("true");
注意:如果字符串格式不匹配数字,会抛出运行时异常NumberFormatException。 例如 Integer.parseInt("abc"); 程序运行报错。
六、面试重点总结
七、常见错题
拓展小知识:包装类还可以实现基本类型和字符串转换,同时包装类支持自动装箱、自动拆箱,这是基本类型和包装类之间的转换,不属于基本数据类型直接转换。





