Data Type : 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.
Two kinds of data types:
- Primitive types are the data types defined by the language itself.
- 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:
|int||A 32-bit (4-byte) integer value|
|short||A 16-bit (2-byte) integer value|
|long||A 64-bit (8-byte) integer value|
|byte||An 8-bit (1-byte) integer value|
|float||A 32-bit (4-byte) floating-point value|
|double||A 64-bit (8-byte) floating-point value|
|char||A 16-bit character using the Unicode encoding scheme|
|boolean||A true or false value (1Bit)|
Default Values of Java’s Primitive Types
Java primitive data types are initialized to some default values when they are declared as class members.
While programming in Java you declare and use variables at two places. First, inside a function those are local to that function. And second, as a class member.
When a variable is declared local to a function it must be initialized or assigned before its first use otherwise compiler reports an error “variable <variable name> might not have been initialized”.
But when a variable is declared as a class member or field, it is not always essential to assign a value to the member. In that case class members are initialized to some default values by the compiler. However, leaving class members uninitialized is not considered a good practice.
The following table lists the default values for Java’s primitive data types.
|String or other object||null|