Operators in Java


1. Introduction to Operators

1.1 What are Operators

Operators are symbols used to perform operations on variables and values.

1.2 Types of Operators

  • Arithmetic
  • Assignment
  • Relational
  • Logical
  • Bitwise
  • Unary
  • Ternary

1.3 Importance

Operators help in:

  • Performing calculations
  • Making decisions
  • Writing logical conditions

1.4 Operator Precedence

Operators follow a priority order (e.g., multiplication before addition).


2. Arithmetic Operators

2.1 Overview

Used for mathematical calculations.

2.2 Addition (+)

int sum = 10 + 5;

2.3 Subtraction (-)

int diff = 10 - 5;

2.4 Multiplication (*)

int product = 10 * 5;

2.5 Division (/)

int result = 10 / 5;

2.6 Modulus (%)

int remainder = 10 % 3;

2.7 Example

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

2.8 Common Mistakes

  • Integer division removes decimals
  • Division by zero causes error

3. Assignment Operators

3.1 Definition

Used to assign values to variables.

3.2 Simple Assignment

int x = 10;

3.3 Compound Assignment

x += 5;   // x = x + 5
x -= 2;
x *= 3;
x /= 2;
x %= 2;

3.4 Chained Assignment

int a, b, c;
a = b = c = 10;

4. Relational Operators

4.1 Definition

Used to compare two values.

4.2 Operators

10 == 10   // true
10 != 5 // true
10 > 5 // true
10 < 5 // false
10 >= 10 // true
10 <= 5 // false

4.3 Result

  • Always returns boolean (true/false)

5. Logical Operators

5.1 Definition

Used to combine conditions.

5.2 AND (&&)

true && false   // false

5.3 OR (||)

true || false   // true

5.4 NOT (!)

!true   // false

5.5 Example

int age = 20;
if (age > 18 && age < 30) {
    System.out.println("Eligible");
}

5.6 Short-Circuit

  • Stops evaluation early if result is already known

6. Bitwise Operators

6.1 Introduction

Operate on binary (bit-level) values.

6.2 Operators

5 & 3   // AND
5 | 3 // OR
5 ^ 3 // XOR
~5 // Complement
5 << 1 // Left shift
5 >> 1 // Right shift

6.3 Example

int a = 5;   // 0101
int b = 3; // 0011
System.out.println(a & b); // 0001 → 1

7. Unary Operators

7.1 Definition

Operate on a single operand.

7.2 Increment (++)

int x = 5;
x++;

7.3 Decrement (–)

x--;

7.4 Unary Plus and Minus

int a = +10;
int b = -10;

7.5 Pre vs Post

int x = 5;
System.out.println(++x); // 6
System.out.println(x++); // 6 (then becomes 7)

8. Ternary Operator

8.1 Definition

A shortcut for if-else.

8.2 Syntax

condition ? value1 : value2;

8.3 Example

int age = 18;
String result = (age >= 18) ? "Adult" : "Minor";

8.4 Nested Ternary

int num = 10;
String res = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";

8.5 Best Practices

  • Keep it simple
  • Avoid complex nesting

Conclusion

Operators are essential in Java programming as they allow you to perform calculations, compare values, and control program logic. By understanding different types of operators and their usage, beginners can write more efficient and meaningful Java programs.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *