Data Types: A data type or simply type is a classification identifying one of various types of data, such as whole numbers or fractional numbers.
Data Type determines:
- the possible values for that type
- the operations that can be done on values of that type
- the meaning of the data
- the way values of that type can be stored
Java data types are mainly of two types:
- Primitive Types
- Reference Types
Primitive Types:
- Primitive types are defined by the language itself.
- The memory location of primitive type variable contains the actual value of the variable
Reference Types:
- Reference types are defined by classes in Java API or by classes created by the developers rather than by the language itself.
- The memory location of reference type variable contains an address that indicates the memory location of the actual object.
Primitive Types vs. Reference Type:
Primitive Types | Reference Types |
1. Primitive types are the fundamental, built in (as keyword) types | 1. Reference types are constructed from primitive types either through class definition, interface definition or the use of arrays. |
2. A variable of any primitive type contains a single value of the appropriate size and format. | 2. A variable of a reference type holds a null reference or a reference to an instance of a class |
3. Values of these types cannot be decomposed | 3. A reference type can be decomposed into several values. |
List of Primitive Types:
Types | Characteristics | Memory Size (byte) | Minimum Value | Maximum Value |
byte | Whole numbers | 1 | -128 | 127 |
short | 2 | -32768 | 32767 | |
int | 4 | -2147483648 | 2147483647 | |
long | 8 | -9223372036854775808 | 9223372036854775807 | |
float | Fractional numbers | 4 | 1.4E-45 | 3.4028235E38 |
double | 8 | 4.9E-324 | 1.7976931348623157E308 | |
char | Any single symbol | 2 | '\u0000', that is 0 | '\uffff' that is 65535 |
boolean | true/false | 1 |
Reference Types Example:
Java classes, interfaces and arrays are reference types.
- String
- Date
- And many more …
Further reading:
- Data type available at http://en.wikipedia.org/wiki/Data_type
- Doug Lowe (2011) Java All-in-One For Dummies, Hoboken: John Wiley & Sons, Inc.
- Types, Values & Variables available at http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html