bscodinglab

Operators

Java provides various operators, such as arithmetic, assignment, comparison, logical, etc. Arithmetic operators perform basic arithmetic operations, assignment operators assign values to variables, comparison operators compare values, and logical operators perform logical operations like AND, OR, NOT.

In Java, operators are symbols or special characters used to perform various operations on variables and values. They allow you to manipulate data, perform arithmetic calculations, compare values, and control the flow of your program. Java operators are categorized into several types based on their functionality. Let’s explore the different types of operators in Java:

  1. Arithmetic Operators :- Arithmetic operators are used for basic mathematical calculations.
OperatorDescriptionExample
+Additionint result = 5 + 3;
-Subtraction`int result = 5 - 3;`
*Multiplication‘int result = 5 * 3;`
/Division`int result = 5 / 3;`
%Modulus (Remainder)`int result = 5 % 3;`
++Increment (postfix or prefix)`int i = 5; i++;` or `int j = 5; ++j;`
--Decrement (postfix or prefix)`int i = 5; i--;` or `int j = 5; --j;`

Example Arithmetic Operatore :-

// Addition 

int a =10;

int b = 5;

int sum = a + b;  // sum = 15

//Subtraction 

int Sub = a – b;  // difference = 5 

// Multiplication 

int multiplication = a * b;   // product = 50

// Division

int division = a / b;  // quotient = 2

// Module 

int remainder = a % b;   // remainder = 0

System.out.println(“Value of a = ” +a +” and b = ” +b);

System.out.println(“Calculation of A and B Is \t Sum = ” + sum + “\t Subtraction = ” + sub + “\t Multiplcaton = ” + multiplication + “\t Devision = ” + division + “\t Module = ” + remainder);

Output :-    Value of a = 10 and b = 5

Calculation of A and B Is Sum = 15 Subtraction = 5 Multiplcaton = 50 Devision = 2 Module = 0

2. Assignment Operators :- Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Simple assignmentint x = 10;
+=Addition assignmentx += 5;
-=Subtraction assignmentx -= 3;
*=Multiplication assignmentx *= 2;
/=Division assignmentx /= 4;
%=Modulus assignmentx %= 3;

Example :- 

public class Demo

{

public static void main(String []args)

{

int x = 4;
// If x is greater than 5, increment x by 1
if (x > 5)
     {
            x++;
            System.out.println(x);
       }
// If x is even, multiply x by 2
if (x % 2 == 0)
      {
              x *= 2; // Hare i Use Muliplication assignment Operatore
               System.out.println(x);
        }
}

    }

Output :-    8

  1. Comparison Operators :- Comparison operators are used to compare values and return a boolean result (`true` or `false`).
OperatorDescriptionExample
==Equal toif (x == y)
!=Not Equal toif (x != y)
>Greater thanif (x > y)
<Less thanif (x < y)
>=Greater than Equal toif (x >= y)
<=Less than equal toif (x <= y)
Example :-
public class ComparisonExample { public static void main(String[] args) { int a = 5; int b = 3; // Equal to boolean isEqual = (a == b); System.out.println("a == b: " + isEqual); // Output: false // Not equal to boolean isNotEqual = (a != b); System.out.println("a != b: " + isNotEqual); // Output: true // Greater than boolean isGreaterThan = (a > b); System.out.println("a > b: " + isGreaterThan); // Output: true // Less than boolean isLessThan = (a < b); System.out.println("a < b: " + isLessThan); // Output: false } }
Output :- false
ture
ture
false
  1. Logical Operators :- Logical operators are used to combine boolean expressions and evaluate their truth values.
OperatorDescriptionExample
&&Logical ANDif (x > 0 && x < 10)
||Logical ORif (x == 0 || x == 10)
!Logical ! (Negation)if (!flag)

Example:-

boolean a = true;
boolean b = false;
// Logical AND
boolean OP_And = a && b; //variable OP_And result is false
System.out.println(OP_And);
// Logical OR
boolean OP_Or = a || b; // variable OP_Or result is ture
System.out.println(OP_Or);
// Logical NOT
boolean OP_Not = !a; // variable OP_Not result is false
System.out.println(OP_Not);

Output :-    false

ture

false

  1. Bitwise Operators :- Bitwise operators perform operations at the binary level on individual bits of integer values.
OperatorDescriptionExample
&Bitwise ANDint result = a & b;
\Bitwise ORint result = a \| b;
^Bitwise XOR (exclusive OR)int result = a ^ b;
~Bitwise NOT (complement)int result = ~a;
<<Left Shiftint result = a << 2;
>>Right Shift (With Sign )int result = a >> 2;
>>>Right Shift (No Sign Extension)int result = a >>> 2;

Example:-

int num1 = 5; // binary: 0101
int num2 = 3; // binary: 0011

        int resultAnd = a & b;  // 0101 & 0011 = 0001
        System.out.println("a & b = " + resultAnd);  // Output: 1
        
        // Bitwise OR
        int resultOr = a | b;   // 0101 | 0011 = 0111
        System.out.println("a | b = " + resultOr);   // Output: 7
        
        // Bitwise XOR
        int resultXor = a ^ b;  // 0101 ^ 0011 = 0110
        System.out.println("a ^ b = " + resultXor);  // Output: 6
Output :-  1
6
7
  1. Ternary Operator (Conditional Operator) :- The ternary operator is a shorthand for an if-else statement, allowing you to make a quick decision based on a condition. in Java is a conditional operator that takes three operands and evaluates to one of two expressions based on the value of the first operand. It is also known as the conditional expression or the three-way conditional operator.

Syntax :-  variable = (condition) ? expression1 : expression2 

Example :- 

int x = 5;

String result = (x > 0) ? “Positive” : “Non-positive”;

Output :-  Positive

  1. Instanceof Operator :- The “instanceof” operator is used to check whether an object is an instance of a particular class or implements a specific interface.

  Syntax :-

Example :- 

public class Test {

   public static void main(String args[]) {

      String Msg = "This is string.";

      // following will return true when Msg Variable is type of String
      boolean result = name instanceof String;
      System.out.println( result );
   }
}

Output :-  true

These are the main categories of operators in Java. Understanding how to use operators effectively is essential for writing efficient and error-free Java code.