Curriculum
Learning Variables, Data Types and Operators is one of the most important steps in Java programming. Every Java application, whether it is a simple calculator or a large enterprise backend system, uses variables, data types, and operators extensively. These concepts form the foundation of programming and help developers store, manipulate, and process data efficiently.
Before building APIs, databases, microservices, or enterprise applications, every Java developer must understand how data is stored and how operations are performed on that data. In this lesson, you will learn what variables are, the different data types available in Java, and how operators help perform calculations and logical operations.
A variable is a named memory location used to store data.
Think of a variable as a container that holds information which can be used and modified throughout a program.
For example:
All of these values can be stored in variables.
Variables help developers:
Without variables, programs would not be able to handle dynamic information.
The basic syntax for declaring a variable is:
dataType variableName = value;
Example:
int age = 25;
In this example:
int is the data typeage is the variable name25 is the value storedAnother example:
String name = "Rahul";
Here:
String is the data typename is the variable"Rahul" is the valueJava follows specific naming rules.
int age;
String studentName;
double salary;
int 123age;
String student-name;
double class;
Use meaningful names:
int employeeAge;
double accountBalance;
String customerName;
Avoid:
int a;
double x;
String temp;
Meaningful names improve code readability.
Data types define the type of data a variable can store.
Different types of information require different data types.
Examples:
Java uses data types to allocate memory efficiently and perform appropriate operations.
Java data types are divided into two categories:
Built-in data types provided by Java.
Created by developers or provided through Java libraries.
Examples:
Let’s first understand primitive data types.
Java provides eight primitive data types.
Used for small integer values.
byte marks = 90;
Size:
1 byte
Range:
-128 to 127
Used for slightly larger integers.
short population = 25000;
Size:
2 bytes
Most commonly used integer type.
int age = 25;
Size:
4 bytes
Used for very large numbers.
long mobileNumber = 9876543210L;
Size:
8 bytes
Stores decimal values.
float percentage = 89.5f;
Size:
4 bytes
More precise decimal type.
double salary = 55000.75;
Size:
8 bytes
Stores a single character.
char grade = 'A';
Size:
2 bytes
Stores true or false values.
boolean isLoggedIn = true;
Size:
1 bit
Non-primitive data types can store more complex information.
Stores text data.
String city = "Jaipur";
Stores multiple values.
int[] marks = {80, 85, 90};
Blueprint for creating objects.
class Student {
}
These concepts will be covered in greater detail later in the course.
Variables can be assigned values during declaration.
Example:
int age = 22;
Or later:
int age;
age = 22;
Both approaches are valid.
Java allows multiple variables of the same type.
Example:
int x = 10, y = 20, z = 30;
Output:
System.out.println(x);
System.out.println(y);
System.out.println(z);
This reduces repetitive code.
Sometimes values should never change.
Example:
Java uses the final keyword.
Example:
final double PI = 3.14159;
Now PI cannot be modified.
Operators are special symbols used to perform operations on variables and values.
Example:
int result = 10 + 5;
The + symbol is an operator.
Operators are essential because programs perform calculations, comparisons, and logical decisions.
Java provides several categories of operators.
Used for mathematical calculations.
| Operator | Meaning |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example:
int a = 20;
int b = 10;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
Output:
30
10
200
2
0
Used to assign values.
Example:
int age = 25;
Common assignment operators:
| Operator | Example |
|---|---|
| = | x = 10 |
| += | x += 5 |
| -= | x -= 5 |
| *= | x *= 5 |
| /= | x /= 5 |
Example:
int salary = 5000;
salary += 1000;
Result:
6000
Used for comparisons.
| Operator | Meaning |
|---|---|
| == | Equal To |
| != | Not Equal To |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than Equal To |
| <= | Less Than Equal To |
Example:
int age = 20;
System.out.println(age > 18);
Output:
true
Used to combine conditions.
| Operator | Meaning |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |
Example:
int age = 25;
boolean citizen = true;
System.out.println(age >= 18 && citizen);
Output:
true
Logical operators are frequently used in authentication and authorization systems.
Increases value by 1.
Example:
int count = 5;
count++;
Result:
6
Decreases value by 1.
Example:
int count = 5;
count--;
Result:
4
These operators are commonly used in loops.
Java follows precedence rules.
Example:
int result = 10 + 5 * 2;
Output:
20
Multiplication executes before addition.
Use parentheses when needed.
Example:
int result = (10 + 5) * 2;
Output:
30
Consider a simple e-commerce application.
double price = 1000;
double discount = 100;
double finalPrice = price - discount;
System.out.println(finalPrice);
Output:
900
Variables store values, data types define those values, and operators perform calculations.
This demonstrates how these concepts work together in real-world applications.
Example:
int salary = 55000.75;
This causes an error.
Correct:
double salary = 55000.75;
Example:
int age;
System.out.println(age);
This generates an error.
Always initialize variables before use.
Avoid:
int x;
int y;
Prefer:
int employeeAge;
int studentMarks;
These habits improve code quality and maintainability.
Understanding Variables, Data Types and Operators is essential for every Java developer. Variables allow programs to store information, data types define the kind of information being stored, and operators perform calculations and logical operations.
These concepts form the building blocks of Java programming and are used in every application, from simple programs to large-scale enterprise systems. Mastering them will make it easier to understand future topics such as conditional statements, loops, methods, object-oriented programming, and backend development.
A variable is a named memory location used to store data that can be accessed and modified during program execution.
Data types define the type of value a variable can store, such as integers, decimals, characters, or boolean values.
The int data type is one of the most commonly used types for storing whole numbers.
Operators are symbols that perform operations such as calculations, comparisons, and logical evaluations.
Variables allow programs to store, process, and manipulate information efficiently.
Want to explore additional programming and software development topics? Click here for more free courses
WhatsApp us