How to Program in Java for the Beginner: Floating-point Numbers and Expressions

Page content

Floating-point numbers – the types float and double

A floating-point number is one which may have a fractional part. A floating-point constant can be written in one of two ways:

• the normal way, with an optional sign, and including a decimal point; for example, -3.75, 0.537, 81.792, 47.0;

• using scientific notation, with an optional sign, including a decimal point and including an ‘exponent’ part; for example, -0.375E1 meaning “-0.375 multiplied by 10 to the power 1”, that is, -3.75. Similarly, 0.537 can be written as 5.37e-1, that is, “5.37 x 10”. The exponent can be specified using either e or E.

Note that there are several ways to write the same number. For example, the following all represent the same number 27.96:







In Java, we can declare a floating-point variable using either float or double. A float value is normally stored as a 32-bit floating-point number, giving about 6 or 7 significant digits. A double value is stored as a 64-bit floating-point number, giving about 15 significant digits.

A floating-point constant is of type double unless it is followed by f or F, in which case it is of type float. Thus 3.75 is of type double but 3.75f (or 3.75F) is of type float. Most calculations are done using double precision. The type float is useful if you need to store lots of floating-point numbers and you wish to use as little storage as possible (and can live with 6 or 7 digits of precision).

In this series, we will mostly use double when working with floating-point numbers.

Floating-point expressions

Floating-point expressions can be written using the following operators:

  • addition

- subtraction

* multiplication

/ division

These operate as expected; in particular, division is performed in the usual way so that, for example, 19.0/5.0 gives the value 3.8.

Assume op1 and op2 are the two operands of an operator (in the expression 2+5: op1=2 and op2=5 are operands; + is the operator), the following shows the type of calculation performed:

  • op1 is float and op2 is float, type of caculation is float
  • op1 is double and op2 is float, type of caculation is double
  • op1 is float and op2 is double, type of caculation is double
  • op1 is double and op2 is double, type of caculation is double

Thus float is performed only if both operands are float; otherwise double is performed.


Java Programming – A Beginner’s Course

Related programming references

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions