Thursday, February 7, 2008

Java Trick : null in string comparison

String str = "null";
 
 
if ( str == "null") {
 System.out.println(" String value is 'null');
} else {
   System.out.println(" String value is not 'null');
}
 
Output:  " String value not 'null' "
 
if ( "null".equals(str)) {
 System.out.println(" String value is 'null');
} else {
   System.out.println(" String value is not 'null');
}
 
Output:  " String value 'null' "
 

No comments: