Skip to Main Content
Java™ Puzzlers: Traps, Pitfalls, and Corner Cases
book

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases

by Joshua Bloch, Neal Gafter
June 2005
Beginner to intermediate content levelBeginner to intermediate
312 pages
6h 24m
English
Addison-Wesley Professional
Content preview from Java™ Puzzlers: Traps, Pitfalls, and Corner Cases

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

A Common-Sense Guide to Data Structures and Algorithms, Second Edition, 2nd Edition

Jay Wengrow
Algorithms, 4th Edition

Algorithms, 4th Edition

Robert Sedgewick, Kevin Wayne

Publisher Resources

ISBN: 032133678XPurchase book