Arithmetic, String, and Other Operators

6.7 Arithmetic, String, and Other Operators

THE OPERATORS

[+] [-] [*] [/] [%] [=] [++] [--] [+=] [-=] [*=] [/=]

DESCRIPTION

Unless noted otherwise, the operators return a value of the same type as their arguments. In cases of binaries operators with operands of different types, lesser types are promoted to higher types. For example, if you add an int to a float, you will get a float. If you add a float to a double you will get a double.

The format used in the descriptions below is:

English Name: syntax
Explanation

plus: value_1 + value_2
The plus operator returns the sum of value_1 and value_2 if they are both numerical values. If either of the operands is a String, all operands are converted to Strings and are then concatenated together. So, 3.4 + 5;
returns 8.4 and "One is "+1+" and all alone";
returns One is 1 and all alone.
minus: value_1 - value_2
The minus operator returns the differece of value_1 and value_2.
multiplication: value_1 * value_2
The multiplication operator returns the product of value_1 and value_2.
division: value_1 / value_2
The division operator returns the quotient of value_1 and value_2. If both value_1 and value_2 are integers, the result can be written mathematically as sgn(value_1)*floor(abs(value_1)/value_2) where sgn(x) is 1 if x>=0 and -1 otherwise and floor(x) returns the largest integer smaller than x. So floor(4.3) is 4, and floor(0.6) is 0.
modulo: value_1 % value_2
The modulo operator returns the remainder of value_1 % value_2. This operator is useful only if value_1 and value_2 are both integers, in which case it is equvalent to value_1-(value_1/value_2)*value_1;.
assignment: variable = value
The assignment operator stores the value value in the variable variable. If variable has a class as its type, value must be an object of type class. If variable is one of the primitive data types, then promotion is allowed as described above.
increment: variable ++ OR ++ variable
The increment operator adds one to variable. This is commonly used in the control statements of loops. The two forms differ in when the increment occurs - either before or after the value of variable is substituted in an expression.. The first form increments after, whereas the second before. So
      int x=3;
      int y=x++;
      System.out.println("x is "+x+". y is "+y+"."); 
would output x is 4. y is 3. whereas
      int x=3;
      int y=++x;
      System.out.println("x is "+x+". y is "+y+"."); 
would output x is 4. y is 4..
decrement: variable -- OR -- variable
The decrement operator subtracts one from variable. The two forms work the same way as the increment operator.
plus assignment: variable += value
The plus assigment operator is a short form for variable = variable + value.
minus assignment: variable -= value
The minus assigment operator is a short form for variable = variable - value.
multiplication assignment: variable -= value
The multiplication assigment operator is a short form for variable = variable * value.
division assignment: variable /= value
The minus assigment operator is a short form for variable = variable / value.
Next 6.8 Boolean Operators

David Maxwell, who is still writing this, would like to hear your comments and suggestions.