It does not work directly because java does not allow automatic conversion to boolean. But you can do the following:
String linha = leitor.readLine();
String saida = "";
if ( (linha = leitor.readLine()) != null) {
saida += "\n";
}
Just as in C and C++, the assignment is an expression, whose value is that of the variable that has just been assigned. In this case its type is String
, what is not accepted within a if
. With the set of parentheses, we can isolate the value of the assignment and compare it with null
to find out if the operation occurred as expected.
"It is possible to assign and at the same time perform a comparison in an if clause in Java?" Yes.
– SparK
You would have forgotten only the second question @Spark =)
– GarouDan