Java 中赋值表达式的计算结果是什么? [英] What does an assignment expression evaluate to in Java?

查看:34
本文介绍了Java 中赋值表达式的计算结果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中遇到了一个语句

I encountered a statement in Java

while ((line = reader.readLine()) != null) {
    out.append(line);
}

Java 中的赋值操作如何返回值?

How do assignment operations return a value in Java?

我们正在检查的语句是 line = reader.readLine() 并将其与 null 进行比较.

The statement we are checking is line = reader.readLine() and we compare it with null.

既然 readLine 会返回一个字符串,那么我们究竟是如何检查 null 的?

Since readLine will return a string, how exactly are we checking for null?

推荐答案

Java 中的赋值运算符计算分配的值(就像它在 ).所以这里会执行readLine(),其返回值保存在line中.然后根据 null 检查存储的值,如果它是 null,则循环将终止.

The assignment operator in Java evaluates to the assigned value (like it does in, e.g., c). So here, readLine() will be executed, and its return value stored in line. That stored value is then checked against null, and if it's null then the loop will terminate.

这篇关于Java 中赋值表达式的计算结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆