Default value of primitive data types in Java


Primitive datatypes are predefined by the language and named by a keyword in Java. Here is an example to display the default value of primitive data types.

Example

 Live Demo

public class Demo {
   static boolean val1;
   static double val2;
   static float val3;
   static int val4;
   static long val5;
   static String val6;
   public static void main(String[] args) {
      System.out.println("Default values.....");
      System.out.println("Val1 = " + val1);
      System.out.println("Val2 = " + val2);
      System.out.println("Val3 = " + val3);
      System.out.println("Val4 = " + val4);
      System.out.println("Val5 = " + val5);
      System.out.println("Val6 = " + val6);
   }
}

Output

Default values.....
Val1 = false
Val2 = 0.0
Val3 = 0.0
Val4 = 0
Val5 = 0
Val6 = null

Above, firstly we have declared variables of specific type. Remember, to get the default values, you do not need to assign values to the variable.

static boolean val1;
static double val2;
static float val3;
static int val4;
static long val5;
static String val6;

Now to display the default values, you need to just print the variables.

System.out.println("Val1 = " + val1);
System.out.println("Val2 = " + val2);
System.out.println("Val3 = " + val3);
System.out.println("Val4 = " + val4);
System.out.println("Val5 = " + val5);
System.out.println("Val6 = " + val6);

Updated on: 26-Jun-2020

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements