A datatype is programming is a way of representing information or values in the computer’s memory.
Support for simple kinds of values are built into the computer.
For instance virtually all computers since the earliest days of computers have been built to do math.
Java has two (that we care about) data types for representing numbers.
int - 32-bit integer values
double - 64-bit floating point values that can have a fractional parts.
int values are represented in the computer with a 32-bits in a pretty simple format.
double values are bigger (64-bits) and a bit more complex. They work scientific notation, encoding a coefficient, an exponent, and the sign of the number allowing us to represent a wider range of values though many will only be approximate.
There are tradeoffs of speed (how fast the computer can compute) and space (how much memory the values use) that make it worthwhile to have two distinct types.
Integer math is very fast because the circuitry is very simple.
So if that’s all you need it’s the best choice.
And there are contexts where only integers make sense so it’s good to know that you’ll never end up with a fractional number.
Floating point math, on the other hand, is more complex and thus slower.
But if you need to do something approximating real math with division and square roots and so forth, you need to be able to approximate real numbers.
Any number with a fractional part including very small fractions. (e.g. \(1.5\), \(.0001234\), etc.)
Any very large number (greater than \(2^{31} - 1\) (approximately two billion) or less than \(-2^{31}\) (approximately negative two billion).
Number of cookies in a cookie jar?
Average weight of the apples in a bag in kilograms?
Number of stars in the Milky Way?
Distance between atoms in a solid in nanometers?
We can write both int and double literal values in our programs:
1234 is an int
1234.0 is a double
0.0001 is a double
1.989e+30 is a double (and the mass of the sun in kg)