Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

null Keyword in Java
Previous Home Next
  1. The null keyword in java programming language is a reserved word that is used to represent no value.
  2. Null is not a keyword in Java. It is a reference literal value.
  3. Primitive data types such as byte, short, int, long, char, float, double, Boolean cannot have a null type.
  4. Reference variable is refer to an object. When a reference variable does not have a value (it is not referencing an object) such a reference variable is said to have a null value.
  5. null is a case sensitive, you cannot write null as Null or NULL, compiler will not recognize them and give error.
  6. Object obj = NULL; // Not Ok 
    Object obj1 = null //Ok
  7. Unlike common misconception, null is not Object or neither a type. It's just a special value, which can be assigned to any reference type and you can type cast null to any type
  8. String str = null; // null can be assigned to String
    Integer itr = null; // you can assign null to Integer also
    Double dbl = null;  // null can also be assigned to Double
            
    String myStr = (String) null; // null can be type cast to String
    Integer myItr = (Integer) null; // it can also be type casted to Integer
    Double myDbl = (Double) null; // yes it's possible, no error

    Example :

    class Rectangle
    {
          double length;
          double breadth;
    }
    class NullRectangle 
    {
        public static void main(String args[]) 
        {
    
             Rectangle myrect1;
             System.out.println(myrect1.length);
    
         }
    }

    output :

    NullRectangle.java:13: error: variable myrect1 might not have been initialized
                 System.out.println(myrect1.length);
                 ^
                1 error

    myrect1 is not initialized.

    Default value inside myrect1 is null, means it does not contain any reference.

    If you are declaring reference variable at “Class Level” then you don’t need to initialize instance variable with null.

    Example :

    class Rectangle
    {
         double length;
         double breadth;
    }
    class NullRectangle1 
    {
          public static void main(String args[])
          {
    			 Rectangle myrect1;
    			 if(myrect1 == null)
                 {
    					 myrect1 = new Rectangle();
                  }
    
           }
    }

    output :

    NullRectangle1.java:14: error: variable myrect1 might not have been initialized
                if(myrect1 == null)
                    ^
                1 error

    Class Level null Value

    1. No need to initialize instance variable with null.
    2. Instance Variable contain default value as “null”.
    3. Meaning of “null” is that – Instance Variable does not reference to any object.
    Rectangle myrect1;

    is similar to-

    Rectangle myrect1 = null;

    Method Level null value

    1. we have to create any object inside “Method” then we must initialize instance variable with Null value.
    2. If we forgot to initialize instance variable with null then it will throw compile time error.

    Valid Declaration :

    Rectangle myrect1 = null;

    Invalid Declaration :

    Rectangle myrect1 ;
Previous Home Next