Java强制类型转换的所有规则及说明
作者:緑水長流*z
强制转换
在Java中,我们也可以将==数据取值范围大的转换为数据取值范围小到的==,这种转换并不是自动的过程,而是需要我们手动的转换,我们称这种转换为强制转换。
例如下面代码:
long a = 10; int b = a; // 报错
long类型内存8个字节,int类型内存4个字节。
long取值范围大于int;想要赋值成功,只有通过强制类型转换,将 long类型强制转换成int类型才能赋值。
强制转换:将 取值范围大的类型 强制转换成 取值范围小的类型;比较而言,自动转换是Java自动执行的,而强制转换需要我们自己手动执行。
强制转换格式:
数据类型 变量名 = (数据类型)被转数据值;
将 1.5 赋值到 int 类型,代码修改为:
long a = 10; int b = (int)a;
同样道理,当一个 short 类型与 int 类型相加,我们知道会类型提升,但是还想给结果赋值给 short 类型变量,就需要强制转换。
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo04 {
public static void main(String[] args) {
// short类型变量,内存中存储两个字节
short s = 1;
int i = 1;
/*
出现编译错误
s和i做运算的时候,i是int类型,s会被提升为int类型
s+i后的结果是int类型,将结果在赋值会short类型时发生错误
*/
// s = s + i; // 编译失败
s = (short) (s + i); // 强制转换,编译成功
System.out.println(s);
}
}
1.转换图解
short 类型内存占有2个字节,在和 int类型运算时会提升为 int 类型 ,自动补充2个字节,计算后的结果还是 int 类型,但最终被强转为了short类型,占用2个字节空间。

2.浮点数与整数的底层存储
下面是8个基本数据类型的取值范围大小:
| 数据类型 | 关键字 | 内存占用 | 取值范围 |
|---|---|---|---|
| 字节型 | byte | 1个字节 | -128~127 |
| 短整型 | short | 2个字节 | -32768~32767 |
| 整型 | int(默认) | 4个字节 | -2147483648~2147483647 |
| 长整型 | long | 8个字节 | -2的63次方~2的63次方-1 |
| 单精度浮点数 | float | 4个字节 | 1.4013E-45~3.4028E+38 |
| 双精度浮点数 | double(默认) | 8个字节 | 4.9E-324~1.7977E+308 |
| 字符型 | char | 2个字节 | 0-65535 |
| 布尔类型 | boolean | 1个字节 | true,false |
我们可以发现,int和float都是占用4个字节大小,但是float的取值范围却比int要大的多。同样的情况在long与double这两种类型上也存在;
这是为什么呢?
double与long表示范围的差距是由于存储结构的不同导致的,换句话说我们保存1000这个数,我们long要存储+1000,其二进制为001111101000,而double存储的是+1E3。
拆开来看,存储一个数double可以分成三部分:
- 尾数位(1)
- 基数位(10)
- 指数位(3)
也就是用科学计数法来存储,这简单的1000就能看出差距,long存储1000需要符号位(+)和数字位(1000),需要5位。而double存储,却只需要3位。
同样一个数,double能够用更少的内存存储,所以多余的内存就能存储更多的数,因此double > long
通过如下代码可以查看到每个类型的最大值:
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo01 {
public static void main(String[] args) {
// 2147483647
System.out.println(Integer.MAX_VALUE);
// 3.4028235E38
System.out.println(Float.MAX_VALUE);
// 9223372036854775807
System.out.println(Long.MAX_VALUE);
// 1.7976931348623157E308
System.out.println(Double.MAX_VALUE);
}
}
3.整数之间的强制转换
强制转换指的是:取值范围大的,转换为取值范围小的。强制转换的时候尤其要注意转换之后的类型是否能存储之前的数据。
示例1:数值==没有超出==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo05_强制类型转换_整数转换_01 {
public static void main(String[] args) {
int a = 100;
byte b = (byte) a;
// byte也能存储100,因此转换的时候不会出现什么问题
System.out.println(b); // 100
}
}
示例2:数值==超出了==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo06_强制类型转换_整数转换_02 {
public static void main(String[] args) {
// 八个字节: 00000000 00000000 0000000 10000100 01000000 11000000 11000000 10000000 ---> 十进制: 568022057088
long a = 568022057088L;
// 四个字节: 01000000 11000000 11000000 10000000 ---> 十进制: 1086374016
int b = (int) a;
System.out.println(b); // 1086374016
// 两个字节: 11000000 10000000 ---> 十进制: -16256
short c = (short) b;
System.out.println(c); // -16256
// 一个字节: 10000000 ---> 十进制: -128
byte d = (byte) c;
System.out.println(d); // -128
}
}
4.浮点数的强制转换
1) 浮点数转浮点数
示例1:数值==没有超出==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo07_强制类型转换_浮点数转浮点数_01 {
public static void main(String[] args) {
double a = 180.8D;
float b = (float) a;
System.out.println(b); // 180.8
}
}
示例2:数值==超出了==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo07_强制类型转换_浮点数转浮点数_02 {
public static void main(String[] args) {
double a = 3.8E38D;
float b = (float) a;
// float的最大取值范围为: 1.4013E-45~3.4028E+38,无法存储3.8E38,赋值失败
System.out.println(b); // Infinity
}
}
超出最大容量:

2) 浮点数转整数
浮点数强制转换为整数类型时也会出现一定的问题;
示例1:浮点数强制转换为整数,数值==没有超出==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo09_强制类型转换_浮点数转整数_01 {
public static void main(String[] args) {
double a = 18.8D;
// 取值范围: -2的63次方~2的63次方-1
long b = (long) a;
// 取值范围: -2147483648~2147483647
int c = (int) a;
// 取值范围: -32768~32767
short d = (short) a;
// 取值范围: -128~127
byte e = (byte) a;
System.out.println(a); // 18.8
System.out.println(b); // 18
System.out.println(c); // 18
System.out.println(d); // 18
System.out.println(e); // 18
}
}
Tips:浮点数转成整数后将会丢失精度,即小数点后面的通通丢失(注意,并不是四舍五入)
示例2:浮点数强制转换为整数(long和int类型),数值==超出了==转换之后类型的取值范围的情况
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo10_强制类型转换_浮点数转整数_02 {
public static void main(String[] args) {
double a = 5.2E63;
// 取值范围: -2的63次方~2的63次方-1
long b = (long) a;
// 取值范围: -2147483648~2147483647
int c = (int) a;
System.out.println(a); // 5.2E63
System.out.println(b); // 9223372036854775807(long的最大值)
System.out.println(c); // 2147483647(int的最大值)
}
}
Tips:当强制超出该类型的范围时:如果是转换为long或int类型,默认为该类型的最大取值范围值
执行结果:

示例3:浮点数强制转换为整数(short和byte类型),数值==超出了==转换之后类型的取值范围的情况
在Java中,将double或float转换为short/byte时,数值将首先转换为int,然后转换为short/byte,如果该数组超出了int的取值范围,那么转换为short或者byte时结果为-1,如果没有超出int范围,那么如果转换为short将会截取2个字节,转换为byte将会截取1个字节。
示例代码:
/**
* @author lscl
* @version 1.0
* @intro:
*/
public class Demo11_强制类型转换_浮点数转整数_03 {
public static void main(String[] args) {
/*
int的取值范围(4个字节): -2147483648~2147483647
short取值范围(2个字节): 取值范围: -32768~32767
byte取值范围(1个字节): -128~127
*/
// 超出了int范围,直接为-1
System.out.println((short) 2147483648.8D); // -1
// 在int范围内,截取两个字节
// 01111111 11111111 11111111 11111100 ---> 2147483644
// 11111111 11111100 ---> -4
System.out.println((short) 2147483644.4D); // -4
// 在int范围内,截取两个字节
// 00000000 00001100 01101110 10110110 ---> 814774
// 01101110 10110110 ---> 28342
System.out.println((short) 814774.8D); // 28342
// 超出了int范围,直接为-1
System.out.println((byte) 2147483648.8D); // -1
// 在int范围内,截取1个字节
// 01111111 11111111 11111111 11111100 ---> 2147483644
// 11111100 ---> -4
System.out.println((byte) 2147483644.4D); // -4
// 在int范围内,截取1个字节
// 00000000 00001100 01101110 10110110 ---> 814774
// 10110110 ---> -74
System.out.println((byte) 814774.8D); // -74
}
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
