OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Introduction to Operators and Types of Operators in Java

Operators in Java – Complete Detailed Notes

Operators in Java are special symbols used to perform operations on variables and values. They are essential for building expressions and controlling logic.


Classification of Java Operators

Java operators are broadly categorized into the following:

  1. Arithmetic Operators

  2. Relational (Comparison) Operators

  3. Logical Operators

  4. Assignment Operators

  5. Unary Operators

  6. Bitwise Operators

  7. Ternary Operator

  8. Shift Operators

  9. Instanceof Operator

  10. Type Cast Operator


1. Arithmetic Operators

Used to perform basic mathematical operations.

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

Example:


int a = 10, b = 3; System.out.println(a + b); // Output: 13 System.out.println(a % b); // Output: 1

Example program
public class ArithmeticDemo { public static void main(String[] args) { int a = 15, b = 4; System.out.println("Addition: " + (a + b)); // 19 System.out.println("Subtraction: " + (a - b)); // 11 System.out.println("Multiplication: " + (a * b)); // 60 System.out.println("Division: " + (a / b)); // 3 System.out.println("Modulus: " + (a % b)); // 3 } }


2. Relational (Comparison) Operators

Used to compare two values. Returns a boolean (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equala >= b
<=Less than or equala <= b

Example Program

public class RelationalDemo {
    public static void main(String[] args) {
        int x = 10, y = 20;
        System.out.println("x == y: " + (x == y)); // false
        System.out.println("x != y: " + (x != y)); // true
        System.out.println("x > y: " + (x > y));   // false
        System.out.println("x < y: " + (x < y));   // true
        System.out.println("x >= y: " + (x >= y)); // false
        System.out.println("x <= y: " + (x <= y)); // true
    }
}

3. Logical Operators

Used to combine multiple boolean expressions.

OperatorDescriptionExample
&&Logical ANDa > 5 && b < 10
``
!Logical NOT!(a == b)

Example Program

 public class LogicalDemo {

    public static void main(String[] args) {

        int a = 5, b = 10;

        boolean result1 = (a < b) && (b > 5);

        boolean result2 = (a > b) || (b == 10);

        boolean result3 = !(a == b);

         System.out.println("AND: " + result1); // true

        System.out.println("OR: " + result2);  // true

        System.out.println("NOT: " + result3); // true

    }

}


4. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assigna = 10
+=Add and assigna += 5a = a + 5
-=Subtract and assigna -= 3
*=Multiply and assigna *= 2
/=Divide and assigna /= 4
%=Modulus and assigna %= 3

Example Program

public class AssignmentDemo {
    public static void main(String[] args) {
        int a = 10;
        a += 5;  // a = a + 5
        System.out.println("a += 5: " + a);  // 15

        a *= 2;  // a = a * 2
        System.out.println("a *= 2: " + a);  // 30

        a -= 10; // a = a - 10
        System.out.println("a -= 10: " + a); // 20
    }
}

5. Unary Operators

Operate on a single operand.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Increment++a, a++
--Decrement--a, a--
!Logical complement!truefalse

Pre-increment and Post-increment:


int a = 5; System.out.println(++a); // 6 (pre-increment) System.out.println(a++); // 6 (post-increment, then becomes 7)

Example Program
public class UnaryDemo { public static void main(String[] args) { int x = 5; System.out.println("x: " + x); // 5 System.out.println("++x: " + ++x); // 6 System.out.println("x++: " + x++); // 6 (then x becomes 7) System.out.println("--x: " + --x); // 6 System.out.println("x--: " + x--); // 6 (then x becomes 5) System.out.println("Final x: " + x); // 5 } }

6. Bitwise Operators

Used to perform operations on bits.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise complement~a

Example Program
public class BitwiseDemo {
    public static void main(String[] args) {
        int a = 5;  // 0101
        int b = 3;  // 0011

        System.out.println("a & b: " + (a & b)); // 1 (0001)
        System.out.println("a | b: " + (a | b)); // 7 (0111)
        System.out.println("a ^ b: " + (a ^ b)); // 6 (0110)
        System.out.println("~a: " + (~a));       // -6
    }
}

7. Ternary Operator

Also known as the conditional operator. It's a shortcut for if-else.

Syntax:


condition ? expression1 : expression2

Example:

int a = 10, b = 20;
int max = (a > b) ? a : b; // max = 20

Example Program

public class TernaryDemo { public static void main(String[] args) { int a = 10, b = 20; int max = (a > b) ? a : b; System.out.println("Maximum is: " + max); // 20 } }

8. Shift Operators

Used to shift bits left or right.

OperatorDescriptionExample
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example Program

public class ShiftDemo {
    public static void main(String[] args) {
        int a = 8; // 1000
        System.out.println("a << 2: " + (a << 2)); // 32
        System.out.println("a >> 2: " + (a >> 2)); // 2
        System.out.println("a >>> 2: " + (a >>> 2)); // 2
    }
}

9. instanceof Operator

Checks if an object is an instance of a specific class or subclass.


String s = "Hello"; System.out.println(s instanceof String); // true

Example Program

public class InstanceofDemo { public static void main(String[] args) { String s = "Hello"; System.out.println(s instanceof String); // true } }


10. Type Cast Operator

Used to convert one data type into another.

Example:


int a = 10; double b = (double) a; // Type casting int to double


Example Program

public class TypeCastDemo { public static void main(String[] args) { int a = 10; double d = (double) a; System.out.println("Converted to double: " + d); // 10.0 } }


Summary Table of Java Operators

Type
Operators
Arithmetic
+, -, *, /, %
Relational
==, !=, >, <, >=, <=
Logical
&&, `
Assignment
=, +=, -=, *=, /=, %=
Unary
+, -, ++, --, !
Bitwise
&, `
Shift
<<, >>, >>>
Ternary
condition ? trueVal : falseVal
instanceof
object instanceof ClassName
Type Cast
(targetType) value


Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC