Previous | Home | Next |
Keywords are predefined identifiers reserved by Java for a specific purpose and are used only in a limited, specified manner. Java has a richer set of keywords than C or C++. The following keywords are reserved for Java:
abstract | double | int | super |
boolean | else | interface | switch |
break | extend | long | synchronized |
byte | false | native | this |
byvalue | final | new | threadsafe |
case | finally | null | throw |
catch | float | package | transient |
char | for | private | true |
class | goto | protected | try |
const | if | public | void |
continue | implements | return | while |
default | import | short |
Java has the full complement of relational operators. For testing the equality you use a double equal sign ==. For example, the value of 5 == 6, is false.
We use a != for inequality. For e.g. the value of the following evaluates to true 3 != 7 is true.
Next we have have the <(less than), > (greater than), <= (less than or equal), and >=(greater than or equal) operators.
Java also uses && for the logical AND operator and || for the logical OR operator. As you can easily remember from the != operator, the exclamation point ! is the logical negation operator. The && and || operators are evaluated in short circuit fashion. This means that when we have an expression like:
p && q
If the truth value of the expression p has been determined to false, the value for the expression q is not calculated. For example consider the following expression
int p=7; int q=1; if((p==6)&&(q==1)) { p=p+1; q=q-1; }
In the above e.g. the first condition p==6 is evaluated to false, so second condition q==1 is never evaluated. Similarly, if p evaluates to true, then the value of p || q is automatically true, without evaluating q.
Java also supports the ternary ?: operator that is occasionally useful. The expression condition ? p : q evaluates to p if the condition is true, else it evaluates to q.
Example:
p > q ? p : q
The above expression gives the value p if p is greater than q or else it will give q.
Previous | Home | Next |