Java Variables and Data Types – Complete Guide with Examples and Usage
Learn everything about Java variables and data types, including types of variables, primitive and non-primitive types, scope, default values, and examples for beginners.
Variables and Data Types in Java – Complete Detailed Tutorial
In Java, variables are containers used to store data values. Each variable has a type that defines the size and layout of memory, the range of values, and the operations that can be performed on it.
Understanding variables and data types is fundamental to writing Java programs.
1. What is a Variable?
A variable is a named memory location that stores data which can be changed during program execution.
Syntax:
dataType variableName = value;
Example:
int age = 25;
String name = "John";
Explanation:
int→ data typeage→ variable name25→ value assigned
2. Types of Variables in Java
Java supports three types of variables:
2.1 Local Variables
- Declared inside a method or block
- Exist only during method execution
- Not initialized by default
public class Demo {
public void show() {
int localVar = 10; // local variable
System.out.println(localVar);
}
}
2.2 Instance Variables
- Declared inside a class but outside any method
- Belong to objects
- Initialized with default values
class Car {
String color; // instance variable
}
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.color = "Red";
System.out.println(c.color);
}
}
2.3 Static (Class) Variables
- Declared with
statickeyword inside class - Shared among all objects of the class
class Counter {
static int count = 0; // static variable
Counter() {
count++;
}
}
public class Test {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
System.out.println(Counter.count); // Output: 2
}
}
3. Data Types in Java
Java has two main categories of data types:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
3.1 Primitive Data Types
| TypeSizeDefaultDescription | |||
| byte | 1 byte | 0 | Small integer (-128 to 127) |
| short | 2 bytes | 0 | Integer (-32,768 to 32,767) |
| int | 4 bytes | 0 | Integer (-2^31 to 2^31-1) |
| long | 8 bytes | 0L | Large integer (-2^63 to 2^63-1) |
| float | 4 bytes | 0.0f | Single precision decimal |
| double | 8 bytes | 0.0d | Double precision decimal |
| char | 2 bytes | '\u0000' | Single character (Unicode) |
| boolean | 1 bit | false | true/false values |
Example:
int age = 25;
float price = 99.99f;
char grade = 'A';
boolean isActive = true;
3.2 Non-Primitive (Reference) Data Types
- Examples:
String,Arrays,Classes,Interfaces - Store reference to objects, not actual data
- Can call methods on these objects
Example:
String name = "John Doe";
int[] numbers = {1, 2, 3, 4, 5};
4. Type Casting in Variables
Java allows conversion between data types:
4.1 Implicit Casting (Widening)
- Smaller type → Larger type automatically
- Safe conversion
int num = 100;
double d = num; // int → double
4.2 Explicit Casting (Narrowing)
- Larger type → Smaller type
- May lose data, requires explicit cast
double price = 99.99;
int p = (int) price; // double → int
5. Variable Naming Rules
- Must start with a letter, $, or _
- Cannot start with a number
- Case-sensitive (
age≠Age) - No Java reserved keywords
- Use camelCase convention for readability
Example:
int studentAge;
String studentName;
6. Example Program – Variables and Data Types
public class VariablesDemo {
static String school = "TextNotes Academy"; // static variable
int studentCount = 50; // instance variable
public static void main(String[] args) {
int grade = 10; // local variable
float fees = 1500.50f;
char section = 'A';
boolean isOpen = true;
System.out.println("School: " + school);
System.out.println("Grade: " + grade);
System.out.println("Fees: " + fees);
System.out.println("Section: " + section);
System.out.println("Is Open: " + isOpen);
}
}
Output:
School: TextNotes Academy
Grade: 10
Fees: 1500.5
Section: A
Is Open: true
7. Summary
- Variables store data; types define size, range, operations.
- Three types: Local, Instance, Static
- Two categories: Primitive and Non-Primitive
- Casting allows type conversion
- Proper naming and type usage ensures error-free code