
Matt R. answered 03/27/19
Java Developer
The first part was great! You do need to check if str!=null first. Because it is short circuit, if this is false, the checking will stop before it tries to check a null string for its value. For the second part, though, you need to use !str.equals(""). Comparing strings with == is unreliable in Java. == checks that they have the same memory address. In Java ,if two strings have the same value, the compiler will try to use only one object for both of them, and then they will have the same memory address. However, you cannot rely on this, as the compiler may use different objects from time to time. Therefore, you should use the equals method. The people who made Java have overridden it to return true if the values in the strings are the same (if the characters and their sequence are the same).
Here is a link on stack overflow that helped me learn:
https://stackoverflow.com/questions/7375427/comparing-two-identical-strings-with-returns-false
Here is the github page of the person who helped me learn by authoring a post on stackoverflow:
https://stackoverflow.com/users/538551/beatgammit
Here is the github page of the person who helped me learn by editing the post on stackoverflow:
https://stackoverflow.com/users/9230526/kale
I as a teacher thank them for being teachers too!