33

Operators

Operators are special symbols used to perform  operations on variables and values. Java supports  several types of operators, which can be broadly  classified into the following categories:

Types of operators java

  • Arithmetic Operators
  • Unary Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operator
  • Instanceof Operator

1. Arithmetic Operator

These operators are used to perform basic arithmetic  operations.

  • + : Addition
  • – : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus (remainder)

Program

1
2
3
4
5
6
7
8
9
10
11
12
13
public class DemoArithmeticOperator
{
  public static void main(String[] args)
  {
  int a = 10;
  int b = 5;
  System.out.println("a+b = "+ (a+b));//15
  System.out.println("a-b = "+ (a-b));//5
  System.out.println("a*b = "+ (a*b));//50
  System.out.println("a/b = "+ (a/b));//2
  System.out.println("a%b = "+ (a%b));//0
  }
}

Output :-

1
2
3
4
5
a+b = 15
a-b = 5
a*b = 50
a/b = 2
a%b = 0

2. Unary Operator

These operators perform  operations on a single operand.

  • + : Unary plus (Indicates  positive value)
  • – : Unary minus (Negates an  expression)
  • ++ : Increment
  • — : Decrement
  • ! : Logical complement  (Inverts the value of a  boolean)

Program

1
2
3
4
5
6
7
8
9
10
11
12
public class UnaryOperators
 {
   public static void main(String[] args)
  {
    int a = 10;
     int b = -a; // b is -10
     a++; // a is now 11
       b--; // b is now -11
     boolean c = true;
        System.out.println(!c); // false
  }
}

Output :-

1
false

3. Assignment Operators

These operators are used to  assign values to variables.

  • = : Simple assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign

program

1
2
3
4
5
6
7
8
9
10
11
12
public class AssignmentOperators {
    public static void main(String[] args)
    {
        int a = 10;
        a += 5; // a is now 15
        a -= 2; // a is now 13
        a *= 3; // a is now 39
        a /= 3; // a is now 13
        a %= 4; // a is now 1
        System.out.println(a);
    }
}

Output :-

1
1

4. Relation Operators

These operators are used to  compare two values.

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

program

1
2
3
4
5
6
7
8
9
10
11
12
13
public class RelationalOperator {
    public static void main(String[] args)
    {
        int a = 10;
        int b = 5;
        System.out.println(a == b); // false
        System.out.println(a != b); // true
        System.out.println(a > b); // true
        System.out.println(a < b); // false
        System.out.println(a >= b); // true
        System.out.println(a <= b); // false
    }
}

Output :-

1
2
3
4
5
6
false
true
true
false
true
false

5. Logical Operators

These operators are used to  combine multiple boolean  expressions.

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT

program

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.operator;
public class LogicalOperator
{
 
public static void main(String[] args)
    {
    boolean a = true;
    boolean b = false;
    System.out.println(a && b); // false
    System.out.println(a || b); // true
    System.out.println(!a); // false
    }
}

Output :-

1
2
3
false
true
false

6. Bitwise Operators

These operators are used to  perform bit-level operations.

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR
  • ~ : Bitwise Complement
  • << : Left shift
  • >> : Right shift
  • >>> : Unsigned right shift

program

1
2
3
4
5
6
7
8
9
10
11
public class BitwiseOperator
{
    public static void main(String[] args)
    {
    int a=5,b=7;
    System.out.println("AND " + (a & b));
    System.out.println("OR " + (a | b));
    System.out.println("XOR " + (a ^ b));
    System.out.println("Complement " + (~a));
    }
}

Let’s :-Table int a=5(0101)       b=(0111)

Output :-

1
2
3
4
AND 5
OR 7
XOR 2
Complement -6

7. Ternary Operators

This is a shorthand for if-else  statements. It is also known  as the conditional operator.

  • ? : : Ternary operator

program

1
2
3
4
5
6
7
8
9
public class TernaryOperator {
    public static void main(String[] args)
    {
        int a = 10;
        int b = 5;
        int max = (a > b) ? a : b;
        System.out.println("Max value: " + max); // 10
    }
}

Output :-

1
Max value: 10

8. Instanceof Operators

This operator is used to test whether an object is an instance of a specific class or subclass.

program

1
2
3
4
5
6
7
public static void main(String[] args)
    {
        String str = "Hello";
        boolean result = str instanceof String;
        System.out.println(result); // true
    }
}

Output :-

1
true

Control Flow Statements

These statements control the execution flow of a  program, allowing for decision making, looping, and  branching.

 DECISION-MAKING STATEMENTS

 These statements allow the program to make  decisions based on certain conditions.

  • IF STATEMENT
  • IF-ELSE STATEMENT
  • IF-ELSE-IF LADDER 
  • SWITCH STATEMENT

1. If Statement

Executes a block of code if a specified  condition is true.

1
2
3
4
Syntax if Statement :-
if (condition) {
    // code to be executed if condition is true
}

program

1
2
3
4
5
6
7
8
9
10
public class If_statements
{
public static void main(String[] args)
{
    int a = 10;
    if (a > 5) {
        System.out.println("a is greater than 5");
}
}
}

Output :-

1
a is greater than 5

2. If-Else Statement

Executes one block of code if a condition  is true, and another block if it is false.

1
2
3
4
5
6
7
Syntax if else Statement :-
if (condition)
{
    // code to be executed if condition is true
else
   // code to be executed if condition is true
}

program

1
2
3
4
5
6
7
8
9
10
11
12
public class If_Else_statements
{
    public static void main(String[] args)
    {
        int a = 10;
        if (a > 20) {
            System.out.println("a is greater than 20");
        } else {
            System.out.println("a is not greater than 20");
        }
    }
}

Output :-

1
a is not greater than 20

3. if-else-if Ladder

Allows for multiple conditions to be  checked sequentially.

program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class If_else_If_Ladder
{
public static void main(String[] args)
{
    int a = 10;
    if (a > 10) {
        System.out.println("a is greater than 10");
    } else if (a == 10) {
        System.out.println("a is equal to 10");
    } else {
        System.out.println("a is less than 10");
    }
}
}

Output :-

1
a is equal to 10

4. Switch Statements

Allows for a variable to be tested for equality against a list of values.

1
2
3
4
5
6
7
8
9
10
11
12
13
Syntax:-
switch (expression)
{
    case value1:
        // code to be executed if expression == value1
        break;
    case value2:
        // code to be executed if expression == value2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}

program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Switch_Statements {
    public static void main(String[] args)
    {
        int day = 3;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            default:
                System.out.println("Other day");
                break;
        }
    }
}

Output :-

1
Wednesday
1
2
trtrt
rtrtrtrt

Write A Comment