Variables are locations in memory that are used to store information.
Java supports a variety of data types, which dictate the type of information that can be stored in a variable.
You create a variable in Java by specifying the type of the variable and an identifier that uniquely identifies the variable.
Let us revisit an earlier example:
The first line of code declares a variable named age whose data type is int, which is an integer (whole number) data type. The second line of code declares a variable named name whose data type is String, which holds text.
Both of these variable declarations set aside memory for each variable. The data type determines the amount of memory set aside.
For example, a Java int
is a 32-bit number, so 32 bits of memory are set aside for an int variable.
Java data types can be grouped into two major categories:
- simple: a core data type that is not derived from any other type and represents a single piece of information (also called a primitive data type)
- composite: a data type based on simple types that is used to represent more complex information