Java 运算符,+ - 下面的代码发生了什么,为什么要这样写
Java Operators, + - what is happening in the below code and why it is written like that
在我们的java代码中,我遇到了一行我不明白为什么
总计 = + - valFromsp; 或 总计 = - + valFromsp;
所以我写了个小程序贴在这里
public class Test {
public static void main (String... arg) {
int total = 20;
int valFromsp = 60 ;
total = + - valFromsp;
System.out.println(total); // prints -60
}
}
就是这个意思:
total = -valFromsp;
这行代码是一个赋值语句,右边的表达式是+ - valFromsp
。 + - valFromsp
是什么意思?如果我们加上括号,它就变成了 +(-(valFromsp))
.
一元运算符 -
对操作数 valFromsp
进行运算,使其成为 -60
。然后 +
一元运算符对 -60
不做任何操作。
+
和 -
一元运算符在 Java 语言规范的 §15.15 中指定:
The operators +, -, ++, --, ~, !, and the cast operator (§15.16) are
called the unary operators.
UnaryExpression:
PreIncrementExpression
PreDecrementExpression
+ UnaryExpression
- UnaryExpression
UnaryExpressionNotPlusMinus
+
一元运算符的使用在 §15.15.3 中进一步指定:
Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.
但是由于您使用的是 int
s,它不会进行一元数字提升,因此 +
什么都不做。即使您使用 byte
、short
或 char
,+
仍然不会执行任何操作,因为 -
一元运算符也进行提升。所以确实没有任何理由同时使用 +
和 -
。
我建议您将其更改为:
total = -valFromsp;
以免以后混淆。
让我开始用一个简单的例子来解释这个,考虑一下
total = + valFromsp;
the above line means total = total + valFromsp;
类似于这个total = - + valFromsp
的意思
total = - (total + valFromsp);
现在您可以再次将其进一步扩展为 total = total - (total + valFromsp)
consider below example which gives same output
int total = 10;
int val = 50;
//total = - + val; //this is also equals to the below line
total = total - (total + val);
System.out.println(total); //output -50.
在我们的java代码中,我遇到了一行我不明白为什么
总计 = + - valFromsp; 或 总计 = - + valFromsp;
所以我写了个小程序贴在这里
public class Test {
public static void main (String... arg) {
int total = 20;
int valFromsp = 60 ;
total = + - valFromsp;
System.out.println(total); // prints -60
}
}
就是这个意思:
total = -valFromsp;
这行代码是一个赋值语句,右边的表达式是+ - valFromsp
。 + - valFromsp
是什么意思?如果我们加上括号,它就变成了 +(-(valFromsp))
.
一元运算符 -
对操作数 valFromsp
进行运算,使其成为 -60
。然后 +
一元运算符对 -60
不做任何操作。
+
和 -
一元运算符在 Java 语言规范的 §15.15 中指定:
The operators +, -, ++, --, ~, !, and the cast operator (§15.16) are called the unary operators.
UnaryExpression: PreIncrementExpression PreDecrementExpression + UnaryExpression - UnaryExpression UnaryExpressionNotPlusMinus
+
一元运算符的使用在 §15.15.3 中进一步指定:
Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary plus expression is the promoted type of the operand. The result of the unary plus expression is not a variable, but a value, even if the result of the operand expression is a variable.
但是由于您使用的是 int
s,它不会进行一元数字提升,因此 +
什么都不做。即使您使用 byte
、short
或 char
,+
仍然不会执行任何操作,因为 -
一元运算符也进行提升。所以确实没有任何理由同时使用 +
和 -
。
我建议您将其更改为:
total = -valFromsp;
以免以后混淆。
让我开始用一个简单的例子来解释这个,考虑一下
total = + valFromsp;
the above line means
total = total + valFromsp;
类似于这个total = - + valFromsp
的意思
total = - (total + valFromsp);
现在您可以再次将其进一步扩展为 total = total - (total + valFromsp)
consider below example which gives same output
int total = 10;
int val = 50;
//total = - + val; //this is also equals to the below line
total = total - (total + val);
System.out.println(total); //output -50.