aspose file tools*
The moose likes Beginning Java and the fly likes String Comparision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Comparision" Watch "String Comparision" New topic
Author

String Comparision

Lopez Mirinda
Greenhorn

Joined: Apr 11, 2007
Posts: 11
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

#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


[My Blog]
All roads lead to JavaRanch
Lopez Mirinda
Greenhorn

Joined: Apr 11, 2007
Posts: 11
Thanks Christophe.. that solved my doubt.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

Welcome to JavaRanch.

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
 
subject: String Comparision