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;
}
相关推荐
码农阿豪3 分钟前
Python Flask应用中文件处理与异常处理的实践指南
开发语言·python·flask
岁岁种桃花儿3 分钟前
CentOS7 彻底卸载所有JDK/JRE + 重新安装JDK8(实操完整版,解决kafka/jps报错)
java·开发语言·kafka
csbysj202016 分钟前
AngularJS 模块
开发语言
独好紫罗兰24 分钟前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n31 分钟前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
不会代码的小测试34 分钟前
UI自动化-POM封装
开发语言·python·selenium·自动化
roman_日积跬步-终至千里40 分钟前
【Java并发】Java 线程池实战:警惕使用CompletableFuture.supplyAsync
java·开发语言·网络
毕设源码-钟学长42 分钟前
【开题答辩全过程】以 基于Springboot的扶贫众筹平台为例,包含答辩的问题和答案
java·spring boot·后端
lsx2024061 小时前
C++ 基本的输入输出
开发语言
CodeSheep程序羊1 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展