Java Programming Handbook

    Operators in Java

    Operators in Java are special symbols that perform operations on variables and values. They help in performing calculations, making decisions, and manipulating data efficiently. Understanding Java operators is essential for writing logical and optimized code.

    Types of Operators in Java#

    Java provides different types of operators, categorized as follows:

    1. Arithmetic Operators#

    Arithmetic operators are used for performing mathematical calculations such as addition, subtraction, multiplication, and division.

    class Main { public static void main(String[] args) { // Declare variables int num1 = 15, num2 = 4; // Performing arithmetic operations System.out.println("num1 + num2 = " + (num1 + num2)); // Output: 19 System.out.println("num1 - num2 = " + (num1 - num2)); // Output: 11 System.out.println("num1 * num2 = " + (num1 * num2)); // Output: 60 System.out.println("num1 / num2 = " + (num1 / num2)); // Output: 3 System.out.println("num1 % num2 = " + (num1 % num2)); // Output: 3 } }

    2. Relational (Comparison) Operators#

    Relational operators compare two values and return a boolean result (true or false).

    class Main { public static void main(String[] args) { // Declare variables int a = 10, b = 20; // Performing relational operations System.out.println("a == b: " + (a == b)); // Output: false System.out.println("a != b: " + (a != b)); // Output: true System.out.println("a > b: " + (a > b)); // Output: false System.out.println("a < b: " + (a < b)); // Output: true System.out.println("a >= b: " + (a >= b)); // Output: false System.out.println("a <= b: " + (a <= b)); // Output: true } }

    3. Logical Operators#

    Logical operators are used to combine multiple conditions.

    class Main { public static void main(String[] args) { // Declare boolean variables boolean cond1 = true, cond2 = false; // Performing logical operations System.out.println("cond1 && cond2: " + (cond1 && cond2)); // Output: false System.out.println("cond1 || cond2: " + (cond1 || cond2)); // Output: true System.out.println("!cond1: " + (!cond1)); // Output: false } }

    4. Bitwise Operators#

    Bitwise operators perform operations on binary representations of numbers.

    class Main { public static void main(String[] args) { // Declare variables int x = 5, y = 3; // Performing bitwise operations System.out.println("x & y: " + (x & y)); // Output: 1 System.out.println("x | y: " + (x | y)); // Output: 7 System.out.println("x ^ y: " + (x ^ y)); // Output: 6 System.out.println("~x: " + (~x)); // Output: -6 System.out.println("x << 1: " + (x << 1)); // Output: 10 System.out.println("x >> 1: " + (x >> 1)); // Output: 2 } }

    5. Assignment Operators#

    Assignment operators assign values to variables.

    class Main { public static void main(String[] args) { // Declare a variable int num = 10; // Performing assignment operations num += 5; // Equivalent to num = num + 5 System.out.println("num += 5: " + num); // Output: 15 num -= 2; System.out.println("num -= 2: " + num); // Output: 13 num *= 3; System.out.println("num *= 3: " + num); // Output: 39 num /= 2; System.out.println("num /= 2: " + num); // Output: 19 num %= 4; System.out.println("num %= 4: " + num); // Output: 3 } }

    6. Unary Operators#

    Unary operators work on a single operand.

    class Main { public static void main(String[] args) { // Declare a variable int a = 5; // Performing unary operations System.out.println("+a: " + (+a)); // Output: 5 System.out.println("-a: " + (-a)); // Output: -5 System.out.println("a++: " + (a++)); // Output: 5 (post-increment) System.out.println("After a++: " + a); // Output: 6 System.out.println("a--: " + (a--)); // Output: 6 (post-decrement) System.out.println("After a--: " + a); // Output: 5 } }

    7. Ternary Operator#

    The ternary operator is a shorthand for if-else statements.

    class Main { public static void main(String[] args) { // Declare a variable int number = 10; // Using ternary operator String result = (number % 2 == 0) ? "Even" : "Odd"; System.out.println("Number is: " + result); // Output: Even } }

    8. Instanceof Operator#

    The instanceof operator checks whether an object belongs to a particular class or subclass.

    class Main { public static void main(String[] args) { // Declare a string object String str = "Hello"; // Checking if str is an instance of String System.out.println("str instanceof String: " + (str instanceof String)); // Output: true } }

    Conclusion#

    Operators are an essential part of Java programming, allowing us to perform calculations, comparisons, logical operations, and more.

    In this blog, we covered different types of operators in Java, including arithmetic, relational, logical, bitwise, assignment, unary, ternary, and instanceof operators, along with practical examples with expected outputs.

    Last updated on Apr 09, 2025