Java Program to get the default value of primitive data types

Getting the default value of primitive data types is not a tedious task. Declare a variable with the specific datatype and display the value. This gives the default value. Do not assign a value to the variable, since we need the default.

However, first we should learn what is primitive datatype in Java. Primitive types are the Java data types used for data manipulation, for example, int, char, float, double, boolean, etc. Let’s learn about them one by one:

  • byte: An 8-bit signed two’s complement integer
    Minimum Value: -128
    Maximum Value: 127
  • short: A 16-bit signed two’s complement integer
    Minimum Value: -32768
    Maximum Value: 32767
  • int: A 32-bit signed two’s complement integer
    Minimum Value: -2,147,483,648
    Maximum Value: 2,147,483,647
  • long: A 64-bit two’s complement integer
    Minimum Value: -9,223,372,036,854,775,808
    Maximum Value: 9,223,372,036,854,775,807
  • char: A single 16-bit Unicode character.
    Minimum Value: ‘\u0000’ (or 0)
    Maximum value: ‘\uffff’
  • float: A single-precision 32-bit IEEE 754 floating point
    Minimum Value: 1.4E-45
    Maximum Value: 3.4028235E38
  • double: A double-precision 64-bit IEEE 754 floating point
    Minimum Value: 4.9E-324
    Maximum Value: 1.7976931348623157E308
  • boolean: Possible values, TRUE and FALSE.

Below are some examples to display the primitive datatypes default value in Java:

Example 1

public class Example {

    static boolean a;
    static int b;
    static long c;
    
  public static void main(String[] args) {
   
    System.out.println("Default value of boolean Type = " + a);
    System.out.println("Default value of int Type = " + b);
    System.out.println("Default value of long Type = " + c);
  }
}

Output

Default value of boolean Type = false
Default value of int Type = 0
Default value of long Type = 0

Example 2

public class Example {

    static byte a;
    static short b;
    static char c;
    static float d;
    static double e;
    
  public static void main(String[] args) {
   
    System.out.println("Default value of byte Type = " +a);
    System.out.println("Default value of short Type = " +b);
    System.out.println("Default value of char Type = " +c);
    System.out.println("Default value of float type = " +d);
    System.out.println("Default value of double Type = " +e);
  }
}

Output

Default value of byte Type = 0
Default value of short Type = 0
Default value of char Type = 
Default value of float type = 0.0
Default value of double Type = 0.0
StringBuffer class in Java
Java Program to display the minimum and maximum value of primitive data types
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment