List out the default values of numeric and non-numeric primitive data types in Java?


When you create instance variables in Java you need to initialize them, else the compiler will initialize on your behalf with default values which are −

  • byte: 0
  • short: 0
  • int: 0
  • long: 0
  • float: 0.0
  • double: 0.0
  • boolean: false
  • string: null

Example

In the following Java program prints the default values of the numeric and non-numeric primitive variables in java.

public class DefaultValues {
   byte byteVariable;
   short shortVariable;
   int intVariable;
   long longVaraible;
   float floatVariable;
   double doubleVariable;
   boolean boolVariable;
   String stringVariable;
   public static void main(String args[]){
      DefaultValues obj = new DefaultValues();
      System.out.println("Default values of numeric variables in Java:");
      System.out.println("byte: "+obj.byteVariable);
      System.out.println("short: "+obj.shortVariable);
      System.out.println("int: "+obj.intVariable);
      System.out.println("long: "+obj.longVaraible);
      System.out.println("float: "+obj.floatVariable);
      System.out.println("double: "+obj.doubleVariable);
      System.out.println("Default values of non-numeric variables in Java:");
      System.out.println("boolean: "+obj.boolVariable);
      System.out.println("string: "+obj.stringVariable);
   }
}

Output

Default values of numeric variables in Java:
byte: 0
short: 0
int: 0
long: 0
float: 0.0
double: 0.0
Default values of non-numeric variables in Java:
boolean: false
string: null
raja
Published on 01-Aug-2019 12:21:52
Advertisements