Java – Assignment Operator2 min read

Assignment operator is used to assigning a value to any variable. This operator assigns the value of the righthand side of an operator to the lefthand side. Various assignment operator in Java:
These are the short version formed by combining the two operators.

For example Instead of writing, we can write a+= 5.

  • Assignment operator: ‘=’ This operator assigns the value on the right to the variable on the left.
    a = 20; B = 30; Ch = 'cha';
  • Add AND assignment operator: ‘+=’ This operator first adds the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
    c+=7; a+=b;
  • Subtract AND assignment operator: ‘-=’ This operator first subtracts the current value of the variable on the left from the value on the right and then assigns the result to the variable on the left.
    c-=7; a-=b;
  • Multiply AND assignment operator: ‘*=’ This operator first multiplies the current value of the variable on the left to the value on the right and then assigns the result to the variable on the left.
    c*=7; a*=b;
  • Divide AND assignment operator: ‘/=’ This operator first divides the current value of the variable on the left by the value on the right and then assigns the result to the variable on the left.
    c/=7; a/=b;
  • Modulus AND assignment operator: ‘%=’ It takes modulus using two operands and assigns the result to the left operand.
    C %= A is equivalent to C = C % A ;
  • Right shift AND assignment operator: ‘>>=’ This operator is used for Right shift Operation.
    C >>= 2 ;
  • Left shift AND assignment operator: ‘<<=’ This operator is used for Left shift Operation.
    C <<= 2 ;
  • Bitwise AND assignment operator: ‘&=’ This operator is used for Bitwise AND Operations.
    C &= 2;
  • Bitwise exclusive OR and assignment operator: ‘^= This operator is used for Bitwise exclusive OR Operations.
    C ^= 2;
  • Bitwise inclusive OR and assignment operator: ‘|= This operator is used for Bitwise inclusive OR.
    C |= 2;

Example of Assignment Operator:

Output:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …