Core Java Programming: From Beginner to Project Development

Lesson 2.1: Variables in Java

Introduction

In the previous module, you learned how to write and run Java programs. However, those programs only displayed fixed text on the screen.

Real-world applications need to store, update, and process data. For example, a student management system stores a student’s name, age, marks, and roll number. A banking application stores account balances and transaction details.

To store such information, Java uses variables.

In this lesson, you will learn what variables are, why they are important, how to declare and initialize them, and the rules for naming variables.


Learning Objectives

After completing this lesson, you will be able to:

  • Understand what a variable is.

  • Declare and initialize variables.

  • Modify variable values.

  • Follow Java variable naming rules.

  • Write programs using variables.


What is a Variable?

A variable is a named memory location used to store data.

The value stored in a variable can change during program execution, which is why it is called a variable.

Real-Life Analogy

Think of a variable as a labeled storage box.

  • The label is the variable name.

  • The contents are the value stored inside.

You can replace the contents whenever needed while keeping the same label.

Example:

Box Label → age
Value     → 20

Later:

Box Label → age
Value     → 21

The value changed, but the variable name remained the same.


Why Do We Use Variables?

Variables help us:

  • Store information.

  • Perform calculations.

  • Reuse data.

  • Update values.

  • Build interactive applications.

Without variables, every value would have to be written manually.


Declaring a Variable

The general syntax is:

dataType variableName;

Example:

int age;

Here:

  • int is the data type.

  • age is the variable name.


Initializing a Variable

Initialization means assigning a value to a variable.

Syntax:

dataType variableName = value;

Example:

int age = 20;

Declaring Multiple Variables

int a = 10; int b = 20; int c = 30;

Or

int a = 10, b = 20, c = 30;

Printing Variables

public class Student { public static void main(String[] args) { String name = "Rahul"; int age = 20; System.out.println(name); System.out.println(age); } }

Output

Rahul
20

Updating Variables

The value of a variable can be changed.

int marks = 75; marks = 90; System.out.println(marks);

Output

90

Rules for Naming Variables

A variable name:

  • Must begin with a letter, _, or $.

  • Cannot start with a number.

  • Cannot contain spaces.

  • Cannot use Java keywords.

  • Is case-sensitive.


Valid Variable Names

studentName
age
rollNumber
salary
_marks
$total

Invalid Variable Names

2student
student name
class
total-marks

These will produce compilation errors.


Java Naming Convention

Use camelCase for variable names.

Examples:

studentName
firstName
totalMarks
phoneNumber

Avoid names like:

a
x
abc

unless used in short loops or temporary calculations.


Example Program

public class StudentInfo { public static void main(String[] args) { String name = "Shiv"; int age = 21; String department = "Computer Science"; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Department: " + department); } }

Output

Name: Shiv
Age: 21
Department: Computer Science

Common Mistakes

Using an Undeclared Variable

age = 20;

Wrong because age has not been declared.

Correct:

int age = 20;

Using a Keyword as a Variable

int class = 10;

This is invalid because class is a Java keyword.


Variable Name Starting with a Number

int 2marks = 50;

This is not allowed.


Best Practices

  • Use meaningful names.

  • Follow camelCase naming.

  • Initialize variables before using them.

  • Avoid unnecessary variables.

  • Keep variable names short but descriptive.


Lesson Summary

In this lesson, you learned:

  • What variables are.

  • Why variables are important.

  • How to declare and initialize variables.

  • Rules for naming variables.

  • How to update and print variable values.

Variables are one of the most important concepts in programming because almost every Java program uses them.


Practice Questions

  1. What is a variable?

  2. Why do we use variables?

  3. What is variable initialization?

  4. Write the syntax for declaring a variable.

  5. Can a variable name start with a number?

  6. What naming convention is used for Java variables?


Programming Exercise

Write Java programs to:

  1. Store your name and print it.

  2. Store your age and print it.

  3. Store your college name.

  4. Store your department.

  5. Update a variable and print the new value.


Assignment

Create a program named StudentProfile.java that stores and displays:

  • Student Name

  • Roll Number

  • Age

  • Department

  • Semester

  • College Name

Use meaningful variable names and proper formatting.

Next Lesson: Lesson 2.2 – Data Types in Java

Scroll to Top