Arithmetic unary (+  -) and binary operators:  +   -   *   /   %

The best way to understand operators is to see them in action. Here is our demo application code (with results captured in comments) that explains the unary operators + and -:

public class Ch09DemoApp {
public static void main(String[] args) {
int i = 2; //unary "+" is assumed by default
int x = -i; //unary "-" makes positive become negative
System.out.println(x); //prints: -2
int y = -x; //unary "-" makes negative become positive
System.out.println(y); //prints: 2
}
}

And the following code demonstrates the binary operators  +-, *, /, and %:

int z = x + y;              //binary "+" means "add"
System.out.println(z); //prints: 0

z = x - y; //binary "-" means "subtract"
System.out.println(z); //prints: -4
System.out.println(y - x); //prints: 4

z = x * y;
System.out.println(z); //prints: -4

z = x / y;
System.out.println(z); //prints: -1

z = x * y;
System.out.println(z % 3); //prints: -1
System.out.println(z % 2); //prints: 0
System.out.println(z % 4); //prints: 0

As you have probably guessed, the % operator (called modulus) divides the left-hand operand by the right-hand operand and returns the remainder.

Everything looks logical and as expected. But, then we try to divide one integer by another integer with the remainder and do not get what was expected:

int i1 = 11;
int i2 = 3;
System.out.println(i1 / i2); //prints: 3 instead of 3.66...
System.out.println(i1 % i2); //prints remainder: 2

The result, i1/i2, should be bigger than 3. It has to be 3.66... or something similar. The problem is caused by the fact that all the numbers involved in the operation are integers. In such a case, Java assumes that the result should also be expressed as an integer and drops (without rounding) the fractional part.

Now, let's declare one of the operands as the double type, with the same value of 11, and try the division again:

double d1 = 11;
System.out.println(d1/i2); //prints: 3.6666666666666665

This time, we got what was expected, and there are other ways to achieve the same result:

System.out.println((float)i1 / i2);  //prints: 3.6666667
System.out.println(i1 / (double)i2); //prints: 3.6666666666666665
System.out.println(i1 * 1.0 / i2); //prints: 3.6666666666666665
System.out.println(i1 * 1f / i2); //prints: 3.6666667
System.out.println(i1 * 1d / i2); //prints: 3.6666666666666665

As you can see, you can cast any of the operands to the float or double types (depending on the precision you need), or you can include the float or double type number. You might remember from Chapter 5, Java Language Elements and Types, that a value with a fractional part is double by default. Or, you can explicitly select a type of the value added, as we did in the last two lines of the preceding code.

Whatever you do, just be careful while dividing two integers. If you do not want the fractional part to be dropped, cast at least one operand to float or double just in case (more about the cast operator in the Cast operator: ( target type ) section later. Then, if you need, you can round up the result to any precision you prefer or cast it back to int:

int i1 = 11;
int i2 = 3;
float r = (float)i1 / i2;
System.out.println(r); //prints: 3.6666667
float f = Math.round(r * 100f) / 100f;
System.out.println(f); //prints: 3.67
int i3 = (int)f;
System.out.println(i3); //prints: 3
Java integer division: if in doubt, make one of the operands double or float, or simply add a 1.0 multiplier to one of them.

In case of String, the binary operator  + means concatenate and the operator is often called the concatenation operator:

String s1 = "Nick";
String s2 = "Samoylov";
System.out.println(s1 + " " + s2); //prints: Nick Samoylov
String s3 = s1 + " " + s2;
System.out.println(s3); //prints: Nick Samoylov

And just as a reminder, in Chapter 5, Java Language Elements and Types, we demonstrated that an arithmetic operation applied to the primitive type char uses the character's code point – a numeric value of the character:

char c1 = 'a';
char c2 = '$';

System.out.println(c1 + c2); //prints: 133
System.out.println(c1/c2); //prints: 2
System.out.println((float)c1/c2); //prints: 2.6944444

These results make sense only if you remember that the code point of the symbol a is 97, while the code point of the symbol $ is 36.

Most of the time, the arithmetic operations in Java are quite intuitive and cause no confusion, except in two cases: