Java中=+运算符的使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19151388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of =+ operator in Java?
提问by Gokul Nath KP
I've the following code:
我有以下代码:
public class Operators {
public static void main(String[] args) {
int i =+ 2;
System.out.println(i);
}
}
Upon executing I'm getting the following output: 2
执行后,我得到以下输出:2
So what does =+
operator actually does here?
那么=+
运算符在这里实际上是做什么的呢?
EDIT:
编辑:
As some answered, it is assigning +2
to i
, consider the following code:
正如一些人回答的那样,它正在分配+2
给i
,请考虑以下代码:
public class Operators {
public static void main(String[] args) {
int i =- -2;
System.out.println(i);
}
}
So in above case, output should be -2
. But I'm getting 2
所以在上面的情况下,输出应该是-2
. 但我得到2
So I suppose, it is -(-2)
, which gives 2
. Right?
所以我想,它是-(-2)
,它给出了2
。对吗?
采纳答案by Subhrajyoti Majumder
int i =+ 2;
It is positive 2(+2) assignment to variable i. It is more miningful or understandable if your write like -- int i = +2;
对变量 i 进行正 2(+2) 赋值。如果你写的像——int i = +2;
One more example -
再举一个例子——
int i = 2;
i=+3;
System.out.println(i);
It prints 3.
它打印 3。
+
Unary plus operator; indicates positive value (numbers are positive without this, however)
+
一元加运算符;表示正值(然而,没有这个数字就是正值)
回答by Red Alert
You are setting i equal to +2, which is what you got. What kind of output are you expecting?
您将 i 设置为 +2,这就是您得到的。你期待什么样的输出?
回答by devnull
Upon saying:
在说:
int i =+ 2;
+
acts as a unary operator.
+
充当一元运算符。
To elaborate, it sets i
to a positive value 2
.
详细地说,它设置i
为正值2
。
EDIT: Your update says:
编辑:您的更新说:
int i =- -2;
produces 2. Why?
产生 2. 为什么?
In this case, it implies that i=-(-(2))
. Note that using a unary minus operator might produce unexpectedresults when the value is, say, Integer.MIN_VALUE
.
在这种情况下,它意味着i=-(-(2))
。请注意,当值为 时,使用一元减运算符可能会产生意外结果,例如Integer.MIN_VALUE
。
回答by user2841290
I believe what you mean by =+ is really +=.
我相信您所说的 =+ 确实是 +=。
Your code is assigning the value of +2 (positive 2) to the integer.
您的代码正在为整数分配 +2(正 2)的值。
For example:
例如:
int x =+ 4;
x =+ 8;
Console.WriteLine(x.ToString());
Console.ReadLine();
Will print "8", not 12. This is because you are assigning x to +4 and then +8.
将打印“8”,而不是 12。这是因为您将 x 分配给 +4 然后 +8。
If you are asking about what += does, it is a shorthand to takes the initial variable and add to it.
如果您要问 += 的作用,那么获取初始变量并将其添加到其中是一种简写。
x += 8
is the same as
是相同的
x = x + 8
By changing the previous example form =+ to += give us:
通过将前面的示例形式 =+ 更改为 += 给我们:
int x = 4;
x += 8;
Console.WriteLine(x.ToString());
Console.ReadLine();
Will print "12".
将打印“12”。
回答by Aniket Thakur
Refer to the following image for unary operators.
有关一元运算符,请参阅下图。
Here is an exmaple to understand it.
这是一个理解它的例子。
public class UnaryDemo {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = +x;
System.out.println("+x = " + result);
result = -y;
System.out.println("-y = " + result);
}
}
and the output is
输出是
+x = 10
-y = -20
So think the operator as variable = +value
rather than variable =+ values
. yeah That space makes it more readable.
因此,将运算符视为variable = +value
而不是variable =+ values
。是的,这个空间使它更具可读性。
回答by Aniket Kulkarni
As all others are answered, I want to give the JLS reference.
Answer to your Edit
正如所有其他人的回答一样,我想提供 JLS 参考。
回答您的编辑
int i =- -2;
As specified in jls
按照jls 中的规定
- Unary numeric promotion (§5.6.1) is performed on the operand.
- The type of the unary minus expression is the promoted type of the operand.
- At run time, the value of the unary minus expression is the arithmetic negation of the promoted value of the operand.
- 对操作数执行一元数值提升(第 5.6.1 节)。
- 一元减号表达式的类型是操作数的提升类型。
- 在运行时,一元减表达式的值是操作数提升值的算术否定。
So,
所以,
System.out.println(i); //prints 2
For integer values, negation is the same as subtraction from zero.
对于整数值,否定与从零中减去相同。
Note
笔记
For floating-point values, negation is not the same as subtraction from zero, because if x is +0.0, then 0.0-x is +0.0, but -x is -0.0.
对于浮点值,否定与从零减法不同,因为如果 x 为 +0.0,则 0.0-x 为 +0.0,而 -x 为 -0.0。
Unary minus merely inverts the sign of a floating-point number. Special cases of interest:
一元减号只是反转浮点数的符号。感兴趣的特殊情况:
If the operand is NaN, the result is NaN. (Recall that NaN has no sign (§4.2.3).)
If the operand is an infinity, the result is the infinity of opposite sign.
If the operand is a zero, the result is the zero of opposite sign.
如果操作数为 NaN,则结果为 NaN。(回想一下 NaN 没有符号(第 4.2.3 节)。)
如果操作数是无穷大,则结果是相反符号的无穷大。
如果操作数为零,则结果为相反符号的零。
Useful links
有用的链接