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
and binary floating-point numbers (most commonly using the type
Although these numbers are stored in the computer
as binary numbers, you will usually use decimal numbers in your
Java source program, and the Java compiler translates them to the equivalent binary form.
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.
Integers are "whole numbers" and can not have a fractional part. for example, 3, 0, -4.
BigInteger- Unbounded integer range.
BigDecimal- Decimal arithmetic.
bytefor integers that are in the range -128 to +127 because a
byteinteger requires only 1/4 the memory that an
First, if you have only a small amount of data, as many programs do, the effect would be unmeasurable.
Second, 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 and often requires benchmarking (actual tests) to determine the speed of various options.
Large amounts of data are another matter, for example, when using arrays. If you're storing millions of numbers, their size has a definite effect on performance.
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 operators,
which is annoying and makes your code harder to read.
I used to choose the smallest type that would work for my data for efficiency reasons, but have
finally given up and just use
because of the convenience - except for large amounts of data.