Friday, February 20, 2009

Arithmetic Operators

Arithmatic Operators

Four types of Arithmatic operators

+ addition
– subtraction
* multiplication
/ division

These can be used in the following way:
int x = 2 * 3;
int y = x - 4;
System.out.println("x - 4 is " + y); // Prints 2

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 Test {
public static void main (String [] args) {
int x = 17;
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 1

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
String concatenation gets interesting when you combine numbers with Strings.
String x = "String";
int y = 6;
int z = 4;
System.out.println(x + y + z);
Will the + operator act as a plus sign when adding the int variables y + z? Or will
the + operator treat 3 and 7 as characters, and concatenate them individually? Will
the result be String10 or String64? 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:

String64

So we could read the previous code snippet as
"Start with String a, String, and add the character 3 (the value of y) to it,
to produce a new string String6, and then add the character 4 (the value of z)
to that, to produce a new string String64, then print it."

Remember:
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. On the exam, don't expect it to always be obvious.
(Actually, now that we think about it, don't expect it ever to be obvious.) Look at
the following code:
System.out.println(x.foo() + 9);
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 9 is concatenated to the returned
String. But if foo() returns a number, then the + operator is used to add 9 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 = "1234";
s += "42";
s += 68;
System.out.println(s);
Since both times the += operator was used and the left operand was a String,
both operations were concatenations, resulting in
12344268

Increment and Decrement Operators
Java has two operators that will increment or decrement a variable by exactly one.
These operators are composed of either two plus signs (++) or two minus signs (--):

++ increment (prefix and postfix)
-- decrement (prefix and postfix)

The operator is placed either before (prefix) or after (postfix) a variable to change
its value. Whether the operator comes before or after the operand can change the
outcome of an expression. Examine the following code:
class Test {
static int players = 0;
public static void main (String [] args) {
System.out.println("players : " + players++);
System.out.println("The value of players is "+ players);
System.out.println("The value of players is now "+ ++players);
}
}
Notice that in the fourth line of the program the increment operator is after the
variable players. That means we're using the postfix increment operator, which
causes players to be incremented by one but only after the value of players is used
in the expression. When we run this program, it outputs the following:

players : 0
The value of players is 1
The value of players is now 2

Notice that when the variable is written to the screen, at first it says the value is
0. Because we used the postfix increment operator, the increment doesn't happen
until after the players variable is used in the print statement. Get it? The "post"
in postfix means after. Line 5 doesn't increment players; it just outputs its value to
the screen, so the newly incremented value displayed is 1. Line 6 applies the prefix
increment operator to players, which means the increment happens before the
value of the variable is used, so the output is 2.
Expect to see questions mixing the increment and decrement operators with
other operators, as in the following example:

int x = 1; int y = 2;
if ((y == x++) | (x < ++y)) {
System.out.println("x = " + x + " y = " + y);
}

The preceding code prints: x = 2 y = 3

You can read the code as follows: "If 2 is equal to 1 OR 2 < 3"
The first expression compares x and y, and the result is false, because the
increment on x doesn't happen until after the == test is made. Next, we increment
x, so now x is 2. Then we check to see if x is less than y, but we increment y before
comparing it with x! So the second logical test is (2 < 3). The result is true, so the
print statement runs.

Look out for problems that use the increment or decrement operators on
a final variable. Because final variables can't be changed, the increment and decrement
operators can't be used with them, and any attempt to do so will result in a compiler
error. The following code won't compile:

final int x = 5;
int y = x++;
and produces the error:
Test.java:4: cannot assign a value to final variable x
int y = x++;

Useful Links :-

Assignment Operators
Logical operators
Relational Operators
Conditional Operators
Bitwise Operator Intro
Bitwise Logical operator
Bitwise Left shift Operator
Bitwise Right shift Operator

Sponsored Links :-

Get Your SCJP preparation Kit free!!!!!!!
Get Your SCWCD preparation Kit free!!!
Are you Intelligent check here

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP