Java Puzzlers' Greatest Hits: Joshua Bloch Neal Gafter
Java Puzzlers' Greatest Hits: Joshua Bloch Neal Gafter
Greatest Hits
Joshua Bloch
Neal Gafter
class Delight {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE;
b < Byte.MAX_VALUE; b++) {
if (b == 0x90)
System.out.print("Joy! ");
}
}
}
class Delight {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE;
b < Byte.MAX_VALUE; b++) {
if (b == 0x90)
System.out.print("Joy! ");
}
}
}
(a) Joy!
(b) Joy! Joy!
(c) Nothing
(d) None of the above
(a) Joy!
(b) Joy! Joy!
(c) Nothing
(d) None of the above
Program compares a byte with an int;
byte is promoted with surprising results
class Delight {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE;
b < Byte.MAX_VALUE; b++) {
if (b == 0x90) // (b == 144)
System.out.print("Joy! ");
}
}
}
// (byte)0x90 == -112
// (byte)0x90 != 0x90
(a) 0
(b) 1
(c) 2
(d) It varies
(a) 0
(b) 1
(c) 2
(d) It varies
No programmer-defined constructor.
(IdentityHashMap is a red herring.)
(a) true
(b) false
(c) It varies
(d) None of the above: it deadlocks
(a) 5
(b) 1000
(c) 5000
(d) Throws an exception
43 Java Puzzlers’ Greatest Hits
What Does It Print?
(a) 5
(b) 1000
(c) 5000
(d) Throws an exception
(a) -22430
(b) 17777
(c) 66666
(d) None of the above
(a) -22430
(b) 17777
(c) 66666
(d) None of the above
12345
System.out.println( l);
+ 5432
}
}
• 95 Puzzles
• 52 Illusions
• Tons of fun
Greatest Hits
Joshua Bloch
Neal Gafter