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:

  1. int → data type
  2. age → variable name
  3. 25 → value assigned

2. Types of Variables in Java

Java supports three types of variables:

2.1 Local Variables

  1. Declared inside a method or block
  2. Exist only during method execution
  3. Not initialized by default

public class Demo {
public void show() {
int localVar = 10; // local variable
System.out.println(localVar);
}
}

2.2 Instance Variables

  1. Declared inside a class but outside any method
  2. Belong to objects
  3. 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

  1. Declared with static keyword inside class
  2. 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:

  1. Primitive Data Types
  2. Non-Primitive (Reference) Data Types

3.1 Primitive Data Types

TypeSizeDefaultDescription
byte1 byte0Small integer (-128 to 127)
short2 bytes0Integer (-32,768 to 32,767)
int4 bytes0Integer (-2^31 to 2^31-1)
long8 bytes0LLarge integer (-2^63 to 2^63-1)
float4 bytes0.0fSingle precision decimal
double8 bytes0.0dDouble precision decimal
char2 bytes'\u0000'Single character (Unicode)
boolean1 bitfalsetrue/false values

Example:


int age = 25;
float price = 99.99f;
char grade = 'A';
boolean isActive = true;

3.2 Non-Primitive (Reference) Data Types

  1. Examples: String, Arrays, Classes, Interfaces
  2. Store reference to objects, not actual data
  3. 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)

  1. Smaller type → Larger type automatically
  2. Safe conversion

int num = 100;
double d = num; // int → double

4.2 Explicit Casting (Narrowing)

  1. Larger type → Smaller type
  2. May lose data, requires explicit cast

double price = 99.99;
int p = (int) price; // double → int

5. Variable Naming Rules

  1. Must start with a letter, $, or _
  2. Cannot start with a number
  3. Case-sensitive (ageAge)
  4. No Java reserved keywords
  5. 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

  1. Variables store data; types define size, range, operations.
  2. Three types: Local, Instance, Static
  3. Two categories: Primitive and Non-Primitive
  4. Casting allows type conversion
  5. Proper naming and type usage ensures error-free code