This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
HERE the second element in the equalsIgnoreCase and equals is null and the first element has value,but it does not throw NullPointerException Why it does behave like this?
it returns me values.
How do I compare two values?
If one is null and other has values,if these are assigned at run time ; how do I compare with equalsIgnoreCase and equals() ?
Apparently null is different from any other value,so I HAVE TO EXPECT null and other values in both elements. This happends for only first element.
How do I write a refined code for comparing two values considering or expecting null ?
Find the java.util.Objects class, which has equals() methods overloaded to take two parameters, and can cope with null values. Beware: it is only available in Java7. Or you can use an if (x != null) ... test.
You should not call equals() method using the reference which points to "null".
In the first case yo will get NullPointerException obviously. but the second approach is correct. see the java SE API.