Lesson 9
December 9, 2000
I recommend that you also make use of my online Java tutorial lessons, which are designed from a more conventional textbook approach. Those tutorial lessons are published at Gamelan.com.
For your convenience, I also maintain a consolidated Table of Contents on my personal web site that links to the individual lessons on the Gamelan site.
Insofar as possible, I will make use of Sun Java in these lessons. However, it will not be possible for me to go back and do a full update each time Sun releases a new version, so over the course of time, I expect to use different versions of Sun Java.
Just in case you would like to sneak a peek, the answers to the questions, and the explanations of those answers are located (in reverse order) at the end of this file.
The questions and the answers are connected by hyperlinks to make it
easy for you to navigate from the question to the answer and back.
It is recommended that you make your first pass through the questions in
the order that they appear so as to avoid inadvertently seeing the answer
to a question before you provide your own answer.
2. True or false? Integer division by zero, including a modulo (%) operation results in an ArithmeticException. No other arithmetic operation in Java can throw an exception. Instead, all other arithmetic operations proceed to a result, even though that result might be arithmetically incorrect.
3. What output is produced by the following program?
class Q84{ public static void main( String args[]){ try{ byte x = 5; byte y = x << 2; System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
4. What output is produced by the following program?
class Q85{ public static void main( String args[]){ try{ byte x = 5; byte y = (byte)(x << 2); System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
5. What output is produced by the following program?
class Q86{ public static void main( String args[]){ try{ byte x = 32; byte y = (byte)(x << 2); System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
6. What output is produced by the following program?
class Q87{ public static void main( String args[]){ try{ byte x = -32; byte y = (byte)(x >> 2); System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
7. What output is produced by the following program?
class Q88{ public static void main( String args[]){ try{ int x = -32; int y = x >>> 2; System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
8. What output is produced by the following program?
class Q89{ public static void main( String args[]){ try{ byte x = -32; byte y = (byte)(x >>> 2); System.out.println(y); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
9. What output is produced by the following program?
class Q90{ public static void main( String args[]){ try{ int x = -7; System.out.println( x/2 + " " + (x >> 1) ); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
10. What output is produced by the following program?
class Q91{ public static void main( String args[]){ try{ int x = 16384; System.out.println(x >> 33); }catch(Exception e){ System.out.println("Exception"); }//end catch }//end main() }//end class definition |
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
If the specified number of bits to be shifted exceeds this number, then the number of bit positions actually shifted is the specified number modulo the number of bits in the type.
In this case, the number of bits actually shifted is 33 modulo 32 or 1. This gives a result of 16384/2 or 8192.
int y = x >>> 2;
An unsigned right shift pulls a zero into the most-significant bit as each shift takes place, regardless of whether the original value was positive or negative.
The original value of x expressed in decimal was:
-32
The original value of x, corresponding to -32 and expressed as a hexadecimal string was:
ffffffe0
After the shift, the value of y expressed as a hexadecimal string was:
3ffffff8
When expressed as a decimal value, this is:
1073741816
In comparison, if this were a signed right shift, the four values in the same order as above would be:
-32
ffffffe0
fffffff8
-8
Thus, an unsigned right shift on a negative value does not preserve the sign and does not result in values that are easily explained in terms of simple division by a factor of two. Generally speaking, the unsigned right-shift operator should not be thought of in terms of arithmetic division. Rather, it has other purposes.
Each time an integer value is shifted one bit to the left, its value is multiplied by a factor of two, provided that no data is lost in the process.
Therefore, a two-bit left shift multiplies the operand by a factor of 4, provided no data is lost in the process. In this case, no data was lost, and the value assigned to y was equal to the value stored in x multiplied by four, giving a result of 20.
In this case, the shifted result must be explicitly cast back to type byte before being assigned to a byte variable. Of course, the use of a cast operator following a left shift can sometimes cause a loss of data in the higher-order bits that are discarded in the conversion.
Under JDK 1.3, the compiler error reads as follows:
Q84.java:8:
possible loss of precision
found
: int
required:
byte
byte y = x << 2;
Other compilers may provide a different error message that means the same thing.
For example, the largest positive value that can be stored in type int, as indicated by the constant named Integer.MAX_VALUE, is 2147483647. The following statement is intended to produce a result that is twice the maximum value.
System.out.println(
2*Integer.MAX_VALUE);
However, it produces the output value -2 instead. This indicates serious arithmetic overflow problems.
It is very important to understand that this arithmetic error does not throw an exception. Rather, it simply produces an incorrect result.
One way to avoid this particular problem is to force the arithmetic to be performed as type long as shown below:
System.out.println(
(long)2*Integer.MAX_VALUE);
This statement produces the output 4294967294 as expected.
If you do this, however, you must be careful not to cast the result back to type int, or you will simply reintroduce the same problem as before.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-