• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Comparision

 
Lopez Mirinda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can somebody please tell me how this works.

String str = null;

Snippet 1:

if ("String".equalsIgnorecase(str)) {
//
} else {
//
}

Snippet 2:

if (str.equalsIgnorecase("String")) {
//
} else {
//
}

#1 does not throw a Null Ponter Exception where as #2 throws.


TIA,
Tanuja
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1 does not throw a Null Ponter Exception where as #2 throws.

#1 : equalsIgnoreCase allows null values to be passed as a parameter. Its returned value will be true if the argument is not null and it represents an equivalent String ignoring case; false otherwise.
#2 : Trying to call a method on a null object will always throw a NullPointerException
 
Lopez Mirinda
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe.. that solved my doubt.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please UseCodeTags when you post source code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic