{ 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

相关文章

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码