Double Floating-Point Literals

Floating-point numbers represent decimal values with a fractional component.

Floating-point numbers can be expressed in either standard or scientific notation.

The following code uses the standard notation.


public class Main {
  public static void main(String[] argv) {
    double d1 = 2.0;
    double d2 = 3.14159;
    double d3 = 0.6667;

    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:


d1 is 2.0
d2 is 3.14159
d3 is 0.6667

Scientific notation

Scientific notation uses a standard-notation, plus a suffix that specifies a power of 10.

The exponent is indicated by an E or e followed.

The exponent can be positive or negative.


public class Main {
  public static void main(String[] argv) {
    double d1 = 6.022E23;
    double d2 = 314159E-05;
    double d3 = 2e+100;

    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:


d1 is 6.022E23
d2 is 3.14159
d3 is 2.0E100

You can explicitly specify a double literal by appending a D or d.

The default double type consumes 64 bits of storage.

The float type requires only 32 bits.


public class Main {
  public static void main(String[] argv) {
    double d = 1.1d;

    System.out.println("d is " + d);

  }
}

The output generated by this program is shown here:


d is 1.1
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.