Display default initial values of DataTypes in Java



To display default initial values of a datatype, you need to just declare a variable of the same datatype and display it.

The following is a Java program to display initial values of DataTypes.

Example

 Live Demo

public class Demo {    boolean t;    byte b;    short s;    int i;    long l;    float f;    double d;    void Display() {       System.out.println("boolean (Initial Value) = " + t);       System.out.println("byte (Initial Value) = " + b);       System.out.println("short (Initial Value) = " + s);       System.out.println("int (Initial Value) = " + i);       System.out.println("long (Initial Value) = " + l);       System.out.println("float (Initial Value) = " + f);       System.out.println("double (Initial Value) = " + d);    }    public static void main(String[] args) {       Demo d = new Demo();       System.out.println("Displaying initial values...");       d.Display();    } }

Output

Displaying initial values... boolean (Initial Value) = false byte (Initial Value) = 0 short (Initial Value) = 0 int (Initial Value) = 0 long (Initial Value) = 0 float (Initial Value) = 0.0 double (Initial Value) = 0.0

In the above program, we have declared a variable with different datatypes.

boolean t; byte b; short s; int i; long l; float f; double d;

Now to get the default initial values, just print the variable declared above.

System.out.println("boolean (Initial Value) = " + t); System.out.println("byte (Initial Value) = " + b); System.out.println("short (Initial Value) = " + s); System.out.println("int (Initial Value) = " + i); System.out.println("long (Initial Value) = " + l); System.out.println("float (Initial Value) = " + f); System.out.println("double (Initial Value) = " + d);

The above displays the default values.

Updated on: 2020-06-26T10:27:05+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements