Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

java.lang.Double

Double is a class available in the java.lang package, and we use a Double class object to wrap double literals as per the business logic of the application.

Important points of java.lang.Double:

1. The java.lang.Double class is a final type class, so no other class can inherit from the Double class, and we are not able to change the implementation of its methods.
2. The Double class objects are immutable in nature.

            
publicclass JTC {
   publicstaticvoid main(String[] args) {
      Double d1 = newDouble(12.34);
      Double d2 = newDouble(45.67);
      Double d3 = d1 + d2;
      System.out.println("d1 == d3 :- " + (d1 == d3));
      System.out.println("d2 == d3 :- " + (d2 == d3));
   }
}
    d1 == d3 :- false
    d2 == d3 :- false
            

In the above example, we can observe that we are wrapping two double-type literals, 12.34 and 45.67, using two Double-type objects, d1 and d2, respectively. We then add d1 and d2, storing the result in Double d3. After all these activities, we compare d1 with d3 and d2 with d3, both resulting in 'false.' This proves that we can't make any changes to the existing objects of the java.lang.Double class, as they are immutable objects.

Important Constructor of java.lang.Double:

1. public java.lang.Double(double): It constructs a new object of the java.lang.Double type, and this newly created object wraps the specified double-type literals.

2. public java.lang.Double(java.lang.String) throws java.lang.NumberFormatException: It creates a new object of the java.lang.Double type, where the newly created object represents the specified String object. When using this constructor to create an object of the java.lang.Double class, the string literals must be in a parsable format of double-type literals; otherwise, a NumberFormatException may occur. Additionally, passing null as an argument can result in a NullPointerException.

            
publicclass JTC {
   publicstaticvoid main(String[] args) {
      Double d1 = newDouble(12.34);
      System.out.println("d1 :- " + d1);
      Double d2 = newDouble("12.34");
      // Double d3 = new Double(null); ---> NullPointerException
      // Double d4 = new Double("abcd"); ---> NumberFormatException
      Double d5 = newDouble("\n12.34\t");
      System.out.println("d5 :- " + d5);
      // Double d6 = new Double("12.3\t4"); ---> NumberFormatException
   }
}
    d1 :- 12.34
    d5 :- 12.34
            

Important Variable of java.lang.Double:

1. public static final double POSITIVE_INFINITY: A constant holding the positive infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0x7ff0000000000000L).
The value displayed on the console is INFINITY.
When we perform an operation using double-type operands, and the result, according to mathematics, is + infinity, then we get the same result in the output.

2. public static final double NEGATIVE_INFINITY: A constant holding the negative infinity of type double. It is equal to the value returned by Double.longBitsToDouble(0xfff0000000000000L).
The value displayed on the console is INFINITY.
When we perform an operation using double-type operands, and the result, according to mathematics, is - infinity, then we get the same result in the output.

3. public static final double NaN: A constant holding a Not-a-Number (NaN) value of type double. It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L).
The value displayed on the console is NaN.
When we perform an operation using double-type operands, and the result, according to mathematics, is undefined, then we get the same result in the output.

4. public static final double MAX_VALUE: A constant holding the largest positive finite value of type double, which is 1.7976931348623157E308.

5. public static final double MIN_VALUE: A constant holding the smallest positive nonzero value of type double, which is 4.9E-324.

            
publicclass JTC {
   publicstaticvoid main(String[] args) {
      System.out.println("Double.POSITIVE_INFINITY :- " + Double.POSITIVE_INFINITY);
      System.out.println("Double.NEGATIVE_INFINITY :- " + Double.NEGATIVE_INFINITY);
      System.out.println("Double.NaN :- " + Double.NaN);
      System.out.println("Double.MAX_VALUE :- " + Double.MAX_VALUE);
      System.out.println("Double.MIN_VALUE :- " + Double.MIN_VALUE);
      System.out.println(12.34 f / 0.0 f);
      System.out.println(-12.34 f / 0.0 f);
      System.out.println(0.0 f / 0.0 f);
   }
}
    Double.POSITIVE_INFINITY :- Infinity
    Double.NEGATIVE_INFINITY :- -Infinity
    Double.NaN :- NaN
    Double.MAX_VALUE :- 1.7976931348623157E308
    Double.MIN_VALUE :- 4.9E-324
    Infinity
    -Infinity
    NaN
            

Important Method of java.lang.Double:

1. public static java.lang.String toHexString(double): It converts the specified double-type value from decimal format to hexadecimal format and returns it as a String.

2. public static double parseDouble(java.lang.String) throws java.lang.NumberFormatException: It parses the specified String argument into a double-type value. When using this method, the String must not be null; otherwise, a NullPointerException is thrown. Additionally, the String argument must be in a parsable format of double type; otherwise, a NumberFormatException is thrown.

3. public static java.lang.Double valueOf(java.lang.String) throws java.lang.NumberFormatException: It returns a new object of the java.lang.Double type, which wraps the same double-type value specified as a String argument. If the String argument is null, a NullPointerException is thrown, and if the String is not in the format of double-type literals, a NumberFormatException is thrown.

4. public static java.lang.Double valueOf(double): It returns a new object of the java.lang.Double type, which wraps the same double-type value specified as an argument.

5. public byte byteValue(): It returns a byte-type value obtained through explicit type-casting of the specified double-type value along with the current working object.

6. public short shortValue(): It returns a short-type value obtained through explicit type-casting of the specified double-type value along with the current working object.

7. public int intValue(): It returns an int-type value obtained through explicit type-casting of the specified double-type value along with the current working object.

8. public long longValue(): It returns a long-type value obtained through explicit type-casting of the specified double-type value along with the current working object.

9. public float floatValue(): It returns a float-type value obtained through explicit type-casting of the specified double-type value along with the current working object.

10. public double doubleValue(): It returns the double-type value of the current working object of the java.lang.Double class.

11. public boolean equals(java.lang.Object): It compares two objects of the java.lang.Double class based on their double-type values.

12. public int compareTo(java.lang.Double): It compares two Double-type objects based on their double-type values. If the current working object has a larger value compared to the argument, it returns 1. If both objects have the same value, it returns 0. If the current working object contains a smaller value compared to the argument, it returns -1.

            
publicclass JTC {
   publicstaticvoid main(String[] args) {
      String s1 = Double.toHexString(12.34);
      System.out.println("s1 :- " + s1);
      doubled1 = Double.parseDouble("12.34");
      // double d2 = Double.parseDouble(null); ---> NullPointerException
      // double d3 = Double.parseDouble("abcd"); ---> NumberFormatException
      doubled4 = Double.parseDouble("\n12.34\t");
      System.out.println("d4 :- " + d4);
      // double d5 = Double.parseDouble("12.3\t4"); ---> NumberFormatException
      doubled6 = Double.valueOf("102.345");
      System.out.println("d6 :- " + d6);
      // double d7 = Double.valueOf(null); ---> NullPointerException
      doubled7 = Double.valueOf("\n102.345\b");
      System.out.println("d7 :- " + d7);
      // double d8 = Double.valueOf("1\n2.56"); ---> NumberFormatException
      doubled8 = Double.valueOf(-1653.567);
      System.out.println("d7 :- " + d8);
      Double double1 = newDouble(102.456);
      byteb1 = double1.byteValue();
      System.out.println("b1 :- " + b1);
      shorts2 = double1.shortValue();
      System.out.println("s2 :- " + s2);
      inti1 = double1.intValue();
      System.out.println("i1 :- " + i1);
      longl1 = double1.longValue();
      System.out.println("l1 :- " + l1);
      floatf1 = double1.floatValue();
      System.out.println("f1 :- " + f1);
      doubled9 = double1.doubleValue();
      System.out.println("d9 :- " + d9);
      Double double2 = newDouble(12.34);
      System.out.println(double1.equals(double2));
      Double double3 = newDouble(12.34);
      System.out.println(double2.equals(double3));
      System.out.println(double1.compareTo(double2));
      System.out.println(double2.compareTo(double1));
      System.out.println(double2.compareTo(double3));
   }
}
    s1 :- 0x1.8ae147ae147aep3
    d4 :- 12.34
    d6 :- 102.345
    d7 :- 102.345
    d7 :- -1653.567
    b1 :- 102
    s2 :- 102
    i1 :- 102
    l1 :- 102
    f1 :- 102.456
    d9 :- 102.456
    false
    true
    1
    -1
    0
            

In this example, we are using different important methods of the java.lang.double class and trying to understand their implementation and restrictions.