What are Assignment Operators in java



Assignment Operators >

Assignment operators can be used with all arithmetic, bitwise and bit shift operators.

Operator
Description
=
assignment operator

x = y  assigns y to x.

+=
Add AND assignment operator

x -= y  is equivalent to (x = x-y)
-=
Subtract AND assignment operator

x -= y  is equivalent to (x = x-y)
*=
Multiply AND assignment operator

x *= y  is equivalent to (x = x*y)
/=
Divide AND assignment operator

x /= y  is equivalent to (x = x/y)


Program 5 to demonstrate Assignment operators
/** JavaMadeSoEasy.com */
public class AssignmentOperatorTest {
   public static void main(String[] args) {
          int x=4;
          int y=2;
          x += y;
          System.out.println("+=   "+ x); //6
         
          x=4; y=2;
         
          x -= y;
          System.out.println("-=   "+ x); //2
         
          x=4; y=2;
         
          x *= y;
          System.out.println("*=   "+ x); //8
         
          x=4; y=2;
         
          x /= y;
          System.out.println("/=   "+ x); //2
         
   }
}
/*OUTPUT
+=   6
-=   2
*=   8
/=   2
*/

No comments:

Post a Comment