Default double value In Java

We will see what is default value of double data type in java in this example . In this tutorial , We will write a java program and declare double variable and then print the value of these variable .We will see also different methods for print double value in java .

Note : The Default value of double in java is 0.0d 

Firstly we see how to print double value in java then check default double value in java .

how to Print double Value In Java?

We can print double java using println() and printf() method as below . printf method provides more formatting option as compare println(). Now we check how to print double value in java with example .

public class PrintDoubleValue {
		public static void main(String[] args) {
		double value = 5.5;
		System.out.println("value: " + value);
		System.out.printf("value: %f", value);
	}

}

Output:

value: 5.5
value: 5.500000

Program to Find the Default value of double In Java

We will check default value of double data type in java in this example. We create a unassigned variable(default double) , then print it’s value using discussed methods above .

public class DefaultDoubleValue {

	double defaultDouble;

	public static void main(String[] args) {
		DefaultDoubleValue defaultDoubleValue = new DefaultDoubleValue();
		defaultDoubleValue.printDoubleDefaultValues();
	}

	public void printDoubleDefaultValues() {
		// print default value of double
		System.out.println("Default value of double: " + defaultDouble);
		System.out.printf("Default value of double : %.1f", defaultDouble);
	}
	
}

Output:

Default value of double: 0.0
Default value of double : 0.0

This examples clears that default double value java is 0.0 .

Let’s be sure about it . We will create few more unassigned double variables and print value of these unassigned double variables .

public class DefaultDoubleValue2 {
	double defaultDouble1;
	double defaultDouble2;

	public static void main(String[] args) {
		DefaultDoubleValue2 defaultDoubleValue2 = new DefaultDoubleValue2();
		defaultDoubleValue2.printDoubleDefaultValues();
	}

	public void printDoubleDefaultValues() {
		// print default value of double
		System.out.println("Default value of double 1: " + defaultDouble1);
		System.out.printf("Default value of double 1 : %.1f", defaultDouble1);
		System.out.println("");
		System.out.println("Default value of double 2: " + defaultDouble2);
		System.out.printf("Default value of double 2 : %.1f", defaultDouble2);
	}
}

Output :

Default value of double 1: 0.0
Default value of double 1 : 0.0
Default value of double 2: 0.0
Default value of double 2 : 0.0

Now it is verify that default double value java is 0.0 .

In this tutorial we learned what is default double value java . We have seen also different method how to print double value in java  . You can see more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

24 − 19 =