Java programming language is a statically typed language, it does not allow to change variable's data type: once declared, they cannot be changed. A variable's type determines the values that the variable can have and the operations that can be performed on it.
There are two major categories of data types in the Java language:
- Primitive data types: specifies the size and type of variable values, and it has no additional methods. Eight primitive data types in Java are boolean, byte, short, int, long, float, double, and char
- Non-primitive data types: they are called reference types because they refer to objects. Examples of non-primitive data types are String, Arrays, Classes and Interfaces.
The main difference between primitive and non-primitive data types are:
- Primitive types are predefined in Java Core. Non-primitive types can be created by programmer (eg: String and several classes and interfaces that are defined in Java Core).
- Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
- A primitive type has always a value, while non-primitive types can be null.
- A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
- A primitive type stored a value and have a fixed size, while non-primitive types don't store the value, but store a reference to that value.
Tutorials in this series:
Java Primitive Data TypesPrimitive types are the most basic data types available within the Java language. The eight primitives defined in Java are : boolean, byte, short, int, long, float, double, and char. These types directly contains the value of that type.
Java StringString is one of the most widely used class in Java. A Java String contains an immutable sequence of Unicode characters. It is an immutable object which means it is constant and can cannot be changed once it has been created.