Operator

Assignment Operator

  • The assignment operator in Java language is used to assign a value to a variable.
  • Syntax:

variable name = expression;

  • where an expression may be a single constant or complex as required.

Types of Assignment Operators:

(1) “=”

  • It is a simple assignment operator. 

Example:

                            ch=’a’;

       x=50;

  • It is used to assign the value to the variable present on the left side of the operator.

(2) “+=”

  • It is the combination of two operators, i.e., ‘+’ and ‘=’. 

          Example:

(x += y) is same as (x = x+y)

  • It first adds the current value of the variable on the left side of the ‘+’ operator to the value on the right side of the operator ‘=’.
  • It then assigns the result to the variable on the left.

(3) “-=”:

  • This operator is the combination of two operators, i.e., ‘-‘ and ‘=’.

Example:

(x-=y) is same as (x=x-y)

  • It first subtracts the current value of the variable on the left side of the ‘-‘ operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(4) “*=”:

  • This operator is combination of two operators i.e., ‘* ‘and ‘=’.

Example:

(x *= y) is same as (x= x * y)

  • It first multiplies the current value of the variable on the left side of the ‘*’ operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(5) “/=”:

  • This operator is the combination of two operators, i.e., ‘/’and ‘=’.

Example:

(x /= y) is same as (x = x/ y)

  • It first divides the current value of the variable on the left side of the ‘/’ operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained result to the variable on the left.

(6)”%=”:

  • This operator is the combination of two operators, i.e., ‘% ‘and ‘=’.

Example:

(x %= y) is same as (x = x% y)

  • It first divides the current value of the variable on the left side of the ‘%’ operator to the value on the right side of the operator ‘=’.
  • It then assigns the obtained remainder of the division to the variable on the left.

Example Program:

Output:

Arithmetic Operator

Arithmetic Operator

An arithmetic operator performs common mathematical operations such as addition, subtraction, multiplication, division and modulus on numerical values (constants and variables).

Different types of Arithmetic Operators:

(1)  ‘+’:

          It performs the addition of two values.

          Example:

                             x + y

(2) ‘-‘:

          It performs the subtraction of one value with another.

          Example:

                             x – y

(3) ‘*’:

          It performs the multiplication of two values.

          Example:

                             x * y

(4) ‘/’:

          It performs the division of one value with another.

          Example:

                             x / y

(5) ‘%’:

          It provides the Remainder after dividing two values.

          Example:

                             x  % y

Example:

Output:

Increment Operator

Increment Operator

  • Java includes increment operator “++” to simplify common operations.
  • The increment operator “++” adds 1 to its operand, and its value gets modified.

Example:

x = x+1;

      is the same as

          ++x;

  • Increment operator may be either precede (prefix) or follow (postfix) the operand.

Example:

x = x+1;

can be written

++x; or x++;

  • In Java, we can’t apply increment operator to the variables declared as final because their value cannot be modified.
  • We cannot use increment operator with boolean.

Example Program:

package Sample;

public class IncrementOperator {

       public static void main(String[] args)

    {

        int a = 10;

        int b = ++a;

        System.out.println(b);

    }

}

Output:

11

Decrement Operator

  • Java includes decrement operator “–” to simplify common operation.
  • The decrement operator “–” subtracts 1 to its operand, and its value gets modified.

Example:

x = x-1;

is the same as

 –x;

  • Decrement operator may be either precede (prefix) or follow (postfix) the operand.

Example:

 x = x-1;

can be written

–x; or x–;

  • In Java, we can’t apply increment operator to the variables declared as final because their value cannot be modified.
  • We cannot use increment operator with boolean.

Example Program:

package Sample;

public class DecrementOperator {

       public static void main(String[] args)

    {

        int a = 10;

        int b = –a;

        System.out.println(b);

    }

}

Output:

9

Relational Operators

  • Relational operators are used to check the type of relationship between two values.

 Different types of Relational Operators:

= =

!=

>=

<=

  1. ‘==’:

    It is known as “Equal to operator”.

    It checks whether the two given values are equal or not. If they are equal, then it     returns true or else false. 

Example:

2==3 returns false.

4==4 returns true.

  1. “!=”:
  • It is known as “Not equal to operator”.
  • It is the Boolean complement of the “==” operator.
  • It checks whether the two given values are equal or not. If so, then it returns true or else false.

           Example:

2!=5 returns true.

1!=1 returns false.

  1. “>”:
  • It is known as “Greater than operator”.
  • It checks whether the left side value of”>” is greater than the right side value or not. 
  • If they are greater, then it returns true or else false.

Example:

                   2>5 returns false.

                   4>1 returns true.

  1. “<“:
  • It is known as “Lesser than operator”.
  • It checks whether the left side value of “< “is lesser than the right side value or not. 
  • If it is lesser, then it returns true or else false.

Example:

                   5<2 returns true.

                   3<5 returns false.

  1. “>=”:
  • It is known as “Greater than or equal to operator”.
  • It checks whether the left side value of “>=” is greater than the right side value or not. 
  • If so, then it returns true or else false.

Example:

                   3>=3 returns true.

                   2<=1 returns false.

  1. “<=”:
  • It is known as “Less than or equal to operator”.
  • It checks whether the left side value of “<=” is lesser than or equal to the right side value or not. 
  • If so, then it returns true or else false.

Example:

                   2<=5 returns true.

                   5<=1 returns false.

Program:

package Sample;

public class RelationalOperators {

     public static void main(String[] args) {

         int a = 5, b = 10;

         System.out.println(“a= ” + a + ” & b = ” + b);

         System.out.println(a == b); 

         System.out.println(a != b); 

         System.out.println(a > b); 

         System.out.println(a < b); 

         System.out.println(a >= b); 

         System.out.println(a <= b); 

       }

}

Output:

a= 5 & b = 10

false

true

false

true

false

true

Logical Operator

  • Logical operators are used in decision making in Java.
  • There are three logical operators supported by the Java language. 

They are shown below:

(1) &&

(2) ||

(3) !

  1. ‘&&’ operator:
  • It is known as Logical AND operator.
  • It returns true when both the given conditions are satisfied. Otherwise, returns false.

                   Example:

 x && y returns true when both a and b are true or else returns false.

  1. ‘||’ operator:
  • It is known as a Logical OR operator. 
  • It returns true even if one or both given conditions are satisfied. Otherwise, returns false.

 Example:

                             x || y returns true if x or y or both are true. Otherwise, returns false.

  1. ‘!’ operator:
  • It is known as Logical NOT operator.
  • The ‘!’operator returns true if the given condition is not satisfied. Otherwise, returns false. 

Example:

!x returns true if x is false. Otherwise, returns false.

Program:

package Sample;

public class LogicalOperator {

     public static void main(String[] args) {

         int a = 3, b = 5, c = 8;

         System.out.println(“a= ” + a + ” & b = ” + b + ” & c = ” + c);

         System.out.println((b > a) && (c > b)); 

         System.out.println((b > a) && (c < b)); 

         System.out.println((b < a) || (c > b)); 

         System.out.println((b < a) || (c < b));

         System.out.println(!(b == a)); 

         System.out.println(!(a > c)); 

       }

}

Output:

a= 3 & b = 5 & c = 8

true

false

true

false

true

true

Bitwise Operator

Bitwise Operator

  • Bitwise operators in Java are used for testing, shifting, setting the actual bits into a byte or word.
  • We cannot use bitwise operations on the float, double, long double, void, or other more complex types.

 Bitwise Operators are shown below:

 (1) &

 (2) |

 (3) ^

 (4) <<  

 (5) >>

 (6) ~

 (7) >>>

  1. ‘&’ Operator:

 It is known as Bitwise AND Operator.

  • It takes two numbers as operands and performs AND operation on every bit of two numbers.
  • If both the given bits are 1, then the result of AND operation is 1.

Program:

package Sample;

public class BitwiseAND {

     public static void main(String[] args)  

     {  

          int a = 2, b = 3;   

     System.out.println(“a & b = ” + (a & b));  

     } 

}

Output:

a & b = 2

  1. ‘|’ Operator:

 It is known as Bitwise OR Operator.

  • It takes two numbers as operands and performs OR operation on every bit of two numbers.
  • If one of the given two bits is 1, then the result of OR Operation is 1.

Program:

package Sample;

public class BitwiseOR {

     public static void main(String[] args)  

     {  

     int a = 2, b = 3;    

     System.out.println(“a | b = ” + (a | b));  

     } 

}

Output:

a | b = 3

  1. ‘^’ Operator:
  • It is known as the Bitwise XOR operator.
  • It takes two numbers as operands and performs an XOR operation on every bit of two numbers.
  • If two bits are different, then the result of the XOR operation is 1.

Program:

package Sample;

public class BitwiseXOR {

     public static void main(String[] args)  

     {  

     int a = 9, b = 8;    

     System.out.println(“a ^  b= ” + (a ^ b));  

     } 

}

Output:

a ^  b= 1

  1. ‘<<‘ Operator:
  • It is known as the Left Shift operator.
  • It takes two numbers as operands.
  • It left shifts the bits of the first operand, and the second operand defines the number of places to shift.

Program:

package Sample;

public class LeftShiftOperator {

     public static void main(String args[])  

     {  

     int a = 10;  

     System.out.println(“a<<1 = ” + (a << 1));  

     } 

}

Output:

a<<1 = 20

  1. ‘>>’ Operator:
  • It is known as the Right Shift operator.
  • It takes two numbers; right shifts the bits of the first operand and the second operand defines the number of places to shift.

Program:

package Sample;

public class RightShiftOperator {

     public static void main(String args[])  

     {  

     int a = 40;  

     System.out.println(“a>>2 = ” + (a >>2));  

     } 

}

Output:

a>>2 = 10

  1. ‘~’ Operator:
  • It is known as Bitwise NOT Operator.
  • It takes one number and compliments all bits of it.

Program:

package Sample;

public class BitwiseNOT {

     public static void main(String[] args)  

     {  

     int a = 5;   

     System.out.println(“~a = ” + (~a));  

     } 

}

 Output:

~a = -6

  1. ‘>>>’ Operator:
  • It is known as Unsigned Right Shift Operator.
  • It shifts a zero at the leftmost position and fills 0.

Example:

If x=11100000 and y=2, find x>>>y?

x >>> y = 11100000 >>> 2 = 00111000

Program:

package Sample;

public class UnsignedRightShift {

     public static void main(String args[])  

     {  

     int a = 20;  

     System.out.println(“a>>>2 = ” + (a >>>2));  

     } 

}

Output:

a>>>2 = 5

Who is the course for?

Prerequisites before starting this course:

Questions 🙵 Answers.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu orci faucibus orci malesuada semper eget non tellus. Cras sed dignissim purus. Mauris varius neque leo, eu pellentesque justo venenatis et. Sed ultricies risus non turpis tempus, nec  nulla suscipit. In comdo urna eu turpis accumsan, et viverra mauris fringillaCras interdum 

Video 48 Min  + 2 Min read to complete

More Courses

You might also be interested in these courses