为什么在Java中无法将(byte)数据类型强制转换为(short)数据类型?

huangapple go评论79阅读模式
英文:

Why can't i cast (byte) Data Type into (short) Data Type in Java

问题

以下是翻译好的内容:

所以,实际上我还是Java编程语言的新手,还在学习有关数据类型转换的知识。在这种情况下,我在将字节转换为短整型时遇到了问题。你们能帮我吗?

public class Main {
    public static void main(Strings[] args) {
        int number = 2;
        long updateNumber = number + 2;
        System.out.println(updateNumber); //输出:4
    }
}

在这种情况下,我可以将(int)正常地转换为(long)数据类型,因为(long)的数据大小大于(int)。当我尝试将(short)转换为(int)时,情况也是一样的。但是为什么我不能将这个概念应用于将(byte)转换为(short)的数据类型转换呢?尽管(short)的数据大小比(byte)大?

public class Main {
    public static void main(Strings[] args) {
        byte number = 2;
        short updateNumber = number + 2;
        System.out.println(updateNumber); //输出:错误
    }
}

我想这就是我的问题。为给您添麻烦,非常抱歉,谢谢呵呵...

英文:

So, Actually Im still new in Java Programming Language and still learning about casting Data type So in this case, i have a trouble in casting Byte into Short. Can You guys help me??

public class Main {
    public static void main(Strings[] args) {
        int number = 2;
        long updateNumber = number + 2;
        System.out.println(updateNumber); //output : 4
    }
}

So in this case, i can cast from (int) into (long) Data type normally because (long) data size is bigger than (int). it's the same case as when i trying to cast it from (short) into (int). But why can't i use this concept to casting Data Type from (byte) into (short)?? eventhough (short) has Data size bigger than (byte)??

public class Main {
    public static void main(Strings[] args) {
        byte number = 2;
        short updateNumber = number + 2;
        System.out.println(updateNumber); //output : Error
    }
}

I guess thats it.. Sorry for the trouble and Thank youu hehe..

答案1

得分: 3

当我们将两个 byte 类型的变量相加,或者将 byte 类型的变量与 int 类型的变量相加时,结果会自动转换(隐式转换)为 int。因此,如果我们想将加法结果赋值给一个 short 类型的变量,编译器会报错。

但是你可以通过显式转换将其赋值给 short 类型的变量。

public static void main(String[] args) throws MyException {
    byte number = 2;
    //显式转换
    short updateNumber = (short) (number + 2);

    System.out.println(updateNumber); // 输出:错误
}

由于加法的结果可能会溢出,因为这里的 number 变量值可以在运行时更改。因此,如果你将 number 声明为 final,并且数字的加法结果适合于 byte 的范围,那么你就不需要进行显式转换,因为编译器会事先知道加法结果是否可以由 byte 来保存。

public static void main(String[] args) throws MyException {
    final byte number = 2;
    //由于 number 是 final,不需要显式转换
    short updateNumber = number + 2;

    System.out.println(updateNumber); // 输出:错误
}
英文:

When we add two byte type variables or byte type variable with int type variable the result is automatically cast (implicit casting) to int. So the compiler will give error if we want to assign the result of addition to a short type variable.

While you can assign it to short type variable by explicit casting.

public static void main(String[] args) throws MyException {
		byte number = 2;
        //Explicit casting
		short updateNumber = (short) (number + 2);

		System.out.println(updateNumber); // output : Error
}

As the result of the addition can overflow because here number variable value can be changed at runtime. So if you declare number as final and addition of the numbers fits in range of byte then you won't need to do explicit casting because compiler would be knowing in advance weather the result of addition can be hold by byte or not.

public static void main(String[] args) throws MyException {
        final byte number = 2;
        //Explicit casting not needed as number is final
        short updateNumber = number + 2;
    
        System.out.println(updateNumber); // output : Error
 }

huangapple
  • 本文由 发表于 2020年9月7日 12:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定