{ 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

相关文章

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

SpringBoot操作spark处理hdfs文件的操作方法

《SpringBoot操作spark处理hdfs文件的操作方法》本文介绍了如何使用SpringBoot操作Spark处理HDFS文件,包括导入依赖、配置Spark信息、编写Controller和Ser... 目录SpringBoot操作spark处理hdfs文件1、导入依赖2、配置spark信息3、cont

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

Java中的密码加密方式

《Java中的密码加密方式》文章介绍了Java中使用MD5算法对密码进行加密的方法,以及如何通过加盐和多重加密来提高密码的安全性,MD5是一种不可逆的哈希算法,适合用于存储密码,因为其输出的摘要长度固... 目录Java的密码加密方式密码加密一般的应用方式是总结Java的密码加密方式密码加密【这里采用的

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b