Java double type creation and comparison
In this chapter you will learn:
- How to find max and min values for double type
- How to create double class with constructor
- How to find out if a double value is an infinite large value, or it is not a number
double type max/min values
Double class defines the following constants to stores its min, max value and its size.
static int MAX_EXPONENT
stores maximum exponent a finite double variable may have.static double MAX_VALUE
stores largest positive finite value of type double.static int MIN_EXPONENT
stores minimum exponent a normalized double variable may have.static double MIN_NORMAL
stores smallest positive normal value of type double.static double MIN_VALUE
stores smallest positive nonzero value of type double.static double NaN
stores Not-a-Number (NaN) value of type double.static double NEGATIVE_INFINITY
stores negative infinity of type double.static double POSITIVE_INFINITY
stores positive infinity of type double.static int SIZE
stores the number of bits used to represent a double value.
The following code finds out the min/max value a double type variable can have.
It also prints out the size of
a double
value.
public class Main {
public static void main(String[] args) {
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
System.out.println(Double.SIZE);// java 2 s . c o m
}
}
The output:
public class Main {
public static void main(String[] args) {
System.out.println(Double.MAX_EXPONENT);
System.out.println(Double.MIN_EXPONENT);
System.out.println(Double.MIN_NORMAL);
/*from java2s . c o m*/
}
}
The output:
The following code uses the Double.NEGATIVE_INFINITY
to check a double value.
public class Main {
public static void main(String args[]) {
//from ja v a 2s .c o m
double d = -10.0 / 0.0;
if (d == Double.NEGATIVE_INFINITY) {
System.out.println("d = " + d);
}
}
}
The output:
Create Double class with Constructor
We can create a double
type object with the following two constructors.
Double(double value)
creates a Double object that represents the primitive double argument.Double(String s)
creates a Double object that represents the double value represented by the string.
The following code creates two Double
type object and one is from
double primitive type and another
is from a string value.
public class Main {
public static void main(String[] args) {
System.out.println(new Double(1.1));
System.out.println(new Double("1.2"));
//from ja va 2 s. c om
}
}
The output:
Is a double value an infinite large value, or it is not a number
isInfinite()
returns true
if the value being tested is
infinitely large or small in magnitude.
isNaN()
returns true if the value being tested is not a number.
boolean isInfinite()
returns true if this Double value is infinitely large in magnitude, false otherwise.static boolean isInfinite(double v)
returns true if the specified number is infinitely large in magnitude, false otherwise.boolean isNaN()
returns true if this Double value is a Not-a-Number (NaN), false otherwise.static boolean isNaN(double v)
returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
The following code divides 1
by 0
and creates a infinite double value and then
its uses the isInfinite()
to check.
public class Main {
public static void main(String[] args) {
Double double1 = new Double(1/0.0);
//from ja v a 2 s .com
System.out.println(double1.isInfinite());
}
}
The output:
public class Main {
public static void main(String[] args) {
Double double1 = new Double(0.0/0.0);
/*from jav a 2 s . com*/
System.out.println(Double.isNaN(double1));
System.out.println(double1.isNaN());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How to convert double value as byte, double, float, int, long, short
- How to convert string value to double
- How to convert double value to string
- How to get the hexadecimal string representation
- How to do bit oriented calculation for double type