Java double type

In this chapter you will learn:

  1. What is Java double type
  2. How to create double type Literals
  3. How to get the value of double infinity

double type

double represents double-precision numbers. double is 64-bit width and its range is from 4.9e-324 to 1.8e+308 approximately.

Here is a program that uses double variables to compute the area of a circle:

public class Main {
  public static void main(String args[]) {
    double pi, r, a;
/*from   ja v a  2  s  .  com*/
    r = 10.8888; // radius of circle
    pi = 3.1415926; // pi, approximately
    a = pi * r * r;

    System.out.println("Area of circle is " + a);
  }
}

The output:

double type Literals

double type numbers have decimal values with a fractional component. They can be expressed in either standard or scientific notation. Standard notation consists of a whole number component followed by a decimal point followed by a fractional component. For example, 2.0, 3.14159, and 0.6667.

public class Main { 
    public static void main(String args[]) { 
        double d =  3.14159; 
        System.out.print(d);//3.14159    
    } //from   ja v a2 s  .c o  m
}

Scientific notation uses a standard-notation, floating-point number plus a suffix that specifies a power of 10 by which the number is to be multiplied. The exponent is indicated by an E or e followed by a decimal number, which can be positive or negative. For example, 6.02E23, 314159E-05, and 4e+100.

public class Main {
  public static void main(String[] argv) {
    double d1 = 6.022E23;
    double d2 = 314159E-05;
    double d3 = 2e+100;
//from  j ava  2s.  c  om
    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:

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

public class Main { 
    public static void main(String args[]) { 
        double d =  3.14159D; 
        System.out.print(d);//3.14159    
    } /*  ja v  a2 s  . com*/
}

Java's floating-point calculations are capable of returning +infinity, -infinity, +0.0, -0.0, and NaN

dividing a positive number by 0.0 returns +infinity. For example, System.out.println(1.0/0.0); outputs Infinity.

public class Main{
  public static void main(String[] args) {
    System.out.println(1.0/0.0);/*  java  2s  .  c o  m*/
  }

}

double Infinity

Dividing a negative number by 0.0 outputs -infinity. For example, System.out.println(-1.0/0.0); outputs -Infinity.

public class Main{
  public static void main(String[] args) {
    System.out.println(-1.0/0.0);// j  a v  a  2s  .c om
  }

}

Output:

Next chapter...

What you will learn in the next chapter:

  1. What is double type NaN(Not a Number)
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing