Good morning,
This is for the newbies:
When you're developing code and you need to test if a String object is truly empty, be careful. Here's an example:
class IsStringTrulyEmpty {
public static void main(String[] args) {
//Is string truly empty?
String s = " ";
String t = "";
//Test
System.out.println("Is s truly empty? " + s.isEmpty());
System.out.println("Is t truly empty? " + t.isEmpty());
}
}
Here is the output when run:
Is s truly empty? false
Is t truly empty? t rue
Notice that s returned false. Why? There is a space between the quotation marks. This may have an impact, because if you're checking if a string is empty, it will never be true if there's a space.
It truly is all in the details.
Happy coding!
No comments:
Post a Comment