Scanner class Methods in Java

Java-Assignment Operators

 Assignment Operator 

The assignment operator is the most commonly used operator. Assignment means to assign something to operand or expression. We have already used assignment operator, but now we will understand which one is the assignment and how it assigns a value.


The Assignment Operator(=) is the most basic operator.In general, it is known as an equal to . It is used to assign some value to the operand. It is a binary operator. It assign the right side value to the left operand.The right side value can be an operand, expression or simple constant.

Syntax:  operand1 = operand2;

For example:  
  1. a=5; here, the = operator is assigning value 5 to variable named "a",
  2. a=b; here, the = operator is assigning the value of "b" variable to "a",
  3. c=a+b; here, the = operator assigns the sum of "a" and "b" to variable named "c",
  4. c=8-2; here, the = operator assigns the difference of two numeric numbers to variable "c".

Shorthand Assignment Operators

These operators are the combination of Assignment(=) with various other operators. Shorthand Assignment Operators are used to make the code less complex and less space consuming.By using these operators , we need not to write the same operand again and again in an expression.

There are total 11 Shorthand Assignment Operators. Five of them are Arithmetic and 6 are Bitwise shorthand operators.

Arithmetic Shorthand Assignment Operators:

These operators are made by grouping arithmetic operators with the Assignment Operator(=).To understand Arithmetic Operators, click here.Lets understand each with the examples:-
  1. Addition Assignment Operator (+=) : This operator is used to add first operand to second operand without writing the  statement. like, operand1= operand1+operand2, instead we can write operand1+=operand2. Both statements will print the same output. For example, a=2;a+=5, the answer will be 7.
  2. Subtraction Assignment Operator (-=) :  This operator is used to  subtract second operand from first operand without writing the statement. like, operand1= operand1-operand2, instead we can write shorthand as operand1-=operand2. For example, a=8;a-=5, the answer will be 3.
  3. Multiplication Assignment Operator (*=) :  This operator is used to multiply first operand with second operand without writing the statement. like, operand1= operand1*operand2, instead we can write operand1*=operand2.For example, a=2;a*=5, the answer will be 10.
  4. Division Assignment Operator (/=) :  This operator is used to divide first operand by second operand without writing the statement. like, operand1= operand1/operand2, instead we can write operand1/=operand2. For example, a=10;a/=5, the answer will be 2.
  5. Modulus Assignment Operator (%=) :  This operator is used to find the remainder of first operand on division by second operand without writing the statement. like, operand1= operand1%operand2, instead we can write operand1%=operand2.  For example, a=7;a%=5, the answer will be 2.
 Example:Now ,we will see and example of Arithmetic Shorthand Assignment Operators:

Java Arithmetic Assignment Operators example
    Output: Comments explain everything in code.Remember a's values is updating after each statement.
Java Arithmetic Assignment Operators example output


Bitwise Shorthand Assignment Operators:

These operators are made by grouping Bitwise operators with the Assignment Operator(=).To understand Bitwise Operators, click here .Lets understand each with the examples:-
  1. Bitwise AND Assignment Operator (&=) :  This operator is used  to save the result of Bitwise AND(&) operation in operand1,we can replace statement operand1= operand1&operand2, with operand1&=operand2. Both statements will print the same output. For example, a=0;a&=1, the answer will be 0.
  2. Bitwise OR Assignment Operator (|=) :  This operator is used to save the result of Bitwise OR(|) operation in operand1,we can skip writing statement like, operand1= operand1|operand2, instead we can write shorthand as operand1|=operand2. For example, a=1, a|=1, the answer will be 1.
  3. Bitwise XOR Assignment Operator (^=) :  This operator is used to  save the result of Bitwise XOR(^) operation in operand 1,we can skip writing statement like, operand1= operand1^operand2, instead we can write operand1^=operand2For example, a=0;a^=0, the answer will be 1.
  4. Bitwise Shift Left Assignment Operator (<<=) : : This operator is used to save the result of Bitwise Shift Left(<<) operation in operand1,we can skip writing statement like, operand1= operand1<<operand2, instead we can write operand1<<=operand2. For example, a=0011;a<<=1, the answer will be 0110.
  5. Bitwise Shift Right Assignment Operator (>>=) :  This operator is used to  save the result of Bitwise Shift Right(>>) operation in operand1,we can skip writing statement  like, operand1= operand1>>operand2, instead we can write operand1>>=operand2. For example, a=0011;a>>=1, the answer will be 0001.
  6. Bitwise Shift Right Zero Fill Assignment Operator (>>>) :  This operator is used to save the result of Bitwise Shift Right Zero Fill(>>>) operation operand1,we can skip writing statement  like, operand1= operand1>>>operand2, instead we can write operand1>>>=operand2. For example, a=1011;a>>>=1, the answer will be 0101.
 Example: Now, we will see an example of Bitwise Shorthand Assignment Operators:
Bitwise Assignment Operators in Java example
Output: Comments explain the code.
Bitwise Assignment Operators in Java example

Experiment: Try different inputs (a=7,10 and b=3,9 and z=9) and analyse the outputs.
Keep Learning! Happy Coding!

Comments