The constructor Short(short) has been deprecated解决方法

The constructor Short(short) has been原因

使用Java编写程序实例化一个包装类Short类型的对象,或其它,比如Long、Byte、Integer等类型的对象的时候,集成开发环境IDE或Java的编译器会给出错误的提示,比如:The constructor Short(short) has been deprecated since version 9 and marked for removal,意思就是,Short(short)或其它类似的(比如Long(long),Byte(byte)等等)的构造器在Java 9的版本中已经被弃用了。

据资料介绍,Java9之后,鼓励采用Java编译器自动装箱的方法来创建相应的包装类对象,比如将基本数据类型的值直接赋值给包装类变量,当然还有其它的方法,比如传参等等。

解决方法

除了直接将基本类型的值赋值给包装类变量以创建包装类对象的方法之外(装箱的方法可参考下方实例代码),也可以使用如下语法进行创建,伪代码如下:

包装类 包装类变量 = 包装类.valueOf( 基本类型值 )

实例代码

下面将通过上述两种方法来创建Long包装类的对象,并通过instanceof来检验:

public class test {
    public static void main(String[] args) {
        Long x1 = Long.valueOf(15L);
        boolean t1 = x1 instanceof Long;
        // 装箱
        Long y1 = 6L;
        boolean t2 = y1 instanceof Long;
        System.out.println(t1);
        System.out.println(t2);
    }

}

免责声明:内容仅供参考,不保证正确性!


全栈后端 / Java教程 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.