Introduction to Java | Programming Basics

Operators in Java

In Java, operators are special symbols or keywords used to perform operations on variables and values. Java has a rich set of operators that can be classified into various categories based on their functionality.

Types of Operators in Java

Java operators are divided into several categories:

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. These include:

Example: Arithmetic Operators

Code Example


public class ArithmeticExample {
public static void main(String[] args) {
    int a = 10, b = 5;
    System.out.println("a + b = " + (a + b));  // Addition
    System.out.println("a - b = " + (a - b));  // Subtraction
    System.out.println("a * b = " + (a * b));  // Multiplication
    System.out.println("a / b = " + (a / b));  // Division
    System.out.println("a % b = " + (a % b));  // Modulus
                    }
                }
                    

Output

a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0

2. Relational (Comparison) Operators

Relational operators are used to compare two values. They return a boolean value (true or false) depending on the comparison.

Example: Relational Operators

Code Example


public class RelationalExample {
public static void main(String[] args) {
    int x = 10, y = 5;
    System.out.println("x == y: " + (x == y));  // Equal to
    System.out.println("x != y: " + (x != y));  // Not equal to
    System.out.println("x > y: " + (x > y));    // Greater than
    System.out.println("x < y: " + (x < y));    // Less than
    System.out.println("x >= y: " + (x >= y));  // Greater than or equal to
    System.out.println("x <= y: " + (x <= y));  // Less than or equal to
                    }
                }
                    

Output

x == y: false
x != y: true
x > y: true
x < y: false
x >= y: true
x <= y: false

3. Logical Operators

Logical operators are used to combine multiple boolean expressions and return a boolean value. The commonly used logical operators are:

Example: Logical Operators

Code Example


public class LogicalExample {
public static void main(String[] args) {
    boolean a = true, b = false;
    System.out.println("a && b: " + (a && b));  // Logical AND
    System.out.println("a || b: " + (a || b));  // Logical OR
    System.out.println("!a: " + !a);             // Logical NOT
                    }
                }
                    

Output

a && b: false
a || b: true
!a: false

4. Assignment Operators

Assignment operators are used to assign values to variables. Commonly used assignment operators include:

Example: Assignment Operators

Code Example


public class AssignmentExample {
public static void main(String[] args) {
    int num = 10;
    num += 5;  // Add and assign
    System.out.println("num += 5: " + num); // num = 15
    num -= 3;  // Subtract and assign
    System.out.println("num -= 3: " + num); // num = 12
    num *= 2;  // Multiply and assign
    System.out.println("num *= 2: " + num); // num = 24
    num /= 4;  // Divide and assign
    System.out.println("num /= 4: " + num); // num = 6
    num %= 3;  // Modulus and assign
    System.out.println("num %= 3: " + num); // num = 0
                    }
                }
                    

Output

num += 5: 15
num -= 3: 12
num *= 2: 24
num /= 4: 6
num %= 3: 0

5. Unary Operators

Unary operators operate on a single operand to perform various operations, such as increment, decrement, and negation.

Example: Unary Operators

Code Example


public class UnaryExample {
public static void main(String[] args) {
    int num = 5;
    System.out.println("++num: " + ++num);  // Pre-increment
    System.out.println("num++: " + num++);  // Post-increment
    System.out.println("--num: " + --num);  // Pre-decrement
    System.out.println("num--: " + num--);  // Post-decrement
    System.out.println("!true: " + !true);  // Logical NOT
                    }
                }
                    

Output

++num: 6
num++: 6
--num: 5
num--: 5
!true: false

6. Conditional (Ternary) Operator

The ternary operator is a shorthand for an if-else statement. It is written as:

condition ? expression1 : expression2;

Example: Conditional Operator

Code Example


public class TernaryExample {
public static void main(String[] args) {
    int x = 10, y = 5;
    String result = (x > y) ? "x is greater" : "y is greater";
    System.out.println(result);
                    }
                }
                    

Output

x is greater