Expressions and Arithmetic Operators

Read this chapter, which discusses arithmetic operations in greater detail along with solving expressions with mixed data types.

4. Arithmetic Operators


Answer:

No. For example, the following is also an expression:
    "This is" + " a string" + " expression"
The above expression creates a new string that is the concatenation of all the strings.

Arithmetic Operators

Arithmetic expressions are especially important. Java has many operators for arithmetic. These operators can be used on floating point numbers and on integer numbers. (However, the % operator is rarely used on floating point.)

For instance, / means integer division if both operands are integers, and means floating point division if one or both operands are floating point.

Arimetic rules

For example, with 16 bit short variables, the arithmetic is done using 32 bits:

short x = 12; // 16 bit short
int result; // 32 bit int

result = x / 3; // arithmetic will be
result = x / 3; // arithmetic will be
// done using 32 bits

The expression   x / 3   divides a 32-bit value 12 by a 32-bit value 3 and puts the 32-bit answer in result. The literal 3 automatically represents a 32-bit value.

Operator   
Meaning
precedence   
-
unary minus
highest
+
unary plus
highest
*
multiplication
middle
/
division
middle
%
remainder
middle
+
addition
low
-
subtraction
low

Question 4:

Look at the example above. Does it really matter that 12 was converted to 32 bits?