Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Operators
Arithmetic, Logical, Bitwise & More



Operators in Java

Operators in Java act like bridges between variables and values. They perform computations, comparisons, assignments, and logical decisions. Understanding these operators is fundamental before you dive into conditions, loops, and more complex logic in Java.

In this guide, we'll walk through all the primary types of Java operators, explaining each with easy-to-understand code examples and detailed output explanations.

Types of Java Operators

Arithmetic Operators in Java

Arithmetic operators are used to perform basic mathematical operations.

OperatorSymbolDescription
Addition+Adds two values
Subtraction-Subtracts right from left
Multiplication*Multiplies two values
Division/Divides left by right
Modulus%Returns remainder
public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 10, b = 3;
        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 / b = " + (a / b));
        System.out.println("a % b = " + (a % b));
    }
}
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1

Assignment Operators in Java

These operators assign values to variables.

OperatorSymbolDescription
Assign=Assigns right side to left variable
Add and Assign+=Performs addition then assigns
Subtract and Assign-=Performs subtraction then assigns
Multiply and Assign*=Performs multiplication then assigns
Divide and Assign/=Performs division then assigns
Modulus and Assign%=Performs modulus then assigns
public class AssignmentExample {
    public static void main(String[] args) {
        int a = 5;
        a += 3;
        a *= 2;
        System.out.println("Final value of a: " + a);
    }
}
Final value of a: 16

Relational Operators in Java

Relational operators compare two values and return a boolean result.

OperatorSymbolDescription
Equal To==Checks if two values are equal
Not Equal To!=Checks if two values are not equal
Greater Than>Checks if left is greater
Less Than<Checks if left is smaller
Greater Than or Equal>=Checks if left is greater or equal
Less Than or Equal<=Checks if left is smaller or equal
public class RelationalExample {
    public static void main(String[] args) {
        int x = 10, y = 20;
        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));
    }
}
x == y: false
x != y: true
x < y: true
x > y: false

Logical Operators in Java

Logical operators help in combining multiple boolean expressions.

OperatorSymbolDescription
Logical AND&&True if both operands are true
Logical OR||True if any operand is true
Logical NOT!Inverts the boolean value
public class LogicalExample {
    public static void main(String[] args) {
        int a = 5, b = 10;
        System.out.println((a > 0) && (b > 0));
        System.out.println((a > 0) || (b < 0));
        System.out.println(!(a == b));
    }
}
true
true
true

Unary Operators in Java

Unary operators operate on a single operand.

OperatorSymbolDescription
Unary Plus+Indicates positive value
Unary Minus-Negates the value
Increment++Increases value by 1
Decrement--Decreases value by 1
Logical NOT!Inverts a boolean
public class UnaryExample {
    public static void main(String[] args) {
        int a = 5;
        System.out.println("a = " + a);
        System.out.println("++a = " + (++a));
        System.out.println("a-- = " + (a--));
        System.out.println("Final a = " + a);
    }
}
a = 5
++a = 6
a-- = 6
Final a = 5

Bitwise Operators in Java

Bitwise operators work on bits and perform bit-by-bit operations.

OperatorSymbolDescription
AND&Bitwise AND
OR|Bitwise OR
XOR^Bitwise exclusive OR
Complement~Bitwise NOT
Left Shift<<Shift bits left
Right Shift>>Shift bits right
public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3;
        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));
    }
}
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2

Things to Remember

QUIZ

Question 1:Which of the following outputs will result from the given code snippet?

int a = 10, b = 3;
System.out.println("a % b = " + (a % b));

Question 2:The statement a += 3 is equivalent to a = a + 3 in Java.

Question 3:Which of the following are valid relational operators in Java?

Question 4:What is the output of the following code?

int a = 5;
System.out.println(++a);
System.out.println(a--);
System.out.println(a);

Question 5:The operator && performs a bitwise AND operation in Java.

Question 6:Which statements about bitwise operators are true in Java?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M