This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 14687
    
  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: 14114
    
  16

Welcome to JavaRanch.

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Comparision