[英]Why does this not compile
I am trying to play with Java to understand operator precedence. 我正在尝试使用Java来了解运算符的优先级。 Based on what I have read I think the following code should work:
根据我阅读的内容,我认为以下代码应该有效:
System.out.println(("" + (1--2)));
I believe it should be evaluated in this order: 我认为应该按以下顺序进行评估:
So I expect it to print 3 but it does not appear to be legal code. 因此,我希望它可以打印3,但它似乎不是合法代码。 Can someone explain where I am going wrong with this?
有人可以解释我在这方面出了什么问题吗?
You need to type it like so: 您需要这样输入:
System.out.println(("" + (1-(-2))))
"--" is not recognized as a valid operator in this instance so it's causing a compiler error. 在这种情况下,“-”未被识别为有效的运算符,因此会导致编译器错误。 Also, it will return 3, not -1.
同样,它将返回3,而不是-1。
Edit: As mentioned, another way to type this is with a space in between the "-" like so: 编辑:如上所述,另一种键入方式是在“-”之间有一个空格,如下所示:
System.out.println(("" + (1 - -2)))
Java reads --
as a decrement operator, meaning that it needs to be attached to a variable for proper syntax, not next to a literal. Java读
--
作为减量运算符,这意味着它需要附加到变量以获取正确的语法,而不是紧靠文字。 You can fix this by simply putting a space in between the two -
symbols, ie System.out.println(("" + (1- -2)));
可以通过简单地将一个空间,在这两者之间确定这
-
码元,即System.out.println(("" + (1- -2)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.