Java NULL empty difference-What is difference between NULL and empty in Java

Spread the love

Java null empty difference: In this tutorial, I will explain to you what is the Java null empty difference. We will see what actually Null value and how it is different from assigning an empty string in a variable.
But first, we need to see what exactly is a variable. And how it is used in programming before learning Java null empty difference.

Variable:

As its name suggests variable is an entity which varies. In programming, a variable is a memory location. A location which gets assigned or dedicated to any value in a RAM.
For example when we declare a variable like
public String email = “hello@gmail.com”;
We are actually reserving memory location with the name “email” in RAM at runtime.

Empty:

So now when you assign blank value in a variable “email” like
String email = “” ;
Then in this case also a memory location with name “email” will gets reserved in RAM but will not have any value or rather you may say “email” memory location contains a BLANK value in it.
Hence an empty value will use to indicate a blank or no value in memory location but with the creation of variable in memory.

NULL:

Now we will simply declare a variable like
public String email;  OR
when we assign a NULL to the variable “email” like
public String email = NULL;
then, in this case, no memory location is get reserved in RAM. No memory initialization for a variable “email” happened. In this case, there is no existence of a variable named “email” in a memory.
So NULL will also denote no value or blank value along with no memory initialization and Java will throw a NullPointerException.
Hope this tutorial will clear your doubt about Java null empty difference.


Spread the love

Leave a Comment