Skip to main content

Command Palette

Search for a command to run...

Operators In Java

Published
5 min read
Operators In Java
E

I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.

Introduction

In our previous discussion, we explored Java's ecosystem, set up our development environment, and executed our first program. Building on our understanding of data types and variables, we now explore operators, which are essential for performing computations and logical operations in Java. Operators allow us to perform various operations on variables, from basic arithmetic to complex logical evaluations.

Operators

Operators are symbols used to perform mathematical or logical operations on variables and values. The main classes of operators in Java are Arithmetic, Relational, Logical and Bitwise operators

Arithmetic Operators

These are used to perform mathematical operations. Java provides the following arithmetic operators:

OperatorNameExamples
+Addition (Unary plus)a + b
-Subtraction (Unary minus)a - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
++Incrementa++ or ++a
--Decrementa-- or --a

Note: Integer division truncates the remainder. To obtain the remainder, use the modulus operator (%).

Example:

public class ArithmeticOperators {
    public static void main(String[] args) {
        int a = 10, b = 3;

        System.out.println("Sum: " + (a + b));
        System.out.println("Difference: " + (a - b));
        System.out.println("Product: " + (a * b));
        System.out.println("Quotient: " + (a / b));
        System.out.println("Remainder: " + (a % b));
    }
}

/**
* Output:
* Sum: 13
* Difference: 7
* Product: 30
* Quotient: 3
* Remainder: 1
*/

Relational Operators

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

OperatorNameExample
\==Equal toa == b
!=Not equala != b
\>Greater thana > b
<Less thana < b
<=Greater than or equal toa >= b
<=Less than or equal tob <= b

Example:

public class RelationalOperators {
    public static void main(String[] args) {
        int x = 10, y = 5;

        System.out.println("x == y: " + (x == y));
        System.out.println("x != y: " + (x != y));
        System.out.println("x > y: " + (x > y));
        System.out.println("x < y: " + (x < y));
    }
}

/**
* Output:
* x == y: false
* x != y: true
* x > y: true
* x < y: false
*/

Logical Operators

Logical operators connect two or more expressions and return a boolean result.

OperatorNameExample
&&Logical ANDa && b
Logical ORab
!Logical NOT!a

Example:

public class LogicalOperators {
    public static void main(String[] args) {
        boolean A = true, B = false;

        System.out.println("A && B: " + (A && B));
        System.out.println("A || B: " + (A || B));
        System.out.println("!A: " + (!A));
    }
}

/**
* Output:
* A && B: false
* A || B: true
* !A: false
*/

Bitwise Operators

Bitwise operators perform operations at the binary level and are commonly used in low-level programming.

OperatorNameExample
&Bitwise ANDa & b
^Bitwise XORa ^ b
Bitwise ORab
~Bitwise Complement~a
<<Left Shifta << 2
\>>Right Shifta >> 2

Example:

public class BitwiseOperators {
    public static void main(String[] args) {
        int a = 5, b = 3;  // 5 = 0101, 3 = 0011 (binary)

        System.out.println("a & b: " + (a & b));
        System.out.println("a | b: " + (a | b));
        System.out.println("a ^ b: " + (a ^ b));
        System.out.println("~a: " + (~a));
        System.out.println("a << 1: " + (a << 1));
        System.out.println("a >> 1: " + (a >> 1));
    }
}

/**
* Output:
* a & b: 1 (0001)
* a | b: 7 (0111)
* a ^ b: 6 (0110)
* ~a: -6 (inverts bits)
* a << 1: 10 (shifts left)
* a >> 1: 2 (shifts right)
*/

Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.

Precedence Table

PrecedenceOperatorsCategories
1 (Highest)(), []Parenthesis
2++, --, ~, !Unary
3*, /, %Multiplication, Division, Modulus
4+, -Addition, Subtraction
5<<, >>, >>>Bitwise Shift
6<, <=, >, >=Relational
7instanceofType comparison
8\==, !=Equality
9&Bitwise AND
10^Bitwise XOR
11Bitwise OR
12&&Logical AND
13Logical OR
14?:Ternary
15 (Lowest)\=, +=, -=, *=, /=, %=Assignment

Example:

public class OperatorPrecedence {
    public static void main(String[] args) {
        int result = 10 + 5 * 2;  // Multiplication (*) is evaluated before addition (+)

        System.out.println("10 + 5 * 2 = " + result);
        result = (10 + 5) * 2;  // Parentheses change precedence
        System.out.println("(10 + 5) * 2 = " + result);
    }
}

/**
* Output:
* 10 + 5 * 2 = 20
* (10 + 5) * 2 = 30
*/

Conclusion

Java operators enable calculations, comparisons, and logical operations. Understanding operator precedence ensures correct expression evaluation and prevents unexpected results. Mastering these fundamentals will help you write cleaner, more efficient code, forming a strong foundation for advanced programming.

More from this blog

Enchill Blogs

22 posts

I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.