Java Programming/Arithmetic expressions

In order to do arithmetic in Java, one must first declare at least one variable. Typically one declares a variable and assigns it a value before any arithmetic is done. Here's an example of declaring an integer variable:

After creating a variable, one can manipulate its value by using Java's operators:  (addition),   (subtraction),   (multiplication),   (integer division),   (modulo or remainder),   (pre- & postincrement by one),   (pre- & postdecrement by one).

The division operator rounds towards zero:  is 2, and   is -2. The remainder operator has the same sign as the left operand; it is defined such that  is always equal to a. The preincrement, predecrement, postincrement, and postdecrement operators are special: they also change the value of the variable, by adding or subtracting one. The only difference is that preincrement/decrement returns the new value of the variable; postincrement returns the original value of the variable.

Question 3.8: Consider the following code:

What will be printed in the standard output?

=> 10  => 20   => 40   => 21   => 7

When using several operators in the same expression, one must consider Java's order of precedence. Java uses the standard PEMDAS (Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction) order. When there are multiple instances of the same precedence, Java reads from left to right. Consider what the output of the following code would be:

The following chart shows how Java would compute this expression:

Besides performing mathematical functions, there are also operators to assign numbers to variables (each example again uses the variable initialized as ):

Using bitwise operators within Java
Java has besides arithmetic operators a set of bit operators to manipulate the bits in a number, and a set of logical operators. The bitwise logical operators are

Besides these logical bitwise functions, there are also operators to assign numbers to variables :

The shift operators are used to shift the bits to the left or right, which is also a quick way to multiply/divide by two: