<< Chapter < Page | Chapter >> Page > |
Listing 5 . Listing for Answer 36. |
---|
/*File SampProg07.java from module 22
Copyright 1997, R.G.BaldwinWithout reviewing the following solution, write a Java
application that clearly illustrates the difference betweenthe prefix and the postfix versions of the increment
operator.Provide a termination message that displays your name.
*********************************************************/class SampProg07{
static public void main(String[]args){
int x = 3;int y = 3;
int z = 10;System.out.println("Prefix version gives "
+ (z + ++x));System.out.println("Postfix version gives "
+ (z + y++));System.out.println("Terminating, Dick Baldwin");
}//end main}//end class SampProg07 |
Java supports the following list of shortcut assignment operators. These operators allow you to perform an assignment and another operation with asingle operator.
+=
-=*=
/=%=&=
|=^=<<=>>=>>>=
For example, the two statements that follow perform the same operation.
The behavior of each of the shortcut assignment operators follows this same pattern.
True.
The assignment operator is a binary operator.
True.
The answer for the bitwise exclusive or is:
The answer for the bitwise inclusive or is:
The answer for the bitwise and is:
True: Bits shifted off the right end are lost.
False: In Java, the signed right shift operation populates the vacated bits with the sign bit, while the left shift and the unsigned right shift populate the vacated bits with zeros.
The following table shows the seven bitwise operators supported by Java.
Operator Typical Use Operation>>OpLeft>>Dist Shift bits of OpLeft right by
Dist bits (signed)<<OpLeft<<Dist Shift bits of OpLeft left by
Dist bits>>>OpLeft>>>Dist Shift bits of OpLeft right
by Dist bits (unsigned)&OpLeft&OpRight Bitwise and of the two
operands| OpLeft | OpRight Bitwise
The bitwise and operator and the bitwise inclusive or operator are shown below.
&bitwise and
| bitwise inclusive or
An important characteristic of the behavior of the logical and operator and the logical or operator in Java is that the expressions containing them are evaluated from left to right. The evaluation of the expression is. terminated as soon as the result of evaluatingthe expression can be determined. For example, in the expression given in Question 24 , if the variable a is less than the variable b , there is no need to evaluate the right operand of the || operator to determine the value of the entire expression. Therefore, evaluation will terminate as soon as it isdetermined that a is less than b .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?