A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/java_assignment_operators_examples.htm below:

Java - Assignment Operators

Java - Assignment Operators

Java assignment operators are used to assign values to variables. These operators modify the value of a variable based on the operation performed. The most commonly used assignment operator is =, but Java provides multiple compound assignment operators for shorthand operations.

List of Java Assignment Operators

The following table lists the assignment operators in Java:

Operator Description Example = Simple assignment operator. Assigns values from right side operands to left side operand. C = A &plus; B will assign value of A &plus; B into C &plus;= Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C &plus;= A is equivalent to C = C &plus; 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 &ast;= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C &ast;= A is equivalent to C = C &ast; 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4