
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Variables allow us to store data, but storing data alone is not very useful. In real-world programs, we often need to perform calculations, compare values, and make decisions based on data. For example, an e-commerce application may need to calculate a product's total price, while a banking application may need to check whether an account has sufficient balance.
To perform such operations, Java provides operators. Operators are special symbols that perform actions on values and variables. The result produced by combining values, variables, and operators is known as an expression.
In this chapter, you will learn how to perform calculations, compare values, update variables, and combine conditions using Java operators.
An operator is a symbol that tells Java to perform a specific operation.
Example:
int sum = 10 + 20;
Here, + is an operator that adds two numbers.
The result is:
30
An expression is a combination of values, variables, and operators that produces a result.
Example:
10 + 20
This expression evaluates to:
30
Another example:
age + 5
If age is 18, the expression evaluates to:
23
Arithmetic operators are used for mathematical calculations.
Example:
int a = 10; int b = 3; 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:
13 7 30 3 1
The assignment operator is used to assign a value to a variable.
int age = 18;
Here, the = operator assigns the value 18 to the variable age.
You can also update a variable:
age = 20;
Now the value of age becomes 20.
Comparison operators compare two values and return either true or false.
Example:
int age = 18; System.out.println(age >= 18);
Output:
true
Logical operators are used to combine multiple conditions.
Example:
boolean hasTicket = true; boolean hasId = true; System.out.println(hasTicket && hasId);
Output:
true
The result is true because both conditions are true.
These operators increase or decrease a variable's value by one.
Increment:
int count = 5; count++;
Value of count becomes:
6
Decrement:
int count = 5; count--;
Value of count becomes:
4
public class Main {
public static void main(String[] args) {
int price = 500;
int quantity = 2;
int total = price * quantity;
System.out.println(total);
}
}
Output:
1000
In this program:
price stores the price of one product.quantity stores the number of products.* multiplies the two values.total.System.out.println() displays the result.The modulus operator (%) returns the remainder after division.
Example:
System.out.println(10 % 3);
Output:
1
Because:
10 ÷ 3 = 3 remainder 1
This operator is commonly used to check whether a number is even or odd.
Example:
System.out.println(8 % 2);
Output:
0
Since the remainder is 0, the number is even.
Wrong:
if(age = 18)
Correct:
if(age == 18)
= assigns a value.
== compares values.
System.out.println(10 / 3);
Output:
3
Both values are integers, so Java returns an integer result.
To get decimals:
System.out.println(10.0 / 3);
Output:
3.3333333333333335