Created
October 12, 2014 11:39
-
-
Save adamretter/2446618f295cdfb3e458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TernaryNpe { | |
public static void main(String args[]) { | |
final String a = "a"; | |
final String aa = "aa"; | |
final String b = null; | |
final String bb = ""; | |
/* Test Case 1 */ | |
//OK | |
boolean result = b == null ^ bb == null ? | |
false : bb == null ? true : b.equals(bb); | |
//NPE | |
result = | |
a.equals(aa) | |
&& b == null ^ bb == null ? | |
false : bb == null ? true : b.equals(bb); | |
/* Test case 2 */ | |
//OK | |
result = (b == null ^ bb == null) ? | |
false : (bb == null ? true : b.equals(bb)); | |
//NPE | |
result = | |
a.equals(aa) | |
&& (b == null ^ bb == null) ? | |
false : (bb == null ? true : b.equals(bb)); | |
/* Test case 3 */ | |
//OK | |
result = ((b == null ^ bb == null) ? | |
false : (bb == null ? true : b.equals(bb))); | |
//NPE | |
result = | |
(a.equals(aa) | |
&& (b == null ^ bb == null) ? | |
false : (bb == null ? true : b.equals(bb))); | |
System.out.println("RESULT=" + result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correct implementation is:
or for Java 7: