4. Loopy Puzzlers
All the puzzles in this chapter concern loops.
Puzzle 24: A Big Delight in Every Byte
This program loops through the byte
values, looking for a certain value. What does the program print?
public class BigDelight { public static void main(String[ ] args) { for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) { if (b == 0x90) System.out.print("Joy!"); } }}
Solution 24: A Big Delight in Every Byte
The loop iterates over all the byte
values except Byte.MAX_VALUE
, looking for 0x90
. This value fits in a byte and is not equal to Byte.MAX_VALUE
, so you might think that the loop would hit it once and print Joy!
on that iteration. Looks can be deceiving. If you ran the program, ...
Get Java™ Puzzlers: Traps, Pitfalls, and Corner Cases now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.