Type |
Description |
Size |
---|---|---|
int |
The integer type, with range -2,147,483,648 . . . 2,147,483,647 | 4 bytes |
byte |
The type describing a single
byte, with range -128 . . . 127 |
1 byte |
short |
The short integer type, with
range -32768 . . . 32767 |
2 bytes |
long |
The long integer type, with range -9,223,372,036,854,775,808 . . . 9,223,372,036,854,775,807 | 8 bytes |
double |
The double-precision
floating-point type, with a range of about ±10^{308} and
about 15 significant decimal digits |
8 bytes |
float |
The single-precision floating-point type, with a range of about ±10^{38} and about 7 significant decimal digits | 4 bytes |
char |
The character type, representing
code units in the Unicode encoding scheme |
2 bytes |
boolean |
The type with the two truth
values false and true |
1 bit |
The larger number of bytes a data types uses, the larger the value a variable of that type can hold; but also, the longer it takes for the computer to perform operations on variables of that type... |
Every integer type has a range of values |
int x; x = 2147483647; x = x + 3; // Should be 2147483650 // But this value is // outside the int range !!! // Instead, x is -2147483646 !!! |
long x; x = 2147483647; x = x + 3; // No overflow... |
The problem just gets alleviated (delayed...)
Try changing the assignment in the test program to:
x = 9223372036854775807L;(you need an "L" at the end to denote a long number) and you will see the overflow problem again...
Floating point numbers
in a computer
DO NOT
represent a value
exactly
In other words, the value of a number represented by a floating point number is always an approximation |
double f = 4.35; System.out.println(f); // Prints 4.35 System.out.println(100*f); // Prints 434.99999999999994 |
These programs use specially written packages of software.
In Java, there is a class called BigDecimal that allow you to manipulate very large decimal numbers without loss of precision
If you want to learn more on this, read Advanced Topic 4.1 on page 107 in your book.