Saturday, May 7, 2011

JAVA OPERATORS

Operators are symbols in a language that stand for built-in functions. Java has a great many operators and,
since this chapter will only serve to introduce the Java language, we will not discuss them all. Here are the
operators we will discuss:
By assignment, we mean taking the value on the right side of the equal sign (sometimes called RS) and giving
that value to the variable on the left side of the equal sign (sometimes called LS). If the value of k is 5, after the
following statement is executed, the value of c will also be 5. Likewise, the value of k will remain 5 after execution:
c = k;
The arithmetic operators perform their expected functions. For example, if the value of a is 3, and the value
of b is 5, after the following statement executes the value of x will be 15:
x = a * b;
In contrast to some other languages, Java has no operator for exponentiation. If a programmer wants to raise
a number to some power, the program must perform the exponentiation either by repeatedly multiplying, or by
using the pow() method of the Math class which is part of the built-in Java library.
The ++ and -- operators are convenience operators that many programmers like because they save some
typing. The following two statements have the same effect:
m = m + 1;
m++;
In either case, the value of m will increase by 1. The -- operator works similarly, except that the value of
the variable will decrease by 1.
The rest of the operators in our list are logical operators. We use logical operators to test the truth of conditions
important to our programs. For example, if our program were going to find all the houses in a neighborhood
whose assessed values were greater than $200,000, our program might have a statement like this in it:
if ( assessedValue > 200000 ) {
We will discuss the use of if statements in the section on Java control structures, so for now simply
observe that the “>” operator allows one to test the truth of the condition that assessed value is greater than
$200,000. If the value of the variable assessedValue at the time the statement executes is greater that
200000, the logical operator “>” will return a value of true. Otherwise, it will return false.
We will discuss the other logical operators in the section on Java control structures. Remember, too, that
we have not discussed all Java operators. Java also has operators for testing and shifting bit values, conditional
statement execution, modulo arithmetic, and some other functions.

No comments:

Post a Comment