Skip to Content

Java Double Example

  1. /*
  2.   Java Double Example
  3.   This example shows how an object of Double can be declared and used.
  4.   Double is a wrapper class provided to wrap double primitive value. It has a single
  5.   field of type double.
  6. */
  7.  
  8. public class JavaDoubleExample {
  9.  
  10.   public static void main(String[] args) {
  11.    
  12.     //create a Double object using one of the below given constructors
  13.    
  14.     //1. Create an Double object from double primitive type
  15.     double d = 10.10;
  16.     Double dObj1 = new Double(d);
  17.     System.out.println(dObj1);
  18.    
  19.     /*
  20.     2. Create Double object from String. Please note that this method can
  21.     throw NumberFormatException if string doesnt contain parsable number.
  22.     */
  23.     Double dObj3 = new Double("25.34");
  24.     System.out.println(dObj3);    
  25.    
  26.    
  27.    
  28.   }
  29. }
  30.  
  31.  
  32. /*
  33. Output of the program would be
  34. 10.1
  35. 25.34
  36. */

Advertisement:
Join 1000+ fellow learners! Enter your email address below: