The term data type refers to the type of data that can be stored in a variable. Sometimes, Java is called a “strongly typed language” because when you declare a variable, you must specify the variable’s type. Then the compiler ensures that you don’t try to assign data of the wrong type to the variable. The following example code generates a compiler error:
int x;
x = 3.1415;
Because x is declared as a variable of type int (which holds whole numbers), you can’t assign the value 3.1415 to it.
Java distinguishes between two kinds of data types: primitive types and reference types. Primitive types are the data types defined by the language itself. By contrast, reference types are types defined by classes in the Java application programming interface (API) or by classes you create rather than by the language itself.
Java defines eight primitive types:
TypeExplanation
intA 32-bit (4-byte) integer value
shortA 16-bit (2-byte) integer value
longA 64-bit (8-byte) integer value
byteAn 8-bit (1-byte) integer value
floatA 32-bit (4-byte) floating-point value
doubleA 64-bit (8-byte) floating-point value
charA 16-bit character using the Unicode encoding scheme
booleantrue or false value