I came across this code:
for (final String s : myList)
{
s.equalsIgnoreCase(test);
updateNeeded = true;
break;
}
I suspect that this is not what the programmer actually wanted to do. I believe he meant to write something like:
for (final String s : myList)
{
if(s.equalsIgnoreCase(test))
{
updateNeeded = true;
break;
}
}
However, I don’t understand why there is no error in the first code snippet.
s.equalsIgnoreCase(test);
since the method .equalsIgnoreCase(“anoterString”) returns a boolean and it is not being assigned to anything or used within a control flow statement
It’s just a method call. You don’t have to use the result of a method call for anything else.
It’s rarely a good idea to ignore the result of a non-void method (in particular, the return value of
InputStream.readis sometimes ignored when it really shouldn’t be) but the language specification makes no attempt to call this out as a problem.