Paramclasses Academy - Blog

Tutorials Details

Java Variables and Data Types Explained with Examples

Java Variables and Data Types Explained with Examples

October 21, 2026

Introduction

Variables and data types are among the most important concepts in Java programming. Every beginner should understand these concepts before moving on to operators, conditions, loops and object-oriented programming.

In this tutorial, you will learn what variables are, how to declare and initialize them, the different data types available in Java, and how to use them in practical programs.

By the end of this guide, you will be able to create variables and store different types of information in a Java program.

What Is a Variable in Java?

A variable in Java is a named storage location used to hold a value that a program can access and, when permitted, change.

For example, a student management program may store a student's name, age, marks and result using variables.

public class Main {
    public static void main(String[] args) {
        int age = 20;
        String name = "Rahul";
        double marks = 85.5;

        System.out.println(name);
        System.out.println(age);
        System.out.println(marks);
    }
}

Output:

Rahul
20
85.5

In this example:

  • age stores an integer.

  • name stores text.

  • marks stores a decimal number.

Each variable has a name, a type and a value.

How to Declare a Variable in Java

Variable declaration tells Java what type of value a variable can hold and what its name is.

Syntax:

dataType variableName;

Example:

int age;
String name;
double salary;

Here, int, String and double are data types, while age, name and salary are variable names.

What Is Variable Initialization?

Variable initialization means assigning an initial value to a variable.

int age = 20;
String name = "Rahul";
double salary = 25000.50;

In the statement int age = 20;:

  • int is the data type.

  • age is the variable name.

  • 20 is the assigned value.

  • = is the assignment operator.

You can also declare a variable first and assign its value later.

int age;
age = 20;

System.out.println(age);

Output:

20

Note: A local variable must be assigned a value before you use it. Java does not automatically initialize local variables.

What Are Data Types in Java?

A data type specifies what kind of value a variable can hold and which operations are appropriate for that value.

For example, a person's age can be represented using int, while a person's name can be stored using String.

Java data types are commonly divided into two categories:

  1. Primitive data types

  2. Reference data types

1. Primitive Data Types in Java

Java has eight primitive data types. These are built into the language.

Data Type Purpose Example
byte Small integer byte age = 20;
short Short integer short year = 2026;
int Whole numbers int marks = 85;
long Large whole numbers long population = 8000000L;
float Decimal numbers float price = 25.5f;
double Decimal numbers with greater precision than float double salary = 25000.75;
char One UTF-16 code unit char grade = 'A';
boolean True or false boolean passed = true;

Let's understand each data type.

1. byte

The byte data type stores small signed integers from -128 to 127.

byte age = 25;
System.out.println(age);

Output:

25

2. short

The short data type stores signed integers from -32,768 to 32,767.

short year = 2026;
System.out.println(year);

Output:

2026

3. int

The int data type is commonly used to store whole numbers.

int marks = 85;
System.out.println(marks);

Output:

85

4. long

The long data type stores 64-bit signed integers. An L suffix is commonly used to indicate a long integer literal.

long population = 8000000L;
System.out.println(population);

Output:

8000000

5. float

The float data type stores 32-bit floating-point numbers. Use the f suffix when writing a decimal literal intended as a float.

float price = 25.5f;
System.out.println(price);

Output:

25.5

6. double

The double data type stores 64-bit floating-point numbers and is a common choice for decimal calculations.

double salary = 25000.75;
System.out.println(salary);

Output:

25000.75

7. char

The char data type stores one UTF-16 code unit. A character literal uses single quotation marks.

char grade = 'A';
System.out.println(grade);

Output:

A

8. boolean

The boolean data type represents a logical value: true or false.

boolean isPassed = true;
System.out.println(isPassed);

Output:

true

2. Reference Data Types in Java

Reference types represent references to objects rather than directly storing primitive values. Examples include classes, arrays and interfaces.

The String class is frequently used to store text.

String Example

String name = "Rahul";
System.out.println(name);

Output:

Rahul

Notice that String starts with a capital S and uses double quotation marks for text literals.

Array Example

An array can hold multiple values of the same element type.

int[] marks = {80, 85, 90};

System.out.println(marks[0]);

Output:

80

Java arrays use zero-based indexing, so marks[0] accesses the first element.

Difference Between Primitive and Reference Data Types

Primitive Types Reference Types
Represent primitive values Represent references to objects or arrays
Include int, char and boolean Include String, arrays and class types
Use predefined primitive keywords Include class, interface and array types
Example: int age = 20; Example: String name = "Rahul";

Rules for Naming Variables in Java

Java variable names must follow certain rules.

  1. A variable name can contain letters, digits, underscores and dollar signs.

  2. A name cannot begin with a digit.

  3. Spaces are not allowed in variable names.

  4. Java keywords, such as class and int, cannot be used as ordinary variable names.

  5. Variable names are case-sensitive. For example, age and Age are different names.

  6. Use meaningful names such as studentName instead of unclear names such as x.

  7. By convention, use camelCase for ordinary variable names.

Valid variable names:

int age = 20;
String studentName = "Rahul";
double totalMarks = 450.5;

Invalid variable names:

// int 2age = 20;       // Cannot start with a digit
// int student age = 20; // Spaces are not allowed
// int class = 10;       // "class" is a keyword

The invalid examples are commented out so that the program can still compile.

Complete Java Program Using Variables and Data Types

The following program demonstrates how different variables can be used together.

public class Main {
    public static void main(String[] args) {
        String studentName = "Rahul";
        int age = 20;
        char grade = 'A';
        double percentage = 85.5;
        boolean passed = true;

        System.out.println("Student Name: " + studentName);
        System.out.println("Age: " + age);
        System.out.println("Grade: " + grade);
        System.out.println("Percentage: " + percentage);
        System.out.println("Passed: " + passed);
    }
}

Output:

Student Name: Rahul
Age: 20
Grade: A
Percentage: 85.5
Passed: true

This example shows how Java uses different data types to represent different kinds of information.

Common Mistakes Beginners Should Avoid

1. Assigning the wrong type of value

// int age = "twenty";

This is invalid because "twenty" is a String, not an integer.

2. Forgetting quotation marks around text

String name = "Rahul";

Text literals need double quotation marks.

3. Using single quotation marks for a String

// String name = 'Rahul';

Single quotation marks are for char literals, not multi-character String literals.

4. Using a local variable before initialization

// int age;
// System.out.println(age);

This produces a compile-time error because the local variable has not been assigned a value.

5. Forgetting the f suffix for a float literal

float price = 25.5f;

Without the f suffix, a decimal literal such as 25.5 is a double by default and cannot be assigned directly to a float.

Frequently Asked Questions (FAQs)

1. What is a variable in Java?

A variable is a named storage location that holds a value used by a program.

2. How many primitive data types are there in Java?

Java has eight primitive data types: byte, short, int, long, float, double, char and boolean.

3. Is String a primitive data type in Java?

No. String is a class and therefore a reference type.

4. What is the difference between int and double?

int stores whole numbers, while double stores floating-point numbers, including decimal values.

5. What is the difference between float and double?

float uses 32-bit floating-point precision, while double uses 64-bit floating-point precision. A double is generally preferred for ordinary decimal calculations.

6. Can a Java variable's value change?

Yes, if it is not declared as a final variable and the new assignment is compatible with its type.

int age = 20;
age = 21;

System.out.println(age);

Output: 21

7. What happens if a variable is used before initialization?

Using an uninitialized local variable causes a compile-time error.

Practice Exercises

Try these beginner exercises:

  1. Create an integer variable named age and print its value.

  2. Create a String variable containing your name.

  3. Create two double variables and print their sum.

  4. Create a char variable containing your grade.

  5. Create a boolean variable named isStudent and print its value.

Challenge: Write a Java program that stores a student's name, roll number, marks and pass status, then displays all four values.

Conclusion

Variables and data types are fundamental building blocks of Java programming. Variables store information, while data types determine the kind of values they can represent.

Start by practising int, double, char, boolean and String. Once these concepts are clear, learning operators, conditions and loops will become easier.