Java Data Types: Comprehensive Guide and Examples
Table of Content:
Data types in Java
Datatype is a spacial keyword used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.Data types represent the different values to be stored in the variable. Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. Java provides eight primitive data types for numeric values, characters, and Boolean values. This section introduces numeric data types.
In general every programming language is containing three categories of data types. They are
- Fundamental or primitive data types
- Derived data types
- User defined data types.
Primitive Data Types
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword.
Categories of Primitive Data Types:
Type | Description | Default | Size | Example Literals |
---|---|---|---|---|
boolean | true or false | false | 1 bit | true, false |
byte | twos complement integer | 0 | 8 bits | (none) |
char | Unicode character | \u0000 | 16 bits | 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß' |
short | twos complement integer | 0 | 16 bits | (none) |
int | twos complement integer | 0 | 32 bits | -2, -1, 0, 1, 2 |
long | twos complement integer | 0 | 64 bits | -2L, -1L, 0L, 1L, 2L |
float | IEEE 754 floating point | 0.0 | 32 bits | 1.23e100f, -1.23e-100f, .3f, 3.14F |
double | IEEE 754 floating point | 0.0 | 64 bits | 1.23456e300d, -1.23456e-300d, 1e1d |
Type | Size | Range |
---|---|---|
byte | 8 bits | -128 .. 127 |
short | 16 bits | -32,768 .. 32,767 |
int | 32 bits | -2,147,483,648 .. 2,147,483,647 |
long | 64 bits | -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 |
float | 32 bits | 3.40282347 x 1038, 1.40239846 x 10-45 |
double | 64 bits | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 |
Data Type | Default Value | Default size |
---|---|---|
boolean | false | 1 bit |
char | '\u0000' | 2 byte |
byte | 0 | 1 byte |
short | 0 | 2 byte |
int | 0 | 4 byte |
long | 0L | 8 byte |
float | 0.0f | 4 byte |
double | 0.0d | 8 byte |
Derived data types
Derived data types are those whose variables allow us to store multiple values of same type. But they never allows to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In general derived data type can be achieve using array.
Categories of Derived Data Types:
Example
int a[] = {10,20,30}; // valid int b[] = {100, 'A', "ABC"}; // invalid
Reference Data Types
User defined data types are those which are developed by programmers by making use of appropriate features of the language.Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.
Default value of any reference variable is null.
Reference Data Types:
A reference variable can be used to refer any object of the declared type or any compatible type.
ExampleGender male = new Gender("yes");