Operators in Java

Ajink GuptaAjink Gupta
6 min read

Operators in Java are essential building blocks of programming. They allow you to perform calculations, comparisons, and manipulate data. In this guide, we’ll cover all types of operators in Java with examples to make it crystal clear.


1. Types of Operators in Java: An Overview

Java offers a wide variety of operators to handle different types of operations. Broadly, they can be categorized as:

  1. Arithmetic Operators: For mathematical calculations.

  2. Assignment Operators: For assigning values to variables.

  3. Relational and Logical Operators: For comparisons and decision-making.

  4. Bitwise Operators: For operations at the binary level.

  5. Increment, Decrement, and Ternary Operators: For shorthand operations.

Let’s explore each type in detail.


2. Arithmetic Operators: Performing Calculations

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

OperatorDescriptionExampleResult
+Additiona + bSum of a and b
-Subtractiona - bDifference of a and b
*Multiplicationa * bProduct of a and b
/Divisiona / bQuotient of a and b
%Modulus (Remainder)a % bRemainder when a is divided by b

Example in Java

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

3. Assignment Operators: Simplify Your Code

Assignment operators are used to assign values to variables. The basic assignment operator is =. Java also provides compound assignment operators:

OperatorDescriptionExampleEquivalent To
=Assign valuea = b-
+=Add and assigna += ba = a + b
-=Subtract and assigna -= ba = a - b
*=Multiply and assigna *= ba = a * b
/=Divide and assigna /= ba = a / b
%=Modulus and assigna %= ba = a % b

Example in Java

public class AssignmentExample {
    public static void main(String[] args) {
        int a = 5;
        a += 3; // a = a + 3
        System.out.println("After += : " + a); // Output: 8

        a *= 2; // a = a * 2
        System.out.println("After *= : " + a); // Output: 16

        a %= 5; // a = a % 5
        System.out.println("After %= : " + a); // Output: 1
    }
}

4. Relational and Logical Operators in Java

Relational Operators

These are used to compare two values. The result is a boolean (true or false).

OperatorDescriptionExampleResult
==Equal toa == btrue if a equals b
!=Not equal toa != btrue if a is not equal to b
>Greater thana > btrue if a is greater than b
<Less thana < btrue if a is less than b
>=Greater than or equal toa >= btrue if a is greater than or equal to b
<=Less than or equal toa <= btrue if a is less than or equal to b

Logical Operators

These combine multiple conditions:

OperatorDescriptionExampleResult
&&Logical ANDa > 5 && b < 3true if both conditions are true
``Logical OR
!Logical NOT!(a > 5)true if the condition is false

Example in Java

public class RelationalLogicalExample {
    public static void main(String[] args) {
        int a = 10, b = 5;
        System.out.println("a > b: " + (a > b)); // Output: true
        System.out.println("a < b: " + (a < b)); // Output: false
        System.out.println("a > 5 && b < 10: " + (a > 5 && b < 10)); // Output: true
        System.out.println("a < 5 || b < 10: " + (a < 5 || b < 10)); // Output: true
    }
}

5. Bitwise Operators: Working with Binary

Bitwise operators operate directly on binary digits. These are useful for low-level programming.

OperatorDescriptionExampleResult
&ANDa & bBitwise AND
``OR`a
^XORa ^ bBitwise XOR
~Complement~aBitwise complement
<<Left shifta << 2Shift bits left by 2
>>Right shifta >> 2Shift bits right by 2

Example in Java

public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3; // a = 0101, b = 0011
        System.out.println("a & b: " + (a & b)); // Output: 1 (0001)
        System.out.println("a | b: " + (a | b)); // Output: 7 (0111)
        System.out.println("a ^ b: " + (a ^ b)); // Output: 6 (0110)
        System.out.println("~a: " + (~a));       // Output: -6
    }
}

6. Understanding Increment, Decrement, and Ternary Operators

Increment and Decrement Operators

These operators increase or decrease a value by 1:

OperatorDescriptionExampleResult
++Increment++aIncreases a by 1
--Decrement--aDecreases a by 1

Example in Java

public class IncrementDecrementExample {
    public static void main(String[] args) {
        int a = 5;
        System.out.println("a++: " + (a++)); // Output: 5 (then a becomes 6)
        System.out.println("++a: " + (++a)); // Output: 7
    }
}

Ternary Operator

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

condition ? value_if_true : value_if_false

Example in Java

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

Conclusion

Operators in Java provide the tools to perform calculations, make decisions, and manipulate data. Understanding these operators and their behavior is key to writing efficient code. Whether you're doing arithmetic, comparing values, or working with binary, these examples should have you covered. Keep practicing, and you'll be a pro in no time!

0
Subscribe to my newsletter

Read articles from Ajink Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ajink Gupta
Ajink Gupta

I am an engineering student pursuing a degree in Artificial Intelligence and Data Science at Datta Meghe College of Engineering. I have strong technical skills in Full Stack Web Development, as well as programming in Python and Java. I currently manage Doubtly's blog and am exploring job opportunities as an SDE. I am passionate about learning new technologies and contributing to the tech community.