[英]Why isn't value of my variable updated after every multiplication?
Why isn't value of my variable poc
updated after every multiplication done within System.out.println();
为什么在System.out.println();
完成每次乘法后,我的变量poc
值没有更新System.out.println();
? ?
public static void main(String[] args) {
byte poc = 0b0001;
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
System.out.println("The value of byte " + poc + " when multiplied by 0010 is: " + poc*0b0010);
}
ps This is not homework. ps这不是家庭作业。 I'm new to Java, and I'm trying to grasp the concept of pass by value. 我是Java的新手,我正在尝试掌握按值传递的概念。 I do not understand why isn't value of poc
updated every time it is multiplied within println
statement. 我不明白为什么每次在println
语句中乘以poc
值时都不会更新。 For example, in the following code variable poc
is updated every time. 例如,在下面的代码中,变量poc
每次都会更新。
public static void main(String[] args) {
byte poc = 0b0001;
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
System.out.println("The value of byte " + poc + " when incremented by 1 is: " + ++poc);
}
Why is updated in the second piece of code, but not in the first one? 为什么在第二段代码中更新而不在第一段代码中更新?
This really has nothing to do with pass-by-value † 这确实与传递值无关†
poc*0b0010
is not a statement that assigns the result of poc*0b0010
to poc
, it is just an expression which returns the result of the multiplication. poc*0b0010
不是分配结果的声明poc*0b0010
到poc
,它只是返回的乘法结果的表达式。
++poc
is an expression that both increments poc
(assignment) and returns the result of the assignment. ++poc
是一个既增加poc
(赋值)又返回赋值结果的表达式。
Note that you can use *=
( a compound assignment operator ) to do this by surrounding poc*0b0010
in brackets to create a Parenthesized Expression within the string concatenation: 请注意,您可以使用*=
( 复合赋值运算符 )通过将poc*0b0010
在方括号中来在字符串连接内创建括号化表达式来执行此操作 :
System.out.println("The value of byte " + poc
+ " when multiplied by 0010 is: " + (poc*=0b0010));
See The Java Tutorials > Assignment, Arithmetic, and Unary Operators for a full tutorial of arithmetic and assignment. 有关算术和赋值的完整教程,请参见Java教程>赋值,算术和一元运算符 。
Also see JLS: 15.15.1. 另请参见JLS:15.15.1。 Prefix Increment Operator ++ and 15.17.1. 前缀递增运算符++和15.17.1。 Multiplication Operator * 乘法运算符*
† for information about pass-by-value see the section "Passing Primitive Data Type Arguments" in this java tutorial † 有关传递值的信息,请参见本Java教程中的 “传递原始数据类型参数”部分
The reason that this is happening is because poc*0b0010
only returns a value, while ++poc
increments the value of poc
AND then returns the updated value of poc
. 这种情况的发生的原因是因为poc*0b0010
只返回一个值,而++poc
增量的价值poc
,然后返回的更新值poc
。
Essentially ++poc
is the shorthand for poc = poc + 1;
本质上, ++poc
是poc = poc + 1;
的简写poc = poc + 1;
Notice how the result of poc + 1
is assigned to poc. 注意如何将poc + 1
的结果分配给poc。
However, poc*0b0010
is just arithmetic being performed & returned, but not assigned to anything. 但是, poc*0b0010
只是正在执行并返回的算术运算,而没有分配任何内容。
If you used poc *= 0b0010
then the expression would be equivalent to poc = poc * 0b0010
. 如果使用poc *= 0b0010
则该表达式将等效于poc = poc * 0b0010
。
Just a final note: This behavior doesn't actually have anything to do with pass-by-value vs pass-by-reference, but instead has to do with assignment and the difference between arithmetic and unary operations. 最后一点:此行为实际上与按值传递与按引用传递无关,而是与赋值以及算术和一元运算之间的区别有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.