If you come from a C or C++ background, keep in mind that Java is more strictly typed than either language. For example, in C/C++ you can assign a floating-point value to an integer. In Java, you cannot. Also, in C there is not necessarily strong type-checking between a parameter and an argument. In Java, there is. You might find Java’s strong type-checking a bit tedious at first. But remember, in the long run it will help reduce the possibility of errors in your code.
The Simple Types
Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float, double, and boolean. These can be put in four groups:
■ Integers -This group includes byte, short, int, and long, which are for wholevalued
■ Floating-point numbers -This group includes float and double, which represent
numbers with fractional precision.
■ Characters -This group includes char, which represents symbols in a character
set, like letters and numbers.
■ Boolean -This group includes boolean, which is a special type for representing
Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers.
The width and ranges of these integer types vary widely, as shown in this table:
Name Width Range
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127
Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. There are two kinds of floating-point types, float and double, which represent single- and double-precision numbers, respectively. Their width and ranges are shown here:
Name Width in Bits Approximate Range
double 64 4.9e–324 to 1.8e+308
float 32 1.4e−045 to 3.4e+038
Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages.
For this purpose, it requires 16 bits. Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
Java has a simple type, called boolean, for logical values. It can have only one of two
possible values, true or false.