📘LESSON 2
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define variables in Java.
- Declare and initialize variables correctly.
- Explain the purpose of variables in programming.
- Differentiate between variable declaration and initialization.
- Use variables to store and display data in Java applications.
3.2 Overview
Variables are fundamental components in programming and are used to store data temporarily while a program executes. In Java, variables allow developers to store values such as numbers, text, and logical data that can be used throughout an application.
This lesson introduces learners to Java variables, including how they are declared, initialized, and used in Java programs. Learners will also explore how variables support calculations, user interaction, and data processing within software applications.
Understanding variables is essential because nearly every Java application relies on variables to manage and manipulate data.
KT0201 — Introduction to Variables
A variable is a named memory location used to store data in a Java application.
Variables allow programs to:
- store information,
- retrieve information,
- update values,
- and process data during execution.
Each variable has:
- a data type,
- a variable name,
- and a value.
Structure of a Variable
int age = 25;
Explanation
| Component | Description |
|---|---|
| int | Data type |
| age | Variable name |
| 25 | Stored value |
Importance of Variables
Variables are important because they:
- store application data,
- support calculations,
- improve program flexibility,
- and allow user interaction.
Variables are used in:
- banking systems,
- inventory systems,
- websites,
- mobile applications,
- and enterprise software.
KT0202 — Declaring Variables
Variable declaration means creating a variable and specifying its data type.
Syntax for Variable Declaration
dataType variableName;
Example
int marks;
In this example:
- int is the data type,
- marks is the variable name.
The variable exists, but it does not yet contain a value.
Rules for Variable Names
Variable names:
- must start with a letter, underscore
_, or dollar sign$, - cannot contain spaces,
- cannot start with numbers,
- and should use camelCase naming convention.
Examples:
- studentName
- totalMarks
- employeeSalary
Invalid examples:
- 1marks
- student name
- @value
KT0203 — Initializing Variables
Variable initialization means assigning a value to a variable.
Example of Variable Initialization
int marks = 80;
In this example:
- the variable is declared,
- and assigned a value at the same time.
Separate Declaration and Initialization
int age;
age = 21;
Importance of Initialization
Initialization ensures that variables contain usable values before they are processed.
Uninitialized variables may cause:
- program errors,
- unexpected behaviour,
- or compilation problems.
KT0204 — Using Variables in Java Programs
Variables can be used:
- in calculations,
- in conditions,
- in loops,
- and in output statements.
Example
public class Main {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
int total = num1 + num2;
System.out.println(total);
}
}
Output
30
Using Variables with Text
public class Main {
public static void main(String[] args) {
String name = "Bukhosi";
System.out.println("Welcome " + name);
}
}
Output
Welcome Bukhosi
KT0205 — Variable Scope
Variable scope refers to where a variable can be accessed in a program.
Variables may exist:
- inside methods,
- inside classes,
- or inside blocks of code.
Local Variable Example
public class Main {
public static void main(String[] args) {
int marks = 75;
System.out.println(marks);
}
}
The variable marks only exists inside the main() method.
Global/Class Variable Example
public class Student {
int age = 20;
}
The variable age belongs to the class and can be accessed by class objects.
KT0206 — Constants in Java
A constant is a variable whose value cannot change after assignment.
Constants use the final keyword.
Example
final double PI = 3.14;
The value of PI cannot be modified later in the program.
Importance of Constants
Constants improve:
- code safety,
- readability,
- and maintainability.
Constants are commonly used for:
- mathematical values,
- tax rates,
- configuration settings,
- and fixed application values.
3.5 Key Notes / Summary
- Variables are used to store data in Java applications.
- Every variable has a data type, name, and value.
- Variables must be declared before use.
- Initialization assigns values to variables.
- Variables support calculations and application logic.
- Scope determines where variables can be accessed.
- Constants use the final keyword and cannot change.