{ Java Puzzlers } 一本有意思的JAVA错误集锦

Java Puzzlers 是一本讲述Java编程中出现的有意思的错误的书,我读的英文版的,当时觉得很好玩,也照着书里面,用eclipse敲完了所有的例子,例子就上传到我的资源里了,希望以后有空的时候可以回头来看看。

例子资源链接:http://download.csdn.net/download/u011680118/10227819


1. Lexical Issues

1.1 the letter l looks like the digit 1 in many fonts.

1.2 negative hex literals appear positive, 0xff

1.3 octal literals look like decimal literals, 012 is 10

1.4 unicode escapes for ASCII characters are confusing, \username

1.5 blackslashes must be escaped, even in comments, \\

1.6 block comments do not nest, use single-line comments! //


2. Integer Arithmetic

2.1 non-zero result of % has sign of left operand, -1 % 2 = -1

2.2 integer arithmetic overflows silently, use long rather than int.

2.3 the sign of the difference of int values does not realiably indicate their order. It may be greater than MAX_VALUE.

2.4 compound assignment operators can cause silent narrowing cast,for byte, short and char.

2.5 integral types are asymmetric: MIN_VALUE is its own negation.

2.6 shift operators use only the low-order bits of their right operand. 5 lower bits for int, and 6 for long.

2.7 when convering between integral types, sign extension is performed if the source type is signed. Use 0xff & 


3. Floating-point Arithmetic

3.1 floating-point is not exact, BigDecimal is more accurate, avoid floating loops and ++/-- operands.

3.2 NaN is not equal to any floating-point value, including itself.

3.3 Conversions from int/long to float, long to double are lossy.

3.4 The BigDecimal(double) returns exact value of its argument, use BigDecimal(string)


4. Expression Evaluation

4.1 mixed type computations are confusing, ? : 

4.2 operands of operators are evaluated left to right.

4.3 operators precedence is not always obvious, use ()

4.4 == and != performs reference comparisons on boxed primitive types

4.5 constant variables are inlined where they're used. Use function to make an expression nonconstant.

4.6 & and | evaluates both operands even when used on boolean values.


5. Flow of Control

5.1 missing break or default in switch cases causes fall-through

5.2 it's difficult to terminate an int-indexed loop at Integer.MAX_VALUE, use long.

5.3 abrupt completion of a finally block masks pending transfer of control. Don't throw exceptions from finally.

5.4 using exceptions for normal control flow leads to bugs and poor performance.


6. Class Initialization

6.1 order of class initialization is top to bottom. Pay attention to static fields and loops.

6.2 timing of NoClassDefFoundError is not reliable, use reflection instead.


7. Instance Creation and Destruction

7.1 instance initializers execute before constructor body.

7.2 invoking an overridden method from a constructor causes method to run before instance is initialized. Use lazy initialization.

7.3 failure to null out references can cause memory leaks.

7.4 failure to add a private constructor makes a class instantiable.

7.5 don't use finalizers.

7.6 don't implement Cloneable.


8. Other Class- and Instance-Related Topics

8.1 there is no dynamic dispatch on static methods. Static just rely on Class.

8.2 inner classes are confusing, prefer static member classes.

8.3 failure to make defensive copies destroy immutability.

8.4 implementing an interface affects the API of the implementing class. Don't use constant interface!

8.5 using int constants as enum values is unsafe.

8.6 mixing raw and parameterized types weakens type checking. Avoid List list 

8.7 returning null instead of a zero-length array or collection is error prone.


9. Name Reuse

9.1 it's easy to overload when you want to override

9.2 overload-resolution rules are not obvious, avoid overloaing and prefer static factories.

9.3 avoid hiding

9.4 avoid shadowing

9.5 avoid obscuring

9.6 obey the naming conventions

9.7 avoid reusing platform class names


10. Strings

10.1 arrays don't overrride object.toString, use String.valueOf, and Arrays.toString

10.2 string.replaceAll takes a regular expression as its first argument.

10.3 string.replaceAll takes a replacement string as its second argument. Use replace.

10.4 avoid string concatenation.

10.5 conversion of bytes to characters needs a charset!

10.6 values of type char are silently converted to int, not string. use String.valueOf


11. I/O

11.1 stream.close can throw IOException

11.2 printStream.write(int) doesn't flush output streams

11.3 consume the output of a process, or it may hang.


12. Threads

12.1 never class Thread.run

12.2 don't use an instance lock if you extend a library class, use a private lock object.

12.3 Thread.interrupted clears the intterrupted status

12.4 neveer wait for a background thread during class initialization.

12.5 synchronize access to shared mutable state.

12.6 never cede control to an alien method from a synchronized method or block.

12.7 invoking wait outside of a while loop causes unpredicable behaviour.

12.8 depending on the thread scheduler may result in erratic and platform-dependent behavior


13. Reflection

13.1 use reflection to instantiate classes, interfaces to access instances.

13.2 don't use reflection on inner classes.

13.3 use java.lang.reflect.constructor.newInstance rather than Class.newInstance which may throw unchecked exceptions.


14. Serialization

14.1 think twice before making a class serializable, accepting default readObject method. Write readObject method defensively.

14.2 design the serialized forms carefully.

14.3 using the default serialized form leaks private fields into a class's public API.

14.4 using the default serialized form can cause poor performance.

14.5 always write a readResolve for instance-control classes.

14.6 declare an explicit serial version UID in seriablizable classes.

14.7 if readObject or readResolve invokes overridable methods, deserializing cyclic object graphs can cause corruption.


15. Other Libraries

15.1 override equals and hashcode together.

15.2 Calendar and Date are poorly designed.

15.3 Many classes such as Long and BigInteger, String are immutable.

15.4 Some deprecated methods are toxic such as Thread.stop/suspend.

15.5 Know and use the libraries!!!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anyanyamy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值