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;
}
相关推荐
Forget the Dream28 分钟前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式
咩咩觉主35 分钟前
C# &Unity 唐老狮 No.7 模拟面试题
开发语言·unity·c#
大丈夫在世当日食一鲲36 分钟前
Java中用到的设计模式
java·开发语言·设计模式
A-Kamen40 分钟前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
练川42 分钟前
Stream特性(踩坑):惰性执行、不修改原始数据源
java·stream
狂奔小菜鸡1 小时前
Java运行时数据区
java·jvm·后端
trymoLiu1 小时前
SpringBoot 实现 RSA+AES 自动接口解密!
java·spring boot
却道天凉_好个秋1 小时前
c++ 嵌入汇编的方式实现int型自增
开发语言·汇编·c++
ChinaRainbowSea1 小时前
MySQL 索引的数据结构(详细说明)
java·数据结构·数据库·后端·mysql
33三 三like2 小时前
软件工程画图题
java·开发语言·软件工程