

{"id":12398,"date":"2018-03-31T09:24:36","date_gmt":"2018-03-31T09:24:36","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=12398"},"modified":"2026-05-07T16:24:03","modified_gmt":"2026-05-07T10:54:03","slug":"java-null","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-null\/","title":{"rendered":"Java Null &#8211; 7 Unknown Facts about Null in Java"},"content":{"rendered":"<p>Before we talk about null in Java, let us go back a few steps and recall what variables in Java are.<\/p>\n<p>Well, simply speaking, they can be thought of as tumblers. And the value of those variables is the water inside the tumbler. Makes sense.<br \/>\nNow, how many types of variables were there?<\/p>\n<p>Well, classifying broadly, there were 2 types.<\/p>\n<p>1. Reference types<\/p>\n<p>2. Primitive types<\/p>\n<p>Primitive types are the ones that would hold values, and reference types are the ones that can store references.<\/p>\n<h3>Null in Java<\/h3>\n<p>Since we just read about primitive and reference types in Java, let\u2019s break down a few points.<\/p>\n<p>When primitive values do not have a value explicitly mentioned by the programmer, the default value is stored based on the data type of the variable.<\/p>\n<p>For example, an uninitialized int variable will have 0 as the default value, a boolean variable will have false as the default value, and so on\u2026<\/p>\n<p>But what will the reference variables have if they are not explicitly referencing an object in memory? Yes, you guessed it! They would store null!<\/p>\n<p>This is exactly why we need null in Java. It is used for representing references when they are not pointing to any objects in memory. It has a special purpose.<\/p>\n<p>In Java, null is a literal. It is the same as true or false. It has a special meaning. Many people confuse it with an identifier or a keyword.<\/p>\n<h3>Important Facts about Null<\/h3>\n<p>Some facts are essential to know when you are working with null in Java.<\/p>\n<ul>\n<li>Null is case-sensitive in nature. This means that you cannot expect NULL to be a \u201cliteral\u201d in Java. It&#8217;s the same as true and false. These have individual meanings.<\/li>\n<li>All reference variables have null as their default value.<\/li>\n<li>The compiler is unable to unbox null objects. It throws a NullPointerException.<\/li>\n<li>Null is not an instance of any class. Hence, a null value will return false if used with the instanceOf operator.<\/li>\n<li>Static methods are callable with a reference to the null type.<\/li>\n<li>You cannot call non-static methods with a reference of the null type.<\/li>\n<li>You can use == and != comparisons with null types in Java.<\/li>\n<\/ul>\n<h3>Java Null Pointer Exception<\/h3>\n<p>When the program tries to access a part of the memory that is not initialized by default, then the compiler throws a null pointer exception.<\/p>\n<p>Simply speaking, this is possible when you try to access the 100th element of an array of 50 elements.<\/p>\n<p><strong>These are some common causes when a null exception occurs:-<\/strong><\/p>\n<ul>\n<li>When an object that is not initialized properly tries to call the method<\/li>\n<li>When an uninitialized object tries to access the variables of the class<\/li>\n<li>When a null is passed to a function as an argument.<\/li>\n<li>When the array is null, and is trying to get the length of that array<\/li>\n<\/ul>\n<p><strong>Java example to evaluate all the facts of null:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.javanull;\r\n\r\n\r\npublic class Main\r\n{\r\n    \r\n    private static Object a;\r\n    public static void staticmethodtest()\r\n    {\r\n        System.out.println(\"This is a static method! \");\r\n    }\r\n    public void nonstaticmethodtest()\r\n    {\r\n        System.out.println(\"This is a non-static method!\");\r\n    }\r\n    public static void main(String[] args) {\r\n        Integer i = null;\/\/null is a reserved word\r\n\r\n        \/\/Returns error-&gt; Integer j=NULL;\/\/NULL is invalid.\r\n \r\n        System.out.println(\"The default value of a is \"+a);\r\n\r\n        Integer value1=null\r\n;\r\n        \/\/Returns error -&gt; int value=value1; Cannot unbox null type.\r\n \r\n        System.out.println(value1 instanceof Integer);\/\/returns false.this is because null isnt an instance of any class. \r\n        Main ob=null;\r\n        ob.staticmethodtest();\r\n        \/\/Returns error-&gt; ob.nonstaticmethodtest(); This is not possible. NullPointerException.\r\n        String a=null;\r\n        String b=null;\r\n        System.out.println(a==b);\/\/returns true;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">false<br \/>\nThis is a static method!<br \/>\ntrue<\/div>\n<ul>\n<li><strong>We came to know about the Null Pointer Exception in Java. But now we will learn how to avoid it.<\/strong><\/li>\n<\/ul>\n<p>We know that we will encounter a null pointer exception when the compiler tries to access some data that is null.<\/p>\n<p>Any data value that is not present in memory or set to null explicitly will return a Null Pointer Exception if accessed. A primary example of this would be trying to access an index in the array that is out of bounds. Let us see this with an example.<\/p>\n<p>Program to illustrate Java NullPointerException:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.javanull;\r\n\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    public static String nullreturnfunc()\r\n    {\r\n        return null;\r\n        \r\n    }\r\n    public static void main (String[] args) {\r\n        String test;\r\n        test=nullreturnfunc();\r\n        System.out.println(test.charAt(3));\r\n        \r\n        \r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Exception in thread &#8220;main&#8221; java.lang.NullPointerException<br \/>\nat Main.main(Main.java:21)<\/div>\n<ul>\n<li><strong>Now we can either use a try-catch method or a simple if-else block to prevent these types of errors. The try-catch method is the more sensible way to do it. But both work.<\/strong><\/li>\n<\/ul>\n<p>The same program, when modified, becomes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.javanull;\r\n\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    public static String nullreturnfunc()\r\n    {\r\n        return null;\r\n        \r\n    }\r\n    public static void main (String[] args) {\r\n        String test;\r\n        test=nullreturnfunc();\r\n        if(test!=null)\r\n        System.out.println(test.charAt(3));\r\n        else\r\n        System.out.println(\"The value is null!\");\r\n        \r\n        \r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The value is null!<\/div>\n<ul>\n<li><strong>Now we try to use try-catch to avoid null pointer exceptions.<\/strong><\/li>\n<\/ul>\n<p>We can use any kind of message we want in the catch block.<\/p>\n<p>If you are having difficulty understanding what a try-catch is, please refer to our exception handling in Java article.<\/p>\n<p>The program becomes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.javanull;\r\nimport java.util.*;\r\n\r\npublic class Main\r\n{\r\n    public static String nullreturnfunc()\r\n    {\r\n        return null;\r\n        \r\n    }\r\n    public static void main (String[] args) {\r\n        String test;\r\n        test=nullreturnfunc();\r\n        try \r\n        {\r\n            System.out.println(test.charAt(3));\r\n        } \r\n        catch(Exception e) \r\n        {\r\n            System.out.println(\"The value is null!\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The value is null!<\/div>\n<h3>Best Ways to Use Null<\/h3>\n<div>\n<p>The following are recommended practices to bear in mind when utilizing null in Java:<\/p>\n<ul>\n<li><strong>Beware of null references:<\/strong> Before using a reference variable, always take into account the possibility that it might be null.<\/li>\n<li><strong>Make strategic use of null checks:<\/strong> Only when required, add null checks to avoid NullPointerExceptions.<\/li>\n<li><strong>Examine alternatives to null:<\/strong> You may be able to completely avoid null references by redesigning your program in certain situations.<\/li>\n<li><strong>Null usage of documents:<\/strong> If you choose to use null, be sure to explain its purpose and methodology in order to make the code easier to read.<\/li>\n<\/ul>\n<\/div>\n<h3>Summary<\/h3>\n<p>We learned a lot about null in Java. It is essential to have a good grasp of null in Java.<\/p>\n<p>Even experienced Java programmers sometimes run into fatal NullPointerExceptions and have no idea how to fix the bug.<\/p>\n<p>Hence, a clear understanding of Java Null will come in handy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we talk about null in Java, let us go back a few steps and recall what variables in Java are. Well, simply speaking, they can be thought of as tumblers. And the value&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84888,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[128,18534,6833,7610,23629,9145,11462,13793,15001,15080],"class_list":["post-12398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-and","tag-facts-about-java-null","tag-instanceof-operator","tag-java-null","tag-java-null-pointer-exception","tag-null-is-case-sensitive","tag-reference-variable-value","tag-static-vs-non-static-methods","tag-type-of-null","tag-types-of-nulls-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Null - 7 Unknown Facts about Null in Java - DataFlair<\/title>\n<meta name=\"description\" content=\"Explore Java Null Tutorial &amp; Interesting facts about Null: Case sensitivity, Reference Variable Value, Autoboxing &amp; Unboxing in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-null\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Null - 7 Unknown Facts about Null in Java - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Explore Java Null Tutorial &amp; Interesting facts about Null: Case sensitivity, Reference Variable Value, Autoboxing &amp; Unboxing in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-null\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-31T09:24:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-07T10:54:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Null - 7 Unknown Facts about Null in Java - DataFlair","description":"Explore Java Null Tutorial & Interesting facts about Null: Case sensitivity, Reference Variable Value, Autoboxing & Unboxing in Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-null\/","og_locale":"en_US","og_type":"article","og_title":"Java Null - 7 Unknown Facts about Null in Java - DataFlair","og_description":"Explore Java Null Tutorial & Interesting facts about Null: Case sensitivity, Reference Variable Value, Autoboxing & Unboxing in Java.","og_url":"https:\/\/data-flair.training\/blogs\/java-null\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-03-31T09:24:36+00:00","article_modified_time":"2026-05-07T10:54:03+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-null\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Java Null &#8211; 7 Unknown Facts about Null in Java","datePublished":"2018-03-31T09:24:36+00:00","dateModified":"2026-05-07T10:54:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/"},"wordCount":850,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg","keywords":["== and !=","Facts about Java null","Instanceof operator","Java Null","Java null pointer exception","null is Case sensitive","Reference Variable value","Static vs. Non static Methods","Type of null","Types of nulls in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-null\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-null\/","url":"https:\/\/data-flair.training\/blogs\/java-null\/","name":"Java Null - 7 Unknown Facts about Null in Java - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg","datePublished":"2018-03-31T09:24:36+00:00","dateModified":"2026-05-07T10:54:03+00:00","description":"Explore Java Null Tutorial & Interesting facts about Null: Case sensitivity, Reference Variable Value, Autoboxing & Unboxing in Java.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-null\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-null\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-null\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Java-Null.jpg","width":1200,"height":628,"caption":"Java Null"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-null\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java Null &#8211; 7 Unknown Facts about Null in Java"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=12398"}],"version-history":[{"count":12,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12398\/revisions"}],"predecessor-version":[{"id":148258,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12398\/revisions\/148258"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84888"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=12398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=12398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=12398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}