Java基础之强制类型转换

显式类型转换

赋值过程中,大类型数据赋值给小类型变量,编译会报错,此时必须通过强制类型转换实现。

固定格式

数据类型 变量名 = (目标数据类型)(数值或变量或表达式);

案例展示:

java 复制代码
package com.briup.chap02;

public class Test082_ExplicitTrans {
    public static void main(String[] args) {
        // 1.数据赋值 强制类型转换
        float f1 = (float)3.14;
        System.out.println(f1);		//3.14

        // 1.数据赋值 强制类型转换
        int size = (int)123L;
        System.out.println(size);	//123

        double len = 178.5;
        // 2.变量赋值 强制类型转换
        int length = (int)len;
        System.out.println(length);	//178

        byte b = 10;
        short s = 5;
        double d = 2.3;
        // 3.表达式赋值 强制类型转换
        float f2 = (float)((b - 5) * s + d);
        System.out.println(f2);		//27.3
    }
}

特殊情况

观察下面代码,思考为什么编译能够成功

java 复制代码
package com.briup.chap02;

public class Test083_SpecialTrans {
    public static void main(String[] args) {
        // 为什么 int -> byte 成功了?
        byte b = 10;
        System.out.println(b);

        // 为什么 int -> short 成功了?
        short s = 20;
        System.out.println(s);

        // 为什么 int -> char 成功了?
        char c = 97;
        System.out.println(c);

        // 为什么 b2赋值成功,b3却失败了?
        byte b2 = 127;
        System.out.println(b2);
        //byte b3 = 128;
        //System.out.println(b3);

        // 为什么 s2赋值成功,s3却失败了?
        short s2 = -32768;
        System.out.println(s2);
        //short s3 = -32769;
        //System.out.println(s3);

        // 为什么 c2赋值成功,c3却失败了?
        char c2 = 65535;
        System.out.println("c2: " + c2);
        //char c3 = 65536;
        //System.out.println(c3);
    }
}

整形常量优化机制

​ 使用整形字面值常量(默认int类型)赋值给其他类型(byte、char、short)变量时,系统会先判断该字面值是否超出赋值类型的取值范围,如果没有超过范围则赋值成功,如果超出则赋值失败。

java 复制代码
public static void main(String[] args) {
    // 为什么 int -> byte 成功了?
    // 字面值常量10,在赋值给b的时候,常量优化机制会起作用:
    //	系统会先判断10是否超出byte的取值范围,如果没有,则赋值成功,如果超出(比如128),则赋值失败
    byte b = 10;
    System.out.println(b);

    // 为什么 int -> short 成功了?
    //	系统会先判断20是否超出short的取值范围,结果是没有,则赋值成功
    short s = 20;
    System.out.println(s);

    //	如果使用-32769赋值,超出short类型取值范围,则赋值失败
    //short s3 = -32769;
    //System.out.println(s3);
}

注意事项:

  • 常量优化机制只适用于常量,如果是变量,则不可以
  • 该优化机制只针对int类型常量,对于long、float、double等常量,则不可以

案例展示:

java 复制代码
public static void main(String[] args) {
   	// 下面3个赋值语句都 编译失败
    byte b = 10L;
    short s = 3.14F;
    int n = 2.3;

    // 下面2个赋值语句 ok
    byte b1 = 2;
    short s2 = 3;

    // 变量赋值或表达式赋值,都编译失败
    byte b3 = s2;
    byte b4 = b1 + s2;
}
相关推荐
咩咩啃树皮6 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
灯澜忆梦7 小时前
GO_并发编程---定时器
开发语言·后端·golang
鱟鲥鳚7 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
-银雾鸢尾-7 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-7 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
大模型码小白8 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司9 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地10 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun31415910 小时前
TCP超时重传机制是为了解决什么问题?
java
段一凡-华北理工大学10 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化