Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between Boolean.TRUE and true

Tags:

java

wrapper

Firstly, I have

private Map<String, Boolean> mem = new HashMap<String, Boolean>();

And then:

  if (wordDict.contains(s) ||  Boolean.TRUE==mem.get(s)) {
        return true;
    }

why can't I use "mem.get(s)==true" in the if statement. There will be a error "Line 6: java.lang.NullPointerException"

I think I still cannot understant wrapper class well. Please give me some guidance. Thank you!

like image 915
beepretty Avatar asked Aug 02 '15 19:08

beepretty


1 Answers

if mem.get(s) is null and will be compared with a primitive boolean value, java will make autoboxing. It means it will call null.booleanValue().

Thats why you get a NPE.

like image 123
Jens Avatar answered Oct 20 '22 21:10

Jens