What are the default values of instance variables whether primitive or reference type in Java?


When we haven’t initialized the instance variables compiler initializes them with default values.

For boolean type, the default value is false, for float and double types default values are 0.0 and for remaining primitive types default value is 0.

Example

Live Demo

public class Sample {
   int varInt;
   float varFloat;
   boolean varBool;
   long varLong;
   byte varByte;
   short varShort;
   double varDouble;
   public static void main(String args[]){
      Sample obj = new Sample();
      System.out.println("Default int value ::"+obj.varInt);
      System.out.println("Default float value ::"+obj.varFloat);
      System.out.println("Default boolean value ::"+obj.varBool);
      System.out.println("Default long value ::"+obj.varLong);
      System.out.println("Default byte value ::"+obj.varByte);
      System.out.println("Default short value ::"+obj.varShort);
      System.out.println("Default double value ::"+obj.varDouble);
   }
}

Output

Default int value ::0
Default float value ::0.0
Default boolean value ::false
Default long value ::0
Default byte value ::0
Default short value ::0
Default double value ::0.0

Updated on: 16-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements