The first println
statement outputs the value of getAuthor2()
which is a space character (" "). This is not an empty string. It does not evaluate to a boolean (true/false) because it's a String object. The other lines evaluate to boolean because they are printing the result of comparators and not the string itself.
The seconds println
is not how you compare strings in Java. It compares references to two different strings that both happen to have a space character in them, but they are different references. To see if getAuthor2()
is a single space you should use the .equals
function like this and you should get true
:
System.out.println(samples.get(0).getAuthor2().equals(" "));
The third println
also gives false
because it's not empty. It has a space in it.
Similarly the fourth println
also gives false because it's not null
either.