what is diff b/w null string and empty string

Posted by Yugandhar under Interview Questions on 9/1/2012 | Points: 10 | Views : 1458 | Status : [Member] | Replies : 5
what is diff b/w null string and empty string
char str1[]=NULL and char str2[]="";
what is diff b/w str1 and str2 ??




Responses

Posted by: Gopesh9 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
See, Null means string doesn't have any value. An empty string is a value, but is just empty. Null has no bounds that means it can be used for string, integer, date.. Empty string is just regarding a string with length equals to zero.

Hope this will help you...

G. S.
.Net Developer

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 9/3/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Empty string is allocated a memory location with value "" and the NULL value is just a pointer pointing nowhere in the memory.
Empty - It has empty value.
NULL - It has no value.
For eg :

String str = "";
str.lentgh; // return 0


String str = null;
str.lentgh; //returns a null pointer exception



Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
char str1[]=NULL
String str = null;

str.lentgh;

null means it will raise null pointer exception means we cant leave a field empty after defing it as not null
char str2[]=""; String str="";
str.length; output.........0


Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: San.Pblr.Gct on: 9/3/2012 [Member] Starter | Points: 25

Up
0
Down
string str=null //this will not create an object. In stack memory will be allocated for str but since its null object, no memory allocation in heap.
string str=""; //this will create an object and allocate it in heap with value "";

moreover null is NOT a value. It is a state indicating that the object value is unknown or nonexistent. It is not zero or blank or an "empty string" and it does not behave like any of these values.

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/3/2012 [Member] Bronze | Points: 25

Up
0
Down
In .Net pre 2.0, "" creates an object while String.Empty creates no object. So it is more efficient to use String.Empty.
.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.
So "" is pretty equivalent to .Empty, but still not as fast as .Length == 0.

Yugandhar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response