Vijay jai Singh
What is difference in these two statements:
String str = null;
String str = "";
Jesper de Jong
In the first one, you set the variable str to null (which means that it does not point to any object), and in the second one you set it to point to a String object which has no content.
Two very different things.
, Ranch Hand
Seems you are a beginner in Java.No problems we all start somewhere.
I hope you are aware that there are two types of variables in Java
in the code line
int a ;
'a' is a primitive variable as it can refer to the primitive of the type int.
Similarly in the code line
str can contain a reference to a String object .
For your information in Java String is a class in the package java.lang.
So if you do a
String str= null what it does is
Makes a reference variable(the bit holder
for a reference value) named str of the type java.lang.String
and making it equal to null simply means the
variable is not referring to any object.
Simply creates an object of the type java.lang.String with no value in it and assigns the refernce variable str to this object.
, Ranch Hand
str = null;
The variable does not point to any memory location.
The vairable points to some memory location having blank value
Oh the stink of it! Smell my tiny ad!
Thread Boost - a very different sort of advertising
This thread has been viewed 1008 times.
All times above are in ranch (not your local) time.
The current ranch time is
Apr 23, 2018 11:39:24.