In Java, variables are classified into different types based on the kind of data they can store. The variable types in Java can be broadly categorized into two groups: primitive data types and reference data types.
Primitive Data Types:
Primitive data types represent the most basic data types available in Java. They are used to store simple values. There are eight primitive data types in Java:
byte: 8-bit signed integer
short: 16-bit signed integer
int: 32-bit signed integer
long: 64-bit signed integer
float: 32-bit floating-point number
double: 64-bit floating-point number
char: 16-bit Unicode character
boolean: Represents true or false values
Example of primitive variable declaration:
int age = 25;
double salary = 50000.50;
char grade = 'A';
boolean isStudent = true;
Reference Data Types:
Reference data types are used to store references or addresses of objects. They include:
Classes: User-defined types
Interfaces: Blueprint of classes
Arrays: Homogeneous collections of elements
Example of reference variable declaration:
String name = "John";
Object obj = new Object(); // Object is a class in Java
int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
It's important to note that primitive data types store actual values, whereas reference data types store references to objects in memory.
Top comments (0)