您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Content Index

Content Page # 34

Real number types

A real number can have values that are in between integers. The informal way to describe such number in English is as 'decimal' numbers.

Examples of real numbers include: 0.0, 34.6, 99.2, -0.002 etc.

Java offers two ways to represent and work with different types of real number. The difference between them are both the range and precision of values they can represent. By precision, it is mean the number of decimal places that numbers can be stored to.

The two floating point number types are:

  • float
  • double

Real numbers are only approximated by float and double, because they have limited precision. For example, there is no way to represent the result of '1 divided by 3' exactly as a decimal number, as it requires indefinite precision (0.333333333….). The double type gives about 17 digits precision, which is enough for most purposes.

Since most mathematical operations are now done by the computer's hardware, there is little to be gained by using the float data type rather than double.

In fact, if you don't indicate otherwise, the Java compiler will assume that you want double and not float precision. For example if you write:

2.1 + 3.4

Java will evaluate this expression to a value of type double.

Unless you are writing quite sophisticated mathematical programs, you probably don't need to worry too much about these issues.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg