O'Reilly logo

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases by Neal Gafter, Joshua Bloch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required