In Java, operators and expressions are fundamental building blocks for creating programs. Operators are symbols that perform specific operations on values and variables whereas Expressions are combinations of values, variables, and operators that can be evaluated to a single value. By combining various operators and expressions, you can create statements that perform calculations, make decisions, and control the flow of your program.
Table of Contents
What are Operators in Java?
Operators are special symbols in Java that perform specific operations on values (operands) and produce a result. Some of the common operators in Java include arithmetic operators, relational operators, and logical operators.
Arithmetic Operators
In Java, arithmetic operators are used for basic mathematical operations like addition, subtraction, multiplication, and division.
public class ArithmeticOperatorsExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 5;
// Addition operator
System.out.println("Addition: " + (num1 + num2));
// Subtraction operator
System.out.println("Subtraction: " + (num1 - num2));
// Multiplication operator
System.out.println("Multiplication: " + (num1 * num2));
// Division operator
System.out.println("Division: " + (num1 / num2));
// Modulus operator
System.out.println("Modulus: " + (num1 % num2));
}
}
Output

Relational Operators
Relational operators are used to compare values and determine the relationship between them. The result of a relational operator is either true or false.
public class RelationalOperatorsExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
// Less than operator
System.out.println("Less than: " + (num1 < num2));
// Less than or equal to operator
System.out.println("Less than or equal to: " + (num1 <= num2));
// Greater than operator
System.out.println("Greater than: " + (num1 > num2));
// Greater than or equal to operator
System.out.println("Greater than or equal to: " + (num1 >= num2));
// Equal to operator
System.out.println("Equal to: " + (num1 == num2));
// Not equal to operator
System.out.println("Not equal to: " + (num1 != num2));
}
}
Output

Logical Operators
Logical operators are used to combine conditions and determine if a certain condition is met. Some of the common logical operators in Java include AND, OR, and NOT.
public class LogicalOperatorsExample {
public static void main(String[] args) {
boolean condition1 = true;
boolean condition2 = false;
// Logical AND operator
System.out.println("Logical AND: " + (condition1 && condition2));
// Logical OR operator
System.out.println("Logical OR: " + (condition1 || condition2));
// Logical NOT operator
System.out.println("Logical NOT (condition1): " + !condition1);
System.out.println("Logical NOT (condition2): " + !condition2);
}
}
Output

What are Expressions in Java?
Expressions are sequences of operators and operands that are used to perform calculations in Java. The result of an expression is a value, which can be used to assign values to variables, evaluate conditions, and perform various other operations.
public class ExpressionsExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
// Arithmetic expression
int result = num1 + num2;
System.out.println("Arithmetic expression: " + result);
// Relational expression
boolean condition = num1 < num2;
System.out.println("Relational expression: " + condition);
// Logical expression
condition = (num1 > 0) && (num2 < 30);
System.out.println("Logical expression: " + condition);
}
}
Output

Conclusion
Operators and expressions provide a way to perform various calculations and operations in your code. Whether you’re adding numbers, comparing values, or evaluating conditions, operators and expressions are essential tools that you’ll use on a regular basis when writing Java programs.