{ 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

相关文章

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.