In this article, we introduce the basic floating-point types – float and double – in Java. We show how to write floating-point constants, how to write floating-point expressions and how expressions are evaluated.

- slide 1 of 3
### 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:

27.96E00

2.796E1

2.796E+1

2.796E+01

0.2796E+02

279.6E-1

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. - slide 2 of 3
### 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. -
op1 is
- slide 3 of 3

#### Introduction to Java Programming

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