Java double type
In this chapter you will learn:
- What is Java double type
- How to create double type Literals
- How to get the value of double infinity
- What is double type NaN(Not a Number)
double type
double represents double-precision numbers. double is 64-bit width and its range is from 4.9e-324 to 1.8e+308 approximately.
Here is a program that uses double variables to compute the area of a circle:
public class Main {
public static void main(String args[]) {
double pi, r, a;
//from j a v a 2 s . c o m
r = 10.8888; // radius of circle
pi = 3.1415926; // pi, approximately
a = pi * r * r;
System.out.println("Area of circle is " + a);
}
}
The output:
double type Literals
double type numbers have decimal values with a fractional component.
They can be expressed in either standard or scientific notation.
Standard notation consists of a whole number component followed by a decimal point followed by a fractional component.
For example, 2.0
, 3.14159
, and 0.6667
.
public class Main {
public static void main(String args[]) {
double d = 3.14159;
System.out.print(d);//3.14159
} /* j a v a2 s . c o m*/
}
Scientific notation uses a standard-notation, floating-point number plus a suffix that specifies a
power of 10
by which the number is to be multiplied.
The exponent is indicated by an E or e followed by a decimal number, which can be positive or negative.
For example, 6.02E23
, 314159E-05
, and 4e+100
.
public class Main {
public static void main(String[] argv) {
double d1 = 6.022E23;
double d2 = 314159E-05;
double d3 = 2e+100;
// ja v a 2 s.c om
System.out.println("d1 is " + d1);
System.out.println("d2 is " + d2);
System.out.println("d3 is " + d3);
}
}
The output generated by this program is shown here:
You can explicitly specify a double literal by appending a D or d.
public class Main {
public static void main(String args[]) {
double d = 3.14159D;
System.out.print(d);//3.14159
} /*from j a va2 s . c o m*/
}
Java's floating-point calculations are capable of returning
+infinity
, -infinity
, +0.0
, -0.0
,
and NaN
dividing a positive number by 0.0
returns +infinity
.
For example, System.out.println(1.0/0.0);
outputs Infinity.
public class Main{
public static void main(String[] args) {
System.out.println(1.0/0.0);/* j ava 2 s. c om*/
}
}
double Infinity
Dividing a negative number by 0.0
outputs -infinity
.
For example, System.out.println(-1.0/0.0);
outputs -Infinity
.
public class Main{
public static void main(String[] args) {
System.out.println(-1.0/0.0);/*from j a v a2s . c om*/
}
}
Output:
double NaN
Dividing 0.0
by 0.0
returns NaN
.
square root of a negative number is NaN
.
For example, System.out.println(0.0/0.0)
and
System.out.println(Math.sqrt(-1.0))
output NaN.
Dividing a positive number by +infinity
outputs +0.0
.
For example, System.out.println(1.0/(1.0/0.0));
outputs +0.0
.
Dividing a negative number by +infinity
outputs -0.0
.
For example, System.out.println(-1.0/(1.0/0.0));
outputs -0.0
.
public class Main {
public static void main(String[] args) {
Double d1 = new Double(+0.0);
System.out.println(d1.doubleValue());
/* j a va 2 s.c o m*/
Double d2 = new Double(-0.0);
System.out.println(d2.doubleValue());
System.out.println(d1.equals(d2));
System.out.println(+0.0 == -0.0);
}
}
The following code parses command-line arguments into double precision floating-point values
public class Main {
public static void main(String[] args) {
if (args.length != 3) {
System.err.println("usage: java Main value1 op value2");
System.err.println("op is one of +, -, *, or /");
return;//from ja v a 2 s .co m
}
try {
double value1 = Double.parseDouble(args[0]);
double value2 = Double.parseDouble(args[2]);
if (args[1].equals("+")) {
System.out.println(value1 + value2);
} else if (args[1].equals("-")) {
System.out.println(value1 - value2);
} else if (args[1].equals("*")) {
System.out.println(value1 * value2);
} else if (args[1].equals("/")) {
System.out.println(value1 / value2);
} else {
System.err.println("invalid operator: " + args[1]);
}
} catch (Exception nfe) {
System.err.println("Bad number format: " + nfe.getMessage());
}
}
}
The Double
class wraps a value of the primitive type double in an object.
An object of type Double
contains a single field whose type is double.
Double
class provides several methods for converting a double to a String and a String to a double.
Next chapter...
What you will learn in the next chapter:
- How to find max and min values for double type
- How to create double class with constructor
- How to compare two double values
- How to find out if a double value is an infinite large value, or it is not a number