为什么会打印 1?
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
System.out.println((byte)+(short)-(int)+(long)-1);
}
}
我们可以混合使用强制转换和 +,-
一元运算符吗?我知道我们可以进行多次强制转换,但为什么不将 + ,-
一元运算符放在中间会产生错误?
最佳答案
你没有加也没有减。那些 + 和 - 运算符是一元符号运算符。
见 documentation在一元运算符部分。
关键序列:
(byte)+(short)-(int)+(long)-1
从右到左计算如下:
the initial value is -1
casting to long (which is still -1)
unary + sign (still -1)
casting to int (still -1)
unary - sign (now the value is 1)
so on (the value remains 1 until the end)
关于java - 为什么混合 + 和 cast 不会在 "+(int)+(long)-1"中产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28582735/