Difference between null and empty string
String s1 = "";
means that the empty String
is assigned to s1
.
In this case, s1.length()
is the same as "".length()
, which will yield 0
as expected.
String s2 = null;
means that (null
) or "no value at all" is assigned to s2
. So this one, s2.length()
is the same as null.length()
, which will yield a NullPointerException
as you can't call methods on null
variables (pointers, sort of) in Java.
Also, a point, the statement
String s1;
Actually has the same effect as:
String s1 = null;
Whereas
String s1 = "";
Is, as said, a different thing.
Difference between null and empty () Java String
"" is an actual string, albeit an empty one.
null, however, means that the String variable points to nothing.
a==b
returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.
a.equals(b)
returns false because "" does not equal null, obviously.
The difference is though that since "" is an actual string, you can still invoke methods or functions on it like
a.length()
a.substring(0, 1)
and so on.
If the String equals null, like b, Java would throw a NullPointerException
if you tried invoking, say:
b.length()
If the difference you are wondering about is == versus equals, it's this:
== compares references, like if I went
String a = new String("");
String b = new String("");
System.out.println(a==b);
That would output false because I allocated two different objects, and a and b point to different objects.
However, a.equals(b)
in this case would return true, because equals
for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.
Be warned, though, that Java does have a special case for Strings.
String a = "abc";
String b = "abc";
System.out.println(a==b);
You would think that the output would be false
, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.
Difference between NULL and Blank Value in Mysql
NULL
is an absence of a value. An empty string is a value, but is just empty.NULL
is special to a database.NULL
has no bounds, it can be used forstring
,integer
,date
, etc. fields in a database.NULL
isn't allocated any memory, thestring
withNULL
value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is""
.
What is difference between null and empty list?
In Java, collections won't magically spring into existence just by adding something to them. You have to initialize pcList
by creating an empty collection first:
List<Map<String, Object>> pcList = new ArrayList<>();
An empty collection isn't the same as null
. An empty collection is actually a collection, but there aren't any elements in it yet. null
means no collection exists at all.
Note that an object can't be of type List
, because that's an interface; therefore, you have to tell Java what kind of List
you really want (such as an ArrayList
, as I've shown above, or a LinkedList
, or some other class that implements List
).
What is the difference between null and empty?
A variable is NULL
if it has no value, and points to nowhere in memory.
empty()
is more a literal meaning of empty, e.g. the string ""
is empty, but is not NULL
.
The following things are considered to
be empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
Source.
Example
$a
is NULL
.
$a = ''
is empty, but not NULL
.
Update
If
$a=''
is empty but notNULL
, when do I use theempty()
function and when do I use theisset()
function.
isset()
will return FALSE
is the variable is pointing to NULL
.
Use empty()
when you understand what is empty (look at the list above).
Also when you say it points nowhere in memory, what does that mean exactly?
It means that $str = ''
will be in memory as a string with length of 0.
If it were $str = NULL
, it would not occupy any memory.
Empty string vs NULL
Some differences between them:
NULL
can be assigned to any type, as opposed to empty string which won't be compatible with date/numerical fields.NULL
is an UNKNOWN value, it doesn't have a value as opposed to an empty string, which is a value, but empty one.- As far as I'm aware of,
NULL
shouldn't capture memory as opposed to an empty string which does. null = null
will result innull
as opposed to''=''
which will result inTRUE
.
What is the difference between null and undefined in JavaScript?
undefined
means a variable has been declared but has not yet been assigned a value :
var testVar;
console.log(testVar); //shows undefined
console.log(typeof testVar); //shows undefined
Related Topics
How to Select Rows Where Column Value Is Not Null Using Codeigniter's Activerecord
Checking for an Empty Result (Php, Pdo, and MySQL)
Array_Unique and Then Renumbering Keys
PHP Curl How to Add the User Agent Value or Overcome the Servers Blocking Curl Requests
Laravel and View Caching in Development -- Can't See Changes Right Away
Error 500: Premature End of Script Headers
How to Append to a Xml File with PHP Preferably with Simplexml
Sort an Array with Special Characters in PHP
PHP Datetime Microseconds Always Returns 0
Url Encode Equivalent in Ruby on Rails
PHP Get the Last 3 Elements of an Associative Array While Preserving the Keys
Any Way to Keep Curl's Cookies in Memory and Not on Disk
How Is an Array in a PHP Foreach Loop Read
Select Last 20 Order by Ascending - PHP/Mysql
Require_Once :Failed to Open Stream: No Such File or Directory