gpt4 book ai didi

What are rules in casting primitive types in expressions in Java? [duplicate](在Java中,在表达式中强制转换原始类型的规则是什么?[副本])

转载 作者:bug小助手 更新时间:2023-10-25 14:50:38 25 4
gpt4 key购买 nike




I'm trying to figure out why some castings work and why others do not. Can you explain to me why some of these examples work and others don't?

我在试着弄清楚为什么有些铸件能用,为什么有些不能。你能给我解释一下为什么这些例子有的管用,有的不管用?


Setup:

设置:


I'm using jshell.

我使用的是jshell。


byte one = 1;
one ==> 1

Example 1:

例1:


jshell> byte val = 200 - 200;
val ==> 0

According to my knowledge these literals are of type int, but code compiles because the result fits in the byte.

据我所知,这些文字是int类型的,但代码会编译,因为结果适合字节。


Example 2

实施例2


jshell> byte val = 1 - one;
| Error:
| incompatible types: possible lossy conversion from int to byte
| byte val = 1 - one;
| ^-----^

When I'm switching one literal to a variable, code suddenly does not compile. Casting any or all of components of the espression doesn't fix the problem.

当我将一个文字切换为一个变量时,代码突然不能编译。强制使用Espress的任何或所有组件并不能解决问题。


Example 3

示例3


jshell> byte val = (byte)(1) - one;
| Error:
| incompatible types: possible lossy conversion from int to byte
| byte val = (byte)(1) - one;
|

I expected this to work. After all I did convert the integer in this expression to byte, so there shouldn't be 'incompatible types' error.

我原以为这能行得通。毕竟我确实把这个表达式中的整数转换成了字节,所以不应该有‘类型不兼容’的错误。


Example 4

示例4


jshell> byte val = 1 - (byte)(one);
| Error:
| incompatible types: possible lossy conversion from int to byte
| byte val = 1 - (byte)(one);
| ^-------------^

This was more an experiment to see if this would fix the problem. I didn't expect this to work.

这在更大程度上是一个实验,看看这是否能解决问题。我没想到这会奏效。


Example 5

例5


jshell> byte val = (byte)(1) - (byte)(one);
| Error:
| incompatible types: possible lossy conversion from int to byte
| byte val = (byte)(1) - (byte)(one);
| ^---------------------^

This one confuses me so much. I'm casting both components to byte, so why would there be an error? After all in Example 1 there was a similar operation and Java didn't complain at all.

这件事让我很困惑。我将两个组件都转换为字节,所以为什么会有错误?毕竟,在示例1中有一个类似的操作,Java完全没有抱怨。


Example 6

例6


jshell> byte val = (byte)(1 - one);
val ==> 0

In the end only casting result of the expression doesn't produce an error. Why?

最后,只有表达式的强制转换结果不会产生错误。为什么?


更多回答

Because of automatic promotion of your byte operands to type int, and the results of the - operations therefore being of type int.

因为字节操作数自动升级为int类型,因此-运算的结果为int类型。

You need to read the 2nd duplink to understand why byte val = 200 - 200; is allowed. (And it isn't a bug in jshell. The JLS says it is valid.)

您需要阅读第二个双链接才能理解为什么允许字节val=200-200;。(而且它不是jshell中的错误。JLS表示这是有效的。)

The critical thing here is that your variable isn't final, thus its value is not considered a constant expression, even if you never change its value once assigned.

这里的关键是,您的变量不是最终变量,因此它的值不被视为常量表达式,即使您在赋值后永远不会更改它的值。

优秀答案推荐

Java automatically promotes each byte, short, or char operand to int when evaluating an expression. Because the operands are automatically promoted to int when the expression is evaluated, the result also gets promoted to int. Thus, the result of the expression is now of type int, which cannot be assigned to a byte without the use of a cast.

在计算表达式时,Java会自动将每个字节、短或字符操作数提升为int。因为在计算表达式时,操作数会自动提升为int,所以结果也会提升为int。因此,表达式的结果现在是int类型,如果不使用强制转换,则不能将其赋给字节。


更多回答

You'll need to expand a bit to explain why byte val = 200 - 200; is allowed.

您需要稍微扩展一下才能解释为什么允许byte val=200-200;。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com