java Java中赋值操作的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38163938/
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
Return value of assignment operation in Java
提问by sunder
I encountered a statement in Java
我在Java中遇到了一个语句
while ((line = reader.readLine()) != null) {
out.append(line);
}
How do assignment operations return a value in Java?
赋值操作如何在 Java 中返回一个值?
The statement we are checking is line = reader.readLine()
and we compare it with null
.
我们正在检查的语句是line = reader.readLine()
,我们将它与null
.
Since readLine
will return a string, how exactly are we checking for null
?
由于readLine
将返回一个字符串,我们究竟如何检查null
?
回答by Mureinik
The assignment operator in Java returns the assigned value (like it does, e.g., in c). So here, readLine()
will be executed, and it's return value stored in line
. That value is then checked against null
, and if it is null
, the loop will terminate.
Java 中的赋值运算符返回分配的值(就像在c 中那样)。所以在这里,readLine()
将被执行,并且它的返回值存储在line
. 然后根据 来检查该值null
,如果是null
,则循环将终止。
回答by ΦXoc? ? Пepeúpa ツ
(line = reader.readLine()) != null
(line = reader.readLine()) != null
means
方法
- the method readLine() is invoked.
- the result is assigned to variable line,
- the new value of linewill be proof against null
- 方法 readLine() 被调用。
- 结果分配给变量line,
- line的新值将证明空值
maybe many operations at once...
可能同时进行多次操作...
回答by Drew Beres
Assignment expressions are evaluated to their assignment value.
赋值表达式被计算为它们的赋值值。
(test = read.readLine())
>>
>>
(test = <<return value>>)
>>
>>
<<return value>>
回答by Roland
The Java? Language Specification 15.26. Assignment Operators
At run time, the result of the assignment expression is the value of the variable after the assignment has occurred.
在运行时,赋值表达式的结果是赋值发生后变量的值。
回答by nPcomp
reader.readLine()
reads and returns a line for you. Here, you assigned whatever returned from that to line and check if the line variable is null or not.
reader.readLine()
为您读取并返回一行。在这里,您将从该返回的任何内容分配给 line 并检查 line 变量是否为空。