《Java 解惑》笔记

(This page is moved and no longer to maintain.)

这本书的原名是:Java Puzzlers: Traps, Pitfalls and Corner Cases

豆瓣链接为:http://www.douban.com/subject/1882469/?i=1

在线版本阅读:中文版PDF

这里的所有代码都上传到 Google Code 上去了,链接在这里(trunk/src/javapuzzlers)。

现在有新版了,不过我还是就这个版本用着吧。书在2006年10月就买了,不过只看了一部分,还是小部分……

另:这本书在网上有不少地方可下载,中文版本已找到,等我这里记录完了就去找英文版。它针对每一个问题配的插图还不错,插图可以放到 flickr 上, 然后在这里使用.

花絮:刚才整理 Google Docs 时,居然发现2006年记录的 Java Puzzlers 笔记——空的。

Chapter 02. Expressive Puzzlers

Puzzle 01: Oddity, 求余运算中含有负数带来的问题

Puzzle 02: Time for a Change, Java无法精确表达浮点数

Puzzle 03: Long Division, 未用long型常数, 导致int数值溢出

Puzzle 04: It's Elementary, 字母l和数字1用错引起的迷思

Puzzle 05: The Joy of Hex, 八进制和十六进制表示的数是该数补码的直接转写, 因此用它们表示负数时, 可以不用添加负号

Puzzle 06: Multicast, 多重转型的问题, 除了涉及窄化和宽化外, 还涉及宽窄化转型

Puzzle 07: Swap Meat, 在赋值运算中, 表达式左边的变量在参与运算之前会被提取出来, 即使是连等式

Puzzle 08: Dos Equis, 条件运算符中混合运算的转型问题

Puzzle 09: Tweedledum, 窄化转型导致的复合赋值运算错误

Puzzle 10: Tweedledee, 在使用复合赋值符时涉及 String 类型的问题

Chapter 03. Puzzlers with Character

这一章主要是关于字符串, 字符和其他文本数据的问题.

Puzzle 11: The Last Laugh, 字符型在使用+号时候, 作了整数相加的运算

Puzzle 12: ABC, char[] 没有重写toString()方法

Puzzle 13: Animal Farm, 加号+和双等号==的优先级不同导致的问题

Puzzle 14: Escape Rout, 在源代码中,所有字符都可以用其Unicode转义符号代替

Puzzle 15: Hello Whirled, Windows斜杠可能导致Unicode转义字符解析错误,应该避免使用

Puzzle 16: Line Printer, 不可见字符的Unicode转义符可能导致的错误

Puzzle 17: Huh?, 全部由Unicode转义符构成的源码

Puzzle 18: String Cheese, 构造函数 java.lang.String(bytes[]), 是一个本地字符集的函数,而本地字符集在不同的OS上,可能是不一样的,因此,这个方法可能带来意想不到的问题

Puzzle 19: Classy Fire, 用/**/注释的时候, 要注意中间的代码是否含有*/符号, 以免误伤. // 的注释方式目前比较流行.

Puzzle 20: What's My Class?, String.replaceAll(String, String) 的参数是按正则表达式来处理的.

Puzzle 21: What's My Class, Take 2, 即使是 JDK API 的函数,对函数使用也不要轻易对其名称望文生义,一定要查文档,甚至看源码!

Puzzle 22: Dupe of URL, 写注释要仔细,不用的代码要删除。

Puzzle 23: No Pain, No Gain, 注意默认的构造方法。

Chapter 04. Loopy Puzzlers

本章涉及各种各样的由循环引出的问题,包括但不限于循环问题本身

Puzzle 24: A Big Delight in Every Byte, 尽量避免不同类型的数值混合运算,避免出现非预期的 cast

Puzzle 25: Inclement Increment, 後++操作的一个机关。不要在同一个语句中对同一个变量多次赋值

Puzzle 26: In the Loop, 溢出问题

Puzzle 27: Shifty i's

Puzzle 28: Looper

Puzzle 29: Bride of Looper

Puzzle 31: Ghost of Looper

Puzzle 33: Looper Meets the Wolfman

Puzzle 34: Down for the Count

Puzzle 35: Minute by Minute

Chapter 05. Exceptional Puzzlers

本章将是各种各样因 Exception 引出的问题。

Puzzle 36: Indecision

Puzzle 37: Exceptionally Arcane

Puzzle 38: The Unwelcome Guest

Puzzle 39: Hello, Goodbye

Puzzle 40: The Reluctant Constructor, 实例初始化操作先于类构造器运行

Puzzle 41: Field and Stream

Puzzle 42: Thrown for a Loop

Puzzle 43: Exceptionally Unsafe

Puzzle 44: Cutting Class

Puzzle 45: Exhausting Workout

Chapter 06. Classy Puzzlers

Puzzle 46: The Case of the Confusing Constructor

Puzzle 47: Well, Dog My Cats!

Puzzle 48: All I Get Is Static

Puzzle 49: Larger Than Life

Puzzle 50: Not Your Type

Puzzle 51: What's the Point?

Puzzle 52: Sum Fun

Puzzle 53: Do Your Thing

Puzzle 54: Null and Void

Puzzle 55: Creationsim

Chapter 07. Library Puzzlers

Puzzle 56: Big Problem

Puzzle 57: What's in a Name?

Puzzle 58: Making a Hash of It

Puzzle 59: What's the Difference?

Puzzle 60: One-Liners

Puzzle 61: The Dating Game

Puzzle 62: The Name Game

Puzzle 63: More of the Same

Puzzle 64: The Mod Squad

Puzzle 65: A Strange Saga of a Suspicious Sort

Chapter 08. Classier Puzzlers

Puzzle 66: A Private Matter

Puzzle 67: All Strung Out

Puzzle 68: Shades of Gray

Puzzle 69: Fade to Black

Puzzle 70: Package Deal

Puzzle 71: Import Duty

Puzzle 72: Final Jeopardy

Puzzle 73: Your Privates Are Showing

Puzzle 74: Identity Crsis

Puzzle 75: Heads or Tails?

Chapter 09. More Library Puzzlers

Puzzle 76: Ping Pong

Puzzle 77: The Lock Mess Monster

Puzzle 78: Reflection Infection

Puzzle 79: It's a Dog's Life

Puzzle 80: Further Reflection

Puzzle 81: Charred Beyond Recognition

Puzzle 82: Beer Blast

Puzzle 83: Dyslexic Monotheism

Puzzle 84: Rudely Interrupted

Puzzle 85: Lazy Initialization

Chapter 10. Advanced Puzzlers

Puzzle 86: Poison-Paren Litter

Puzzle 87: Strained Relations

Puzzle 88: Raw Deal

Puzzle 89: Generic Drugs

Puzzle 90: It's Absurd, It's a Pain, It's Superclass!

Puzzle 91: Serial Killer

Puzzle 92: Twisted Pair

Puzzle 93: Class Warfare

Puzzle 94: Lost in the Shuffle

Puzzle 95: Just Desserts