JAVA DATA TYPES AND VARIABLES – DETAILED
NOTES
In Java, data types define the type of data a variable can store, while variables are containers used
to store data values. Understanding data types and variables is fundamental to Java programming.
1. Data Types in Java
Java data types are classified into two main categories: Primitive Data Types and Non-Primitive
(Reference) Data Types.
1.1 Primitive Data Types
Primitive data types are basic data types that store simple values. Java has eight primitive data
types.
Data Type Size Default Value Example
byte 1 byte 0 byte b = 10;
short 2 bytes 0 short s = 100;
int 4 bytes 0 int i = 1000;
long 8 bytes 0L long l = 100000L;
float 4 bytes 0.0f float f = 10.5f;
double 8 bytes 0.0d double d = 99.99;
char 2 bytes '\u0000' char c = 'A';
boolean 1 bit false boolean flag = true;
Diagram (Primitive Data Types Classification):
Primitive Data Types → byte, short, int, long, float, double, char, boolean
1.2 Non-Primitive (Reference) Data Types
Non-primitive data types store references to objects. They are used to store complex data.
Examples of non-primitive data types include: String, Array, Class, Interface, and Object.
Diagram:
Non-Primitive Data Types → String, Array, Class, Interface, Object
2. Variables in Java
A variable is a container that holds data which can be changed during program execution.
2.1 Types of Variables
Java variables are classified into three types based on their scope and lifetime.
a) Local Variable
A local variable is declared inside a method or block and is accessible only within that block.
Example:
void show() { int x = 10; }
b) Instance Variable
Instance variables are declared inside a class but outside methods. Each object has its own copy.
Example:
class Test { int a = 10; }
c) Static Variable
Static variables belong to the class and are shared among all objects.
Example:
class Test { static int count = 0; }
Diagram (Variable Scope):
Class → Instance Variable
Class → Static Variable
Method → Local Variable
Conclusion
Understanding Java data types and variables is essential for writing efficient and error-free
programs. They form the foundation of Java programming and are frequently asked in exams and
interviews.