Wednesday, 15 May 2013

casting - What is the cleanest way to compare an int with a potentially null Integer in Java? -



casting - What is the cleanest way to compare an int with a potentially null Integer in Java? -

map<string,integer> m; m = new treemap<string,integer>();

is practice add together next cast avoid null pointer exception when m.get() null.

system.out.println( ((integer) 8).equals( m.get("null") ) ); // returns false

alternatively prior null check starts bit ugly.

system.out.println( m.contains("null") != null && m.get("null").equals( 8 ) );

is there improve way write this? thanks.

i seek avoid casts whenever possible, i'd rather utilize following, looks nicer in opinion:

integer.valueof(8).equals(m.get("null"))

java casting nullpointerexception

No comments:

Post a Comment