When you write
String st1 = null;
You create a reference “str1″ to an object that still does not exist in the heap, but when you type :
String st2 = "" ;
You create a reference “str2″ to a string object that is already allocated in the heap which preserves the string object size.
if you do that expression
str1 == str2 ;
It should evaluate to false, because you are comparing reference values, which will not be equal.
if you do that expression
if(str1.equals(str2) ){ }
It should throw a NullPointerException because you are trying to access the method equals on a null object ( str1 )
but if you try that expression
if(str2.equals(str1) ) {}
It should return false, because the value of ( str2 ) is an empty string not null object.
String st1 = null;
You create a reference “str1″ to an object that still does not exist in the heap, but when you type :
String st2 = "" ;
You create a reference “str2″ to a string object that is already allocated in the heap which preserves the string object size.
if you do that expression
str1 == str2 ;
It should evaluate to false, because you are comparing reference values, which will not be equal.
if you do that expression
if(str1.equals(str2) ){ }
It should throw a NullPointerException because you are trying to access the method equals on a null object ( str1 )
but if you try that expression
if(str2.equals(str1) ) {}
It should return false, because the value of ( str2 ) is an empty string not null object.
0 comments:
Post a Comment