Java Double Keyword
- In java primitive data types, we have two different types of data types which are Boolean and floating-point data types.
- In floating data type again we have four types which are integers, float, double, long.
- So, now in this article we are going to learn about the primitive data type which is a double keyword or data type.
- The name of the keyword double is evolved because of the float data type this is because the float capacity is 4 bytes whereas double is 8 bytes which is twice or double of float.
- The double keyword is also known to be a precise data type or keyword as it can store the large values of the integers while declaring using double.
- Double keyword or data type stores the real numbers which includes integers, rational and irrational, natural numbers, whole numbers.
- So, the double stores the double precision of 64-bits.
- The variables and the methods are being implemented using the keyword double or keyword data type.
- The decimal numbers are generally initialized or declared using the double keyword or data type.
Points to remember:
- The range of the double keyword is varied from the values 4.940656458412e-324d to1.79763134862e+308d (positive and negative).
- Double keyword holds the default value that is 0.0d.
- Default storage capacity or default size of the keyword double is taken as 8 bytes.
- The double keyword cannot be implemented for precise values like currency because it effects the approaching way of assigning values through double keyword.
Syntax of Java Double Keyword
double variable name.
EX: double a; doubleraju; etc.
Here in above example double is the keyword and a, raju are variables name.
We can also declare multi variables for a keyword double at a time that is
EX: double variable 1, variable 2,variable3.
Java Double Keyword Examples
Example 1:
Simple example program to declare the variable using double keyword.
public class DblEx1 {
public static void main (String [] s) {
double R=9.5;
System.out.println("R: "+R);
}
}
Output:
R:9.5
Example 2:
The integer value of a variable is assigned to the double data type in this program. The providing of the value is called typecast integer to double. So that it shows the similar values in decimal form.
public class DblEx2 {
public static void main (String [] s) {
double D1=3;
double D2=17;
System.out.println("D1: "+D1);
System.out.println("D2: "+D2);
}
}
Output:
D1: 3.0
D2: 17.0
Example 3:
In this example program we will test the double keyword by assigning bigger values.
public class DblEx3 {
public static void main (String [] s) {
double Dee 1=78.8653457874857.
double Dee 2=5.676793847579876.
System.out.println("Dee1: "+Dee1).
System.out.println("Dee2: "+Dee2).
}
}
Output:
Dee1: 78.8653457874857.
Dee2: 5.676793847579876
Example 4:
Assigning the float value to decimal variable for testing the working of double keyword.
public class DblEx4 {
public static void main (String [] s) {
double mah1=87.11f;
double mah2=39f;
System.out.println("mah1: "+mah1);
System.out.println("mah2: "+mah2);
}
}
Output:
Mah 1: 87.11000015258789
Mah 2: 39.0
Example 5:
Assigning the maximum range of decimal value using double data type.
public class DblEx5 {
public static void main (String [] s) {
double Dav1=4.94065645841246544e-324d.
double Dav2=1.79769313486231570e+308d;
System.out.println("Dav1: "+Dav1);
System.out.println("Dav2: "+Dav2);
}
}
Output:
Dav1: 4.9E-324
Dav2: 1.7976931348623157E308
Example 6:
Here this example provides the values the in scientific notation using double keyword.
public class DblEx6 {
public static void main (String [] s) {
double Raj1=1873.2;
//providing similar value in scientific notation
double Raj2=1.8732e3;
System.out.println("Raj1: "+Raj1);
System.out.println("Raj2: "+Raj2);
}
}
Output:
Raj1: 1873.2
Raj2: 1873.2
Example 7:
We can also create method that returns the type of integer using double keyword.
public class DblEx7 {
public double display (double hight)
{
return hight;
}
public static void main (String [] s) {
DblEx7 g=new DblEx7();
System.out.println(g. display(98.5));
}
}
Output:
98.5
Example 8:
Example program for Double and integer interaction in java programming language.
public class DblDemo1{
public static void main (String [] args) {
int l = 6;
double Dbl1 = 9;
double Dbl2 = k*7.0;
System.out.println(Dbl1);
System.out.println(k*Dbl1);
System.out.println(Dbl);
}
}
output:
6.0
9.0
14.0