# Java Notes

# Numbers

**Two kinds of numbers.**
There are basically two kinds of numbers in Java and most other programming languages:
binary **integers** (most commonly using the type `int`

)
and binary **floating-point** numbers (most commonly using the type `double`

).
Although these numbers are stored in the computer
as binary numbers, you normally write decimal numbers in your
Java source program, and the Java compiler translates them to the equivalent binary form.
Similarly, on output these binary numbers are converted to a decimal character
form that you can read.

**Integers** are "whole numbers" and can not have a fractional part.
for example, 3, 0, -4.

**Floating-point** numbers can have a fractional part and we usually write
them with a decimal point, for example, 3.14159265, 0.01, -4.0.

## Typical Issues

- Deciding which to choose: integer or floating-point.
- Be sure the range (minimum to maximum value) is sufficient.
- Be sure the accuracy (number of digits of precision) is sufficient in floating-point.
- Conversions between integer, floating-point, and String.
- Consequences of integer and floating-point arithmetic.

## Numbers in Java

## Topics to be added

`BigInteger`

- Unbounded integer range.`BigDecimal`

- Decimal arithmetic.- IEEE-754 representation of floating-point numbers, including overflow, underflow, infinity, and NaN.
`strictfp`

- Autoboxing
- Algorithms in Math class
- Wrapper class methods
- Lack of checking for integer overflow
- Bit operations
- Conversion from String to number
- Conversion from number to String - default, NumberFormat, printf

## Number FAQ

- Q: Is it more efficient to use small types (like
`short`

) than`int`

? -
A: In principle small variables (those using fewer bytes) should be faster than
large variables. So on the surface it would seem that you should use, for example,
`byte`

for integers that are in the range -128 to +127 because a`byte`

integer requires only 1/4 the memory that an`int`

does.For large amounts of data, for example, when using arrays, small data types can definitely be more efficient.. If you're storing millions of numbers, their size has a definite effect on performance.

If you only have a small amount of data, using smaller data types may not improve performance at all, or may even slow execution!

*Small effect.*If you have only a small amount of data, as many programs do, the effect would be unmeasurable.*Memory implemenation.*Because entire memory blocks are passed between the CPU and RAM there may not actually be any difference for small amounts of data! The exact way that memory is handled in the hardware is complex involving details of access on the memory chip, transfer through several levels of cache, and possible virtual memory issues. This requires benchmarking (actual tests) to determine the true speed of various options.*Ease of use.*`int`

and`double`

are the types that the Java compiler likes for integer and floating-point constants and expressions, so you will find it*much*easier to use them. If you use other sizes of numbers, you will have to write a lot of*cast*operations, which is annoying and makes your code harder to read. I used to choose the smallest type possible for efficiency reasons, but have finally given up and now just use`int`

and`double`

for everything because of the convenience - except for large amounts of data.*Widening.*Because Java's natural integer type is the 4-byte`int`

, most operations require*widening*`byte`

or`short`

values to 4 bytes before using them. The result is that computations with values shorter than`int`

may be slower because of the widening requirement.

## Webliography

- Check out William Kahan's homepage if you want to find out more about the dark corners of floating-point numbers and Java. www.cs.berkeley.edu/~wkahan/