Java floating-point literals Question 1

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    System.out.println("1.0 / 3.0 is " + 1.0 / 3.0); 
    System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); 
    System.out.println("1.0D / 3.0D is " + 1.0D / 3.0D); 
  }
}


1.0 / 3.0 is 0.3333333333333333
1.0F / 3.0F is 0.33333334
1.0D / 3.0D is 0.3333333333333333

Note

The double type values are more accurate than the float type values.

Floating-point literals are written with a decimal point.

By default, a floating-point literal is treated as a double type value.

For example, 5.0 is considered a double value, not a float value.

To mark a float, use the letter f or F.

To mark a double, use the letter d or D.

For example:

100.2f or 100.2F for a  float number
100.2d or  100.2D for a double number. 



PreviousNext

Related