Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Assignment Operators with Examples



Java Assignment Operators

Following are the assignment operators supported by Java language −

Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C
+= Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C − A
*= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

The following programs are simple examples which demonstrate the assignment operators. Copy and paste the following Java programs as Test.java file, and compile and run the programs −

Example 1

In this example, we're creating three variables a,b and c and using assignment operators. We've performed simple assignment, addition AND assignment, subtraction AND assignment and multiplication AND assignment operations and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int b = 20;
      int c = 0;

      c = a + b;
      System.out.println("c = a + b = " + c );

      c += a ;
      System.out.println("c += a  = " + c );

      c -= a ;
      System.out.println("c -= a = " + c );

      c *= a ;
      System.out.println("c *= a = " + c );
   }
}

Output

c = a + b = 30
c += a  = 40
c -= a = 30
c *= a = 300

Example 2

In this example, we're creating two variables a and c and using assignment operators. We've performed Divide AND assignment, Multiply AND assignment, Modulus AND assignment, bitwise exclusive OR AND assignment, OR AND assignment operations and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int c = 15;
      
	  c /= a ;
      System.out.println("c /= a = " + c );

      c = 15;
      c %= a ;
      System.out.println("c %= a  = " + c );

      c = 15;
      c &= a ;
      System.out.println("c &= a  = " + c );
	  
      c = 15;	  
      c ^= a ;
      System.out.println("c ^= a   = " + c );

      c = 15;
      c |= a ;
      System.out.println("c |= a   = " + c );
   }
}

Output

c /= a = 1
c %= a  = 5
c &= a  = 10
c ^= a   = 5
c |= a   = 15

Example 3

In this example, we're creating two variables a and c and using assignment operators. We've performed Left shift AND assignment, Right shift AND assignment, operations and printed the results.

public class Test {

   public static void main(String args[]) {
      int a = 10;
      int c = 0;

      c <<= 2 ;
      System.out.println("c <<= 2 = " + c );

      c = 15;
      c >>= 2 ;
      System.out.println("c >>= 2 = " + c );
   }
}

Output

c <<= 2 = 0
c >>= 2 = 3
java_basic_operators.htm
Advertisements