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

2024-06-15 08:48

本文主要是介绍{ 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!!!





这篇关于{ Java Puzzlers } 一本有意思的JAVA错误集锦的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062976

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.