Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » OperatorsRSS Feeds

Assignment Operators and Shortcut Assignment Operators

Posted By: Byron Powell     Category: Java     Views: 14245

This article lists and explains assignment and shortcut assignment operators available in java.

Assignment operators are used to assign value to a variable. The variable are always on the left-hand side of the assignment operator and the value to be assigned is always on the right-hand side of the assignment operator. The assignment operator is evaluated from right to left, so a = b = c = 0; would assign 0 to c, then c to b then b to a.

The shortcut assignment operator can be used for all Arithmetic Operators i.e. You can use this style with all arithmetic operators.

Assignment Operators with examples

 Operator

Meaning and example 

 = 

Assignment operator

Example a = b

b is evaluated and a set to this value.

 +=, -=, *=, /=, %=

Arithmetic operation and then assignment 

Example a -= b is equivalent to a = a - b

&=, |=, ^= 

Bitwise operation and then assignment

Example a &= b is equivalent to a = a & b 

 <<=, >>=, >>>=

Shift operations and then assignment

Example a <<= b is equivalent to a = a << b  



Examples of Assignment Operators

Example 1 : Program that displays use of all assignment and shortcut assignment operators i.e. =, +=, -=, *=, /=, %=, &= etc

class AssignOptrDemo 
{
   public static void main(String[] args) 
   {
int a = 10, b = 15, c = 15;
System.out.println("Assignment and shortcut assignment operators");
System.out.println(" a = " + (a = 15)); 
System.out.println(" Addition  = " + (a += b)); 
System.out.println(" Subtraction  = " + (c -= b)); 
System.out.println(" Division = " + (a /= 2)); 
System.out.println(" Multiplication = " + (a *= 2)); 

   }
}


Output

Assignment and shortcut assignment operators
a = 15
Addition = 30
Subtraction = 0
Division = 15
Multiplication  = 30
  
Share: 


Didn't find what you were looking for? Find more on Assignment Operators and Shortcut Assignment Operators Or get search suggestion and latest updates.

Byron Powell
Byron Powell author of Assignment Operators and Shortcut Assignment Operators is from Houston, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!