download

Matakuliah
Tahun
Versi
: M0074/PROGRAMMING II
: 2005
: 1/0
MATERI PENDUKUNG
OPERATOR
1
•
•
Operators
Operators allow you to access, manipulate, relate, or refer to Java language
elements, from variables to classes. Operators have properties of
precedence and associativity. When several operators act on the same
element (or operand), the operators' precedence determines which operator
will act first. When more than one operator has the same precedence, the
rules of associativity apply. These rules are generally mathematical; for
instance, operators will usually be used from left to right, and operator
expressions inside parentheses will be evaluated before operator
expressions outside parentheses. Operators generally fall into six
categories: assignment, arithmetic, logical, comparison, bitwise, and
ternary. Assignment means storing the value to the right of the = inside the
variable to the left of it. You can either assign a value to a variable when you
declare it or after you have declared it. The machine doesn't care; you
decide which way makes sense in your program and your practice:
double bankBalance;
//Declaration
bankBalance = 100.35;
//Assignment
double bankBalance = 100.35; //Declaration with assignment
2
•
In both cases, the value of 100.35 is stored inside the memory reserved by
the declaration of the bankBalance variable. Assignment operators allow
you to assign values to variables. They also allow you to perform an
operation on an expression and then assign the new value to the right-hand
operand, using a single combined expression. Arithmetic operators perform
mathematical calculations on both integer and floating-point values. The
usual mathematical signs apply: + adds, - subtracts, * multiplies, and /
divides two numbers. Logical, or Boolean, operators allow the programmer
to group boolean expressions in a useful way, telling the program exactly
how to determine a specific condition. Comparison operators evaluate
single expressions against other parts of the code. More complex
comparisons (like string comparisons) are done programmatically. Bitwise
operators act on the individual 0s and 1s of binary digits. Java's bitwise
operators can preserve the sign of the original number; not all languages
do. The ternary operator, ?:, provides a shorthand way of writing a very
simple if-then-else statement. The first expression is evaluated; if it's true,
the second expression is evaluated; if the second expression is false, the
third expression is used.
3