用户:
Godeyes查看:2 回复:9 评论:2 创建时间:2020-05-09T07:54:13
哈喽,大家好,我是敲击小萌新---Godeyes
今天我们来继续学习java



基本数据类型之间是存在固定的转换规则的,现总结出以下 6 条规则, 无论是哪个程序,
将这 6 个规则套用进去,问题迎刃而解:
八种基本数据类型中,除 boolean 类型不能转换,剩下七种类型之间都可以进行转换;
如果整数型字面量没有超出 byte,short,char 的取值范围,可以直接将其赋值给
byte,short,char 类型的变量;
小容量向大容量转换称为自动类型转换,容量从小到大的排序为:byte < short(char) <
int < long < float < double,其中 short 和 char 都占用两个字节,但是 char 可以表示更大
的正整数;
大容量转换成小容量,称为强制类型转换,编写时必须添加“强制类型转换符”,但
运行时可能出现精度损失,谨慎使用;
byte,short,char 类型混合运算时,先各自转换成 int 类型再做运算;
多种数据类型混合运算,各自先转换成容量最大的那一种再做运算;
接下来,根据以上的 6 条规则,我们来看一下以下代码,指出哪些代码编译报错,以及怎么解决(大家注意看代码的注释信息):
public class TypeConversionTest {
public static void main(String[] args) {
byte b1 = 1000;
byte b2 = 20;
short s = 1000;
int c = 1000;
long d = c;
int e = d;
int f = 10 / 3;
long g = 10;
int h = g / 3;
long m = g / 3;
byte x = (byte)g / 3;
short y = (short)(g / 3);
short i = 10;
byte j = 5;
short k = i + j;
int n = i + j;
char cc = 'a';
System.out.println("cc = " + cc);
System.out.println((byte)cc);
int o = cc + 100;
System.out.println(o);
}
}
编译报错,错误信息如下所示:
如何修改,请看以下代码:
public class TypeConversionTest {
public static void main(String[] args) {
//1000 超出 byte 取值范围,不能直接赋值
//byte b1 = 1000;
/*
* 如果想让上面程序编译通过,可以手动强制
* 类型转换,但程序运行时会损失精度
*/
byte b1 = (byte)1000;
//20 没有超出 byte 取值范围,可以直接赋值
byte b2 = 20;
//1000 没有超出 short 取值范围,可以直接赋值
short s = 1000;
//1000 本身就是 int 类型,以下程序不存在类型转换
int c = 1000;
//小容量赋值给大容量属于自动类型转换
long d = c;
//大容量无法直接赋值给小容量
//int e = d;
//加强制类型转换符
int e = (int)d;
//int 类型和 int 类型相除最后还是 int 类型,所以结果是 3
int f = 10 / 3;
long g = 10;
/*
* g 是 long 类型,long 类型和 int 类型最终结果是 long 类
* 型,无法赋值给 int 类型
*/
//int h = g / 3;
//添加强制类型转换符
int h = (int)(g / 3);
//long 类型赋值给 long 类型
long m = g / 3;
/*
* g 先转换成 byte,byte 和 int 运算,最后是 int 类型,
* 无法直接赋值给 byte
*/
//byte x = (byte)g / 3;
//将以上程序的优先级修改一下
byte x = (byte)(g / 3);
short y = (short)(g / 3);
short i = 10;
byte j = 5;
/*
* short 和 byte 运算时先各自转换成 int 再做运算,结
* 果是 int 类型,无法赋值给 short
*/
//short k = i + j;
int n = i + j;
char cc = 'a';
System.out.println("cc = " + cc);
//将字符型 char 转换成数字,'a'对应的 ASCII 是 97
System.out.println((byte)cc);
/*
* char 类型和 int 类型混合运算,char 类型先转换成
* int 再做运算,最终 197
*/
int o = cc + 100;
System.out.println(o);
}
}
运行结果如下图所示:
通过本小节的学习,大家主要掌握基本数据类型转换的 6 条规则,以后遇到类似的问题直接套用规则即可。
GodeyesJava学习教程(第一章)[介绍java]:https://shequ.codemao.cn/community/281707
java学习教程(第二章)[java的注释]:https://shequ.codemao.cn/community/281722
Java(第三章)[编写第一个java程序]:https://shequ.codemao.cn/community/281753
Java(特殊篇)[运行和编译java程序]:https://shequ.codemao.cn/community/281785
Java学习教程(第四章)[标识符与关键字]:https://shequ.codemao.cn/community/282020
Java学习教程(第五章)[变量]{1.字面量}:https://shequ.codemao.cn/community/282531
Java学习教程(第五章)[变量]{变量概述}:https://shequ.codemao.cn/community/283059
Java学习教程(第五章)[变量]{使用变量}:https://shequ.codemao.cn/community/283194
[教程帖]Java学习教程第五章[变量]{变量分类}:https://shequ.codemao.cn/community/283732
[教程帖]Java学习教程(第五章)[变量]{变量作用域和章节总结}:https://shequ.codemao.cn/community/283745
[教程帖]Java学习教程(第六章)[数据类型]{数据类型概述}:https://shequ.codemao.cn/community/284512
[教程帖]Java学习教程(第六章)[数据类型]{字符编码}:https://shequ.codemao.cn/community/285286
[教程帖]Java学习教程(第六章)[数据类型]{字符型详解 理解}:https://shequ.codemao.cn/community/286136
[教程帖]Java学习教程(第六章)[数据类型]{整数型详解(理解)}:https://shequ.codemao.cn/community/289851
[教程帖]Java学习教程(第六章)[数据类型]{布尔型详解(理解)}:https://shequ.codemao.cn/community/290606
[教程帖]Java学习教程(第六章)[数据类型]{ 浮点型详解(理解)}:https://shequ.codemao.cn/community/291409
[教程帖]Java学习教程(第六章){基本数据类型转换 理解}:https://shequ.codemao.cn/community/292208
点赞0
评论
Unsignesd再给大家来一个双面赋值
public class elloworld {
static byte m = 127;
static int M = m;
static int l = 126;
static byte L = (byte) l;
static long h = l;
public static void main(String [] args ) {
System.out.println(m +" "+M+" "+l+" "+L +" "+h);
}
}点赞0
评论