by Dinesh Thakur Category: Operators and Expressions

Besides the assignment operator in Java has combined operators assignment. They contribute to reducing the amount of code as impracticable two operations by an operator. The combined operators have the following syntax:

operand1 operator = operand2 ;

The above expression is identical to the following :

operanda1 = operanda1 operator operanda2 ;

Here is an example of a combined assignment operator :

int x = 2 ;

int y = 4 ;

x * = y; / / Same as x = x * y;

System.out.println (x); / / 8

The most commonly used compound assignment operators are + = (value added operand2 to operand1) - = ( subtract the value of operand right by the value of the first on the left ) . Other composite operators assignment are * = , / = and % = . The following example gives a good idea of ​​the combined operators assignment and their use:

int x = 6 ;

int y = 4 ;

System.out.println (y * = 2 ); / / 8

int z = y = 3 ; / / y = 3 and z = 3

System.out.println (z); / / 3

System.out.println (x | = 1 ); / / 7

System.out.println (x + = 3 ); / / 10

System.out.println (x / = 2 );/ / 5

In the example, first create the variables x and y and we appropriate them values ​​6 and 4. On the next line print the bracket y, then we assign a new value to the operator * = 2 and literal. The result of the operation is 8. Further, in the example apply other compound assignment operators and earn the result of Console.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.