Java整数转短整数的规则

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

Java integer to short conversion rules

问题

在代码中,只有第一个情况(整数变量到short)会出现编译错误(从int到short的丢失转换)。为什么其他情况(整数文字到short和最终整数变量到short)没有相同的编译错误?

public class Test
{
    public static void main(String[] args)
    {
        short shortNum = 0;

        //整数变量到short
        int intNum = 12;
        shortNum = intNum;

        //整数文字到short
        shortNum = 12;

        //最终整数变量到short
        final int finalNum = 12;
        shortNum = finalNum;
    }
}

只有第一个情况会出现编译错误,因为在这种情况下,从int到short的转换是有损失的,可能导致数据丢失。而在其他两种情况下,编译器能够确定没有数据丢失,因此没有编译错误。在第二种情况中,整数文字12可以直接赋值给short,因为它在short的范围内。在第三种情况中,虽然final int finalNum被声明为int,但由于它是final的,它的值在编译时就已知,并且可以安全地分配给short。

英文:

In the code, only the first case(integer variable to short) has a compile error (lossy conversion from int to short). Why don't the other cases (integer literal to short and final integer variable to short) have this same compile error?

public class Test
{	
	public static void main(String[] args)
	{
		short shortNum = 0;
		
		//integer variable to short
		int intNum = 12;
		shortNum = intNum;
		
		//integer literal to short
		shortNum = 12;

		//final integer variable to short
		final int finalNum = 12;
		shortNum = finalNum;
	}
}

答案1

得分: 1

short shortNum = 0;

//将整数变量转换为short
int intNum = 12;
shortNum = (short)intNum; //这不起作用,因为在从较大的数值数据类型转换为较小的数值数据类型时,需要进行类型转换。

//将整数文字转换为short
shortNum = 12; //这不是整数文字本身,你正在用12初始化你的short变量,它适合short数据类型,所以它是short。

//将最终整数变量转换为short
final int finalNum = 12; //你的变量是final的,根据Java语言规范,如果final变量的值适合你要转换的类型,那么不需要显式转换。
shortNum = finalNum;

英文:

See the answer as comments:

short shortNum = 0;

//integer variable to short
int intNum = 12;
shortNum = intNum; //doesn't work, because casting primitives is required any time you are going from a larger numerical data type to a smaller numerical data type.
        
//integer literal to short
shortNum = 12; //This is not integer literal per se, you're initializing your short variable with 12, which fits in short data type, so it's short.

//final integer variable to short
final int finalNum = 12; //your variable is final, and according to Java Language Specification, if final variable's value fits into the type you're casting it, then no explicit cast is needed.
shortNum = finalNum;

答案2

得分: 0

答案在这个帖子中:https://stackoverflow.com/questions/24716899/type-cast-issue-from-int-to-byte-using-final-keyword-in-java。

如果Java知道这个数字是一个常量并且适合另一种类型,那么你可以使用它。

对于非常量值的情况,你无法知道变量将如何演变...在这种情况下,为了保护,它不会允许类型转换。

英文:

The answer is in this post: https://stackoverflow.com/questions/24716899/type-cast-issue-from-int-to-byte-using-final-keyword-in-java.

If Java knows that the number is a constant and fits in another type, then you can use it.

In the case of a non-constant value, you can't know how the variable will evolve... In this case, for protection, it will not allow a type change.

huangapple
  • 本文由 发表于 2020年7月31日 00:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177322.html
匿名

发表评论

匿名网友

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

确定