Java基本数据类型转换规则

Java中的基本数据类型转换分为两种:自动类型转换(隐式转换)和强制类型转换(显式转换)。

目录

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

转换发生时期和注意细节:

示例代码:

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

一些练习题


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

自动类型转换:当java程序在进行赋值或者运算时,精度小的数据类型会自动转换为精度大的数据类型。

       转换规则:byte → short → int → long → float → double
                                                    ↑
                                                  char

转换发生时期和注意细节:

  1. 多种类型数据混合运算时,系统首先自动将所有数据转换成容量最大的那种数据类型,然后再计算
  2. 精度小的数据类型赋值给精度大的数据类型时,发生自动类型转换
  3. byte,short和char之间不会相互自动转换,但它三者可以计算,计算时首先转换为int类型
  4. boolean不参与转换
  5. byte i=1; i++和i=i+1这两种看似相似的操作在类型转换方面确实有重要区别,i++ 不会发生类型转换,i = i + 1 会发生类型转换并导致编译错误   (同样的规则适用于所有比int小的整数类型(byte, short, char))
  6. 当把具体数赋给byte,short,char时,1.先判断该值是否在byte,short,char范围内,如果在就可以赋值  (注意给char赋整数时,会转换成对应码值的字符)  2.如果是变量赋值,需要判断类型符不符合转换规则
示例代码:

1.多种类型的数据混合运算时

public static void main(String[] args) {
  int n1=1;//ok
  float f1=n1+1.1;//报错,n1+1.1 => 结果类型为double
  double d1=n1+1.1;//ok
  float f2=n1+1.1F;//ok

  byte b1=1;
  short s1=1;
  byte b2=b1+s1;//报错 结果类型为int
  int i1=b1+s1;//ok

 }

2.精度小的数据赋值给精度大的数据(反过来会报错)

public static void main(String[] args) {
  int i1=1;
  double d1=i1;//ok
  int i2=d1;//报错double ->int

  int i3=1.1;//报错double ->int
  float f1=1.1;//报错double ->float
  float f2=1.1f;//ok

 }

3.byte,short和char类型数据计算

 public static void main(String[] args) {
  byte b1=1;
  short s1=1;
  char c1='a';
  int i1=b1+s1+c1;//ok
  System.out.println(i1);
 }

4.boolean不参与转换

public static void main(String[] args) {
  boolean b1=false;
  int i1=b1;//错误

  int i=0;
  if(i){//错误 int -> boolean
   System.out.println("...");
  }
 }

5.1  i++ 不会发生类型转换

public static void main(String[] args) {
  byte i = 1;
  i++;  // 合法,不会报错
 }

原因:

  • i++是Java中的复合赋值操作(相当于i += 1)
  • Java语言规范特别规定,对于++、--、+=、-=等复合赋值运算符,编译器会自动执行隐式的强制类型转换
  • 相当于编译器自动帮你做了:i = (byte)(i + 1)

5.2  i = i + 1 会发生类型转换并导致编译错误

public static void main(String[] args) {
  byte i = 1;
  i = i + 1;  // 错误 int->byte
 }

原因:

1.根据Java的二元数值提升规则,当byte参与运算时:

  • i(byte类型)会被自动提升为int类型
  • 1(int字面量)已经是int类型
  • 所以i + 1的结果是int类型

2.将int类型赋值给byte变量需要显式强制转换:

  • i = (byte)(i + 1); 这样才能通过编译

6.赋值

public static void main(String[] args) {
  byte b=1;//ok
  byte b1 = 128;//错误 int->byte (超过了byte的范围 -128到127 )
  short s1 = 128;//ok
  byte b2 = s1;//错误 short->byte
 }

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

强制类型转换:将精度大的数据类型转换为容量小的数据类型。使用时要加上强制转换符()。可能造成精度降低或溢出。

代码示例:

 public static void main(String[] args) {
  int i=1;
  byte b1=(byte) i;//ok

  double d=1.1;
  int i1=(int)d;//ok
  System.out.println(i1);
 }

注意:

  1. 当进行数据转换,精度为大——>小,就需要使用强制转换
  2. 强制符号支队域最近的操作数有效,往往使用小括号提升优先级

一些练习题

 public static void main(String[] args) {

  short s=12;      //ok
  s=s-9;            //错误 int ->short

  byte b=12;      //ok
  b=b-9;          //错误 int ->byte

  b= (byte)(b-9);  //ok


  char a='男';
  char a1='女';
  System.out.println(a+a1);    //字符码值+字符码值
  
  byte b1= 1;
  byte b2= b1==1?2:3;//错误 int->byte
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值