Double Wrapper Class in Java

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.
In addition, this class provides several methods for converting a double to a String and a String to a double, as well as other constants and methods useful when dealing with a double.

Double class Constructors

  • Double(double value) - Constructs a newly allocated Double object that represents the primitive double argument.
  • Double(String s) - Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
Example:
double b = 55.05;
String str = "45";

// Construct two Double objects
Double x = new Double(b);
Double y = new Double(str);

Double class Methods

The class diagram shows a list of APIs/Methods that Integer class provides.
In this post, we will learn a few important methods of Integer wrapper class.
Example: This program demonstrates usage of Double wrapper class methods
package com.javaguides.corejava.wrapperclasses;

/**
 * This class to demonstrate Double wrapper class methods
 * @author javaguides.net
 *
 */
public class DoubleClassExample {
 public static void main(String[] args) {
  double b = 55.05;
  String str = "45";

  // Construct two Double objects
  Double x = new Double(b);
  Double y = new Double(str);

  // toString()
  System.out.println("toString(b) = " + Double.toString(b));

  // valueOf()
  // return Double object
  Double z = Double.valueOf(b);
  System.out.println("valueOf(b) = " + z);
  z = Double.valueOf(str);
  System.out.println("ValueOf(bb) = " + z);

  // parseDouble()
  // return primitive double value
  double zz = Double.parseDouble(str);
  System.out.println("parseDouble(bb) = " + zz);

  System.out.println("bytevalue(x) = " + x.byteValue());
  System.out.println("shortvalue(x) = " + x.shortValue());
  System.out.println("intvalue(x) = " + x.intValue());
  System.out.println("longvalue(x) = " + x.longValue());
  System.out.println("doublevalue(x) = " + x.doubleValue());
  System.out.println("floatvalue(x) = " + x.floatValue());

  int hash = x.hashCode();
  System.out.println("hashcode(x) = " + hash);

  boolean eq = x.equals(y);
  System.out.println("x.equals(y) = " + eq);

  int e = Double.compare(x, y);
  System.out.println("compare(x,y) = " + e);

  int f = x.compareTo(y);
  System.out.println("x.compareTo(y) = " + f);

  Double d = Double.valueOf("1010.54789654123654");
  System.out.println("isNaN(d) = " + d.isNaN());

  System.out.println("Double.isNaN(45.12452) = " + Double.isNaN(45.12452));

  // Double.POSITIVE_INFINITY stores
  // the positive infinite value
  d = Double.valueOf(Double.POSITIVE_INFINITY + 1);
  System.out.println("Double.isInfinite(d) = " + Double.isInfinite(d.doubleValue()));

  double dd = 10245.21452;
  System.out.println("Double.toString(dd) = " + Double.toHexString(dd));

  long double_to_long = Double.doubleToLongBits(dd);
  System.out.println("Double.doubleToLongBits(dd) = " + double_to_long);

  double long_to_double = Double.longBitsToDouble(double_to_long);
  System.out.println("Double.LongBitsToDouble(double_to_long) = " + long_to_double);
 }

}
Output:
toString(b) = 55.05
valueOf(b) = 55.05
ValueOf(bb) = 45.0
parseDouble(bb) = 45.0
bytevalue(x) = 55
shortvalue(x) = 55
intvalue(x) = 55
longvalue(x) = 55
doublevalue(x) = 55.05
floatvalue(x) = 55.05
hashcode(x) = 640540672
x.equals(y) = false
compare(x,y) = 1
x.compareTo(y) = 1
isNaN(d) = false
Double.isNaN(45.12452) = false
Double.isInfinite(d) = true
Double.toString(dd) = 0x1.4029b7564302bp13
Double.doubleToLongBits(dd) = 4666857980575363115
Double.LongBitsToDouble(double_to_long) = 10245.21452

Reference

Comments