Arithmetic Operators
- Details
- Category: Programming Guides & Tutorials
- Published on Saturday, 05 January 2013 08:20
- Written by Vinayaga Moorthy
Arithmetic Operators
We're sure you're familiar with the basic arithmetic operators.
1. + addition
2. – subtraction
3. * multiplication
4. / division
These can be used in the standard way:
int x = 5 * 3;
int y = x - 4;
System.out.println("x - 4 is " + y); // Prints 11
The Remainder (%) Operator
One operator you might not be as familiar with is the remainder operator, %. The remainder operator divides the left operand by the right operand, and the result is the remainder, as the following code demonstrates:
class MathTest {
public static void main (String [] args) {
int x = 15;
int y = x % 4;
System.out.println("The result of 15 % 4 is the "
+ "remainder of 15 divided by 4. The remainder is " + y);
}
}
Running class MathTest prints the following:
The result of 15 % 4 is the remainder of 15 divided by 4. The remainder is 3
(Remember: Expressions are evaluated from left to right by default. You can change this sequence, or precedence, by adding parentheses. Also remember that the * , / , and % operators have a higher precedence than the + and - operators.)
String Concatenation Operator
The plus sign can also be used to concatenate two strings together, as we saw earlier (and as we'll definitely see again):
String animal = "Grey " + "elephant";
String concatenation gets interesting when you combine numbers with Strings. Check out the following:
String a = "String";
int b = 3;
int c = 7;
System.out.println(a + b + c);
Will the + operator act as a plus sign when adding the int variables b + c? Or will the + operator treat 3 and 7 as characters, and concatenate them individually? Will the result be String10 or String37? OK, you've had long enough to think about it. The int values were simply treated as characters and glued on to the right side of the String, giving the result:
String37
So we could read the previous code as
"Start with String a, String, and add the character 3 (the value of b) to it, to produce a new string String3, and then add the character 7 (the value of c) to that, to produce a new string String37, then print it out."
However, if you put parentheses around the two int variables, as follows,
System.out.println(a + (b + c));
you'll get this: String10
Using parentheses causes the (b + c) to evaluate first, so the rightmost + operator functions as the addition operator, given that both operands are int values. The key point here is that within the parentheses, the left-hand operand is not a String. If it
were, then the + operator would perform String concatenation. The previous code can be read as
"Add the values of b + c together, then take the sum and convert it to a String and concatenate it with the String from variable a."
The rule to remember is this:
If either operand is a String, the + operator becomes a String concatenation operator. If both operands are numbers, the + operator is the addition operator.
You'll find that sometimes you might have trouble deciding whether, say, the lefthand operator is a String or not. Look at
the following code:
System.out.println(x.foo() + 7);
You can't know how the + operator is being used until you find out what the foo() method returns! If foo() returns a String, then 7 is concatenated to the returned String. But if foo() returns a number, then the + operator is used to add 7 to the return value of foo().
Finally, you need to know that it's legal to mush together the compound additive operator (+=) and Strings, like so:
String s = "123";
s += "45";
s += 67;
System.out.println(s);
Since both times the += operator was used and the left operand was a String, both operations were concatenations, resulting in
1234567
Few examples, you might see a line such as
int b = 2;
System.out.println("" + b + 3);
which prints 23
but if the print statement changes to
System.out.println(b + 3);
then the result becomes 5
blog comments powered by Disqus
More 

