| NOTE
|
1. all java methods must be contained in a class, and all program
staements must be placed inside a method. 2. typically, the class that contains the main methods does not contains many additional mathods. 3. the words class, public, static, void , and main are reserved words, also called keywords. 4. the keyword public signal that the class or method is usable outside of the class, whereas private data members or methods are not. 5. the key word static is used for methods that will not access any objects of a class, such as the mothods in the firstprog class, such as the methods in a source file that contains no insurance variables. most methods in java do operate on objects and are not statics. the main method, however, must always be static. 6. the program shown on the previous page is a java application. this is not to confused with a java applet,a program that runs inside a web browser or applet viewer. applets are not part of the AP sybset. |
| OPERATOR |
MEANING |
EXAMPLE |
| + |
addition |
3+ x |
| - |
subtraction |
p - q |
| * |
multiplication |
6 * i |
| / | division |
10/4 //returns 2, not 2.5! |
| % |
mod(remainder) |
11% 8 //returns 3 |
| OPERATOR |
MEANING |
EXAMPLE |
| == |
equal
to |
if
(x == 100) |
| ! = |
not
equal to |
if
(age ! = 21) |
| > |
greater
than |
if
(salary > 30000) |
| < |
less
than |
if
(grade < 65) |
| >= |
greater
then or equal to |
if
(age >= 16) |
| <= |
less
then or equal |
if
(height <=6) |
|
COMPATING FLOATING-POINT NUMBERS since floating-point numbers are manipulated and stored with a fixed number of significant digits, arithmetic operations generally produce result that must be rounded. this causes round-of error. one concequence is that you cant rely on using the = = or ! = operators to compare two double values for equality. they may differ in their last significant digit or two becuase of round-off error. instead you should test that the magnitud of the diference between the numbers is less than some number about the size of the machine precision. the machine precision is usually denoted e, and is typically about 10^-16 for double precision. so you would like to test something like this [x-y]<e. but this is not good if x and y are very large. these nukbers are essentially equal to machine precision. since they only differ in the 16th signifiant digit. but [x-y] = 10^-6 , not only 10^-16. here you should chack the relative difference: [x-y] / min ([x], [y])<e but this test wilol fail for very small numbers, in particular if one of the numbers is zero. so the best general strategy is to test the relative difference unless the numbers are small, in which case you test the absolute difference, whew! |
| OPERATOR |
MEANING |
EXAMPLE |
| ! |
NOT |
if ( ! found) |
| && |
AND |
if (x < 3 && y >4) |
| I
I |
OR |
if (age < 2 II height <4) |
| && |
T |
F |
II |
T |
F |
! |
|
| T |
T |
F |
T |
T |
T |
T |
F |
| F |
F |
F |
F |
T |
F |
F |
T |
| OPERATOR |
EXAMPLE |
MEANING |
| = |
x =2 |
simple assigment |
| + = |
x + = 4 |
x = x + 4 |
| - = |
y - = 6 |
y = y - 6 |
| *= |
p * = 5 |
p = p * 5 |
| / = |
n / = 10 |
n = n / 10 |
| % = |
n % = 10 |
n = n % 10 |
| OPERATOR |
EXAMPLE |
MEANING |
| + + |
i + + or + + i |
i is incremented by 1 |
| -- |
k -- or -- k |
k is decremented by 1 |
| ESCAPE
SEQUENCE |
MEANING |
| \n |
newline |
| \" |
double quote |
| \\ |
backslash |
| EXCEPTION |
DISCUSSED IN
PAGE |
| arithmeticexception |
15 |
| nullpointerexception |
41 |
| classcastexception |
79 |
| arrayindexoutofboundexception |
170 |
| indexoutoffboundexception | 180 |
| nosuchelementexception |
250, 252, 375 |
| illegalstateexception |
252, 255 |