<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Developers Journal</title>
    <description>The latest articles on DEV Community by Developers Journal (@dj_devjournal).</description>
    <link>https://dev.to/dj_devjournal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F82682%2F8b56794e-c9a9-44a6-8c4b-5dd1131efd1e.jpg</url>
      <title>DEV Community: Developers Journal</title>
      <link>https://dev.to/dj_devjournal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dj_devjournal"/>
    <language>en</language>
    <item>
      <title>Understanding null in Java</title>
      <dc:creator>Developers Journal</dc:creator>
      <pubDate>Wed, 16 Oct 2019 06:57:59 +0000</pubDate>
      <link>https://dev.to/dj_devjournal/understanding-null-in-java-4o31</link>
      <guid>https://dev.to/dj_devjournal/understanding-null-in-java-4o31</guid>
      <description>&lt;p&gt;Java and null share a unique bond. Almost all of the Java developers get trouble with the NullPointerException and this is the most infamous fact about null and Java. In this journal, we will be understanding null in Java.&lt;/p&gt;

&lt;p&gt;Null was there for a long time and we all know that null creates more problems than it solves. So, let‘s try to learn more about the concepts of null and make sure that we use it correctly.&lt;/p&gt;

&lt;p&gt;Many of the developers are already familiar with null but for those who are not, they can learn some old and new things about null keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is null?
&lt;/h2&gt;

&lt;p&gt;null is a very important concept in Java. The original intention of inventing null was to denote the absence of something. But over the years it has troubled Java developers with the nasty NullPointerException.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read More:&lt;/strong&gt; &lt;a href="https://developersjournal.in/how-to-handle-null-pointer-exception-in-java/"&gt;How to Handle Null Pointer Exception in Java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we start, let’s recall and understand what is variable and what is a value? Variables are basically the containers that store the value inside them. Every time we declare a variable, we need to specify the type that will define the kind of value which will be stored in the variable.&lt;/p&gt;

&lt;p&gt;In Java, there are two major categories of types: primitive and reference. Variables that are declared of type primitive store values whereas variables declared as reference types store references. Given below is a basic example of variables and value assigned to those variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;in.developersjournal&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Uninitialised variable of reference type.&lt;/span&gt;
    &lt;span class="c1"&gt;// Hence it will store null in it.&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;djObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Uninitialised int. It is of primitive type.&lt;/span&gt;
    &lt;span class="c1"&gt;// Hence it will store 0 until initialised.&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;firstInt&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Initialised integer with value 10.&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;secondInt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondInt&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstInt&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;djObject&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet declares 3 variables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object djObject – Reference Type&lt;/li&gt;
&lt;li&gt;int firstNumber – Primitive Type&lt;/li&gt;
&lt;li&gt;int secondNumber – Primitive Type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here the first two variables are not initialized while declaring them and the third variable is initialized with the number 10. Since the Object is of reference type thus it stores null in it and the firstNumber stores 0 as the default value. The output of the above code snippet is shown in Figure 1 shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4OgCdt9B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://developersjournal.in/wp-content/uploads/2019/10/Screenshot-2019-10-15-at-11.50.38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4OgCdt9B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://developersjournal.in/wp-content/uploads/2019/10/Screenshot-2019-10-15-at-11.50.38.png" alt="Understanding Null"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Figure 1: OUTPUT of the code snippet written above.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Properties of null
&lt;/h2&gt;
&lt;h4&gt;
  
  
  #null Keyword / Reserved Word
&lt;/h4&gt;

&lt;p&gt;In Java, null is a reserved word (keyword) for literal values. It seems like a keyword, but actually, it is a literal similar to true and false. The reserved word null is case sensitive and we cannot write null as Null or NULL, the compiler will not recognize them and give an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;djObject1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;NULL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Not Ok - Compile time error&lt;/span&gt;
&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;djObject2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  #null used as a default
&lt;/h4&gt;

&lt;p&gt;Every primitive type of variable has a default value (e.g. int has 0, boolean has false) if not initialized at the time of declaration. Similarly null is the default value of any reference type variable which is not initialized at the time of declaration. This is true for all kinds of variables, instance variable or static variable, except that compiler will warn you if you use a local variable without initializing them.&lt;/p&gt;

&lt;h4&gt;
  
  
  #null – Casting to other types
&lt;/h4&gt;

&lt;p&gt;null is not an Object or neither a type. It’s just a special value, which can be assigned to any reference type. Typecasting null to any reference type is fine at both compile-time and runtime and it will not throw any error or exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Typecasting null to String type&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;myStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Typecasting to an Integer class&lt;/span&gt;
&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;myItr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Typecasting to a Double class&lt;/span&gt;
&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;myDbl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An important point to remember here is that null can only be assigned to reference types. We cannot assign null to primitive variables e.g. int, double, float or boolean. If we try to do so, then the compiler will complain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// incompatible types : required int found null&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// incompatible types : required short found null&lt;/span&gt;
&lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// incompatible types : required byte found null&lt;/span&gt;
&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  #null – instanceOf operator
&lt;/h4&gt;

&lt;p&gt;instanceof operator will return false if used against any reference variable with null value or null literal itself. We can see it in action in code snippet below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NullInstanceOfExample&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Byte&lt;/span&gt; &lt;span class="n"&gt;nullReferenceVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Checking instanceof with Byte type variable with null stored"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nullReferenceVariable&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Byte&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nullReferenceVariable is instance of Byte"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nullReferenceVariable is NOT an instance of Byte"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;    
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  NullPointerException (NPE)
&lt;/h2&gt;

&lt;p&gt;NullPointerException is a runtime exception. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when an application attempts to use an object reference with a null value.&lt;/p&gt;

&lt;p&gt;To get more understanding on NullPointerException, please have a look at the below mentioned journal where we have tried to explain the NPE in more details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read More:&lt;/strong&gt; &lt;a href="https://developersjournal.in/how-to-handle-null-pointer-exception-in-java/"&gt;How to Handle Null Pointer Exception in Java&lt;br&gt;
Conclusion&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That’s about null in Java. Common scenarios around the null have been tried to cover: how to use it, where to use it, and how it can be used as the placeholder for reference type variables.&lt;/p&gt;

&lt;p&gt;Always remember, null is the default value of any reference variable and you cannot call any instance method, or access an instance variable using null reference in Java.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Originally Published at &lt;a href="https://developersjournal.in/understanding-null-in-java/"&gt;Understanding null in Java - Developers Journal&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
    </item>
  </channel>
</rss>
