Arithmetic Assignment Operators Assignment Help

Assignment Help: >> Elements of Java - Arithmetic Assignment Operators

Arithmetic Assignment Operators

Java gives special operators which could be used to merge an arithmetic operation along with an assignment. As you probably know, statements such as the following are quite general in programming.

a = a + 4;

Within java you can rewrite these statements as display here:

a + = 4;

This version uses the + = assignment operator. Both statements perform the similar action they increase the value of a through 4.

Here is another instance, a = a % 2;

that can be expressed as

a % = 2;

Within this case, the % = gets the remainder of a/2 and puts that result back into a. There are assignment operators for all the arithmetic, binary operation. Therefore, any statement of the forms. The assignment operators give two advantages.  First one is, they save you a bit of typing, since they are "shorthand" for their equivalent long forms.  Second is, they are implemented more efficiently through the Java run time system than are their equivalent long forms.  By these reasons, you will frequently see the assignment operators used in professionally written Java programs.

Those operators are unique in that they can appear both in postfix form, where they follow the operand as just shown, and prefix form, where they precede the operand. In the further examples there is no difference among the prefix form and postfix forms.

Within postfix form, the previous value is gained for use in the expression and then the operand is modified. For instance:

x = 42;

y = + + x;

In that case, y is set to 43 as you would expect, since the increment occurs before x is assigned to y. Therefore, the line y = + + x; is the equivalent of these two statement.

x = x + 1;

y = x;

Therefore, when written such as this,

x=42;

y=x++;

The value of x is obtained before the increment operator is executed, so the value of y is 42. Obviously, in both cases x is set to 43. Here, the line y=x++; is the equal of these two statements:

y=x;

x=x+1;

The given program describes the increment operator.

// Demonstrate ++.

class IncDec {

public static void main(String a[ ]);

int a=1;

int b=2;

int c,d;

c = ++b;

d = a++;

c++;

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

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

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

System.out.println("d = " +d);

}

}

The output of this program follows

a=2

b=3

c =4

d=1

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd