java中的类型自动转换机制解析

网友投稿 250 2022-08-21


java中的类型自动转换机制解析

目录类型自动转换机制解析概述数据类型只会自动提升,不能自动降低java中整数默认的数据类型是int类型自动类型转换xnCKN&强制类型转换什么时候会发生类型转换类型转换分类自动类型转换(隐式类型转换)强制类型转换(显式类型转换)

类型自动转换机制解析

概述

自动类型转换也叫隐式类型转换表达式的数据类型自动提升

所有的byte型、short型和char的值将被提升到int型。

如果一个操作数是long型,计算结果就是long型;

如果一个操作数是float型,计算结果就是float型;

如果一个操作数是double型,计算结果就是double型。

数据类型只会自动提升,不能自动降低

int值可以赋值给long、float、double型变量,不能赋值给byte、short、char型变量

对于函数的传参也是一样

当然,在有函数重载的情况下,java编译器会自动选择最匹配的函数进行调用

Java中整数默认的数据类型是int类型

所有长度低于int的类型(byte、short、char)在运算之后结果将会被提升为int型

当然还有以下的这种情况,这种情况是因为我们在进行赋值运算的时候,java编译器可以明确知道运算的结果是否超过byte或short的取值范围,所以 byte a = 1 + 1; 并没有报错。而上面 byte c = a + b; 编译出错的原因是因为a和b均为一个变量,相加的结果是否会超过byte的取值范围编译器并不知道,所以编译器将结果提升为int型了。

小结一下:

当编译器明确知道整数的运算结果没有到达int的表示范围时,byte、short或char类型的运算结果不会被自动提升为int类型当编译器明确知道或不清楚整数的运算结果是否到达int的表示范围时,编译器将会自动将运算的结果转换成int,即使原来是byte、short或char类型。

自动类型转换 & 强制类型转换

什么时候会发生类型转换

答: 赋值 | 运算时 ,两边数据类型不一致时就会发生类型转换

如下:

public class TypeTest {

public static void main(String[] args){

// 运算时发生的隐式类型转换,两整数相除得到的还是一个整数

byte a = 3;

byte b = 4;

int num = a + b;

System.out.println(num); // 7

// 赋值时发生的隐式类型转换

int ch = '0';

System.out.println(ch); // 48

// 运算时发生的强制类型转换

byte a1 = 12;

byte a2 = 12;

byte num1 = (byte)(a1 + a2);

System.out.println(num1); // 24

// 赋值时发生的强制类型转换

short b3 = 1234;

byte a3 = (byte) b3;

System.out.println(a3); // -46

}

}

运行截图:

类型转换分类

自动类型转换强制类型转换

自动类型转换(隐式类型转换)

规则:从小到大 ,低字节向高字节自动提升

顺序:

byte(1字节) – > short(2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)

char (2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)

画图分析:

代码展示:

public class TypeDemo {

public static void main(String[] agrs){

// byte -- > short

byte b1 = 127;

short s1 = b1;

System.out.println(s1); // 127

// short -- > int

short s2 = 30000;

int i = s2;

System.out.println(i); // 30000

// int -- > long

int num = 2100000000;

long lg = num;

System.out.println(num); // 2100000000

// long -- > float

long lg1 = 200000000000000L;

float f1 = lg1;

System.out.println(f1);// 2.00000001E14

// float -- > double

float f2 = 3.14f;

double d1 = f2;

System.out.println(d1); // 3.140000104904175

// char -- > int

char ch = 'a';

int i1 = ch ;

System.out.println(i1); // 97

// char -- > long

char ch1 = 'b';

long lg2 = ch1;

System.out.println(lg2); // 98

// char -- > double

char ch2 = 'c';

double dou = ch2;

System.out.println(dou); // 99.0

// char -- > float

char ch3 = 'd';

float f3 = ch3;

System.out.println(f3); // 100.0

}

}

运行截图:

注意:

byte、short不能和char进行相互转换

代码展示:

public class TypeDemo2 {

public static void main(String[] agrs){

// byte -- > char

byte bt = 127;

char ch = bt;

System.out.println(ch);

// short -- > char

short sh = 12;

char ch1 = sh;

System.out.println(ch1);

}

}

编译错误截图:

float虽然是4个字节,但是float比long表示的数据范围更大。说明数据范围的大小和字节的大小不一定相关

代码展示:

public class TypeDemo3 {

public static void main(String[] agrs){

long lg = 20000000000000L;

float f1 = lg;

System.out.println(f1); // 1.99999997E13

}

}

运行截图:

boolean类型不能参与类型转换

代码展示:

public class TypeDemo4 {

public static void main(String[] agrs) {

boolean flag = 12;

int flag1 = flag;

System.out.println(flag1);

}

}

编译错误截图:

强制类型转换(显式类型转换)

规则:从大到小,高字节向低字节手动强制转换

顺序:

double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > short (2字节)-- > byte(1字节)

double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > char(2字节)

画图分析:

(掌握)格式:目标数据类型 变量名 = (目标数据类型) 变量 | 常量;

代码展示:

public class TypeDemo5 {

public static void main(String[] agrs){

// float -- > long

// final float PI = 3.14f;

// long num = (long) PI; // 3

// float little = 3.14f;

// long num = (long)little; // 3

long num = (long)3.14f;

System.out.println(num);// 3

// double -- > float

// double dou = 3.14;

// float little1 = (float)dou; // 3.14

// float little1 = (float) 3.14d; // 3.14

final double dou = 3.14;

float little1 = (float)dou;

System.out.println(little1); // 3.14

// long -- > int

// long num1 = 2000000000000L;

// int num2 = (int)num1; // -1454759936

// int num2 = (int)2000000000000L; // -1454759936

final long num1 = 2000000000000L;

int num2 = (int)num1;

System.out.println(num2); // -1454759936

// int --> short

// int num3 = 12;

// short num4 = (short)num3; // 12

// short num4 = (short)40000; // -25536

final int num3 = 60;

short num4 = (short)num3;

System.out.println(num4); // 60

// short -- > byte

final short sh = 12345;

byte bt = (byte)sh;

System.out.println(bt); // 57

short sh1 = 78;

bt = (byte) sh1;

System.out.println(bt); // 78

}

}

运行截图:

注意:

强制类型转换有数据丢失,一般不建议使用

代码展示:

public class TypeDemo6 {

public static void main(String[] agrs) {

short a = 1245;

byte b = (byte)a;

System.out.println(b);

}

}

运行截图:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于@Query注解的用法(Spring Data JPA)
下一篇:java中基本数据类型与Object的关系说明
相关文章

 发表评论

暂时没有评论,来抢沙发吧~