When you define a variable in Java, you must tell the compiler what kind of a variable it is. That is, whether it will be expected to store an integer, a character, or some other kind of data. This information tells the compiler how much space to allocate in the memory depending on the data type of the variable. The data type of a variable, therefore, determines the type of data that can be stored in the variable and the operations that can be performed on this data.

Java is a strongly typed language. This means that every variable has a type, every expression has a type, and every type is strictly defined. It also means that all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility by the Java compiler. Any type mismatches are considered errors and need to be corrected before compilation is over.

In Java programming language, data types are mainly divided into two categories and they are as follows:

  • Primitive data types
  • Reference data types

Primitive Data Types

The Java programming language provides primitive data types to store and represent data. A primitive data type also called built-in data types, stores a single value, normally a literal of some sort, such as a number or a character.

The table below lists the primitive data types in Java.

Data Type Description Range
byte 8-bit signed integer -128 to 127
short 16-bit signed integer -32768 to 32767
long 64-bit signed integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32-bit signed integer -2,147,483,648 to 2,147,483,647
float 32-bit floating-point variable -3.40292347E+38 to +3.40292347E+38
boolean Stores a true or false value true or false
char 16-bit Unicode character 0 to 65535
double 64-bit floating-point variable -1.79769313486231570E+308 to 1.79769313486231570E+308

Variables of Primitive Data Types

Whenever a variable is declared, it is either assigned a value (if the variable has been declared and initialized on the same line) or it holds a default value (if the variable has only been declared and not initialized). More than one variable of the same data type can be declared in Java by using the comma (,) separator.

The following code demonstrates the declaration and initialization of variables in Java.

Code Snippet:

int empNumber;
float salary;
char gender = ‘M’;
double shareBalance = 456790.897;
boolean ownVehicle = false;

empNumber = 101;
salary = 6789.50f;

System.out.println(“Employee Number: “+empNumber);
System.out.println(“Salary: “+salary);
System.out.println(“Gender: “+gender);
System.out.println(“Share Balance: “+shareBalance);
System.out.println(“Owns vehicle: “+ownVehicle);

The snippet demonstrates how to declare and initialize variables in Java. Here, variables of type int, float, char, double, and boolean are declared. All float values need to have an f appended at their end. Values are assigned to each of these variables and are displayed using the println() method.

Reference Data Types

A variable of a reference data type holds the reference to the actual value or a set of values represented by the variable. The table below displays three reference data types that are available in Java.

Data Type Description
Array A collection of several items of the same data type such as names of students.
Class A collection of variables and methods. For example, class Student containing the complete details of the students and the methods that operate on the details.
Interface A type of class in Java used to implement multiple inheritance.