• 改善Java代码有哪些方法?


    前言

    Java是一门优秀的面向对象的编程语言,针对遇到同样的一个问题会有很多中解法哪种实现方法是最好的呢,还需要不断的探究JDK的底层原理。我会例出Java改善的建议哦,希望大家可以在平时开发工作去使用,也希望可以帮到你提高工作效率

    用整数处理货币

    大家考虑以下代码输出的值是多少?

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
    2. System.out.println(10.00-9.60);
    3. }
  • 实际结果: 0.40000000000000036

    原因:

    计算机中浮点数有可能是不准确的,因为计算机中浮点数的存储规则导致的。 0.4的二进制是:0.0110……乘2取整,顺序排列

    解决方案:

    1. 使用BigDecimal
    2. 使用整型(把参与运算的值扩大100倍,并转为整型,然后在展现时再缩小100倍,这样处理的好处是计算简单,准确,一般在非金融行业(如零售行业)应用较多)
    1. <pre class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">根据国际标准IEEE(电气和电子工程协会)规定,任何一个浮点数NUM的二进制数可以写为:
    2. NUM = (-1) ^ S * M * 2 ^ E;//(S表示符号,E表示阶乘,M表示有效数字)
    3. ①当S为0时,表示一个正数;当S为1时,表示一个负数
    4. ②M表示有效数字,1<= M <2
    5. ③2^E表示指数
    6. 比如十进制的3.0,二进制就是0011.0 就可以写成(-1)^ 0 * 1.1 * 2 ^ 1
    7. 在比如十进制的-3.0,二进制就是-0011.0 就可以写成(-1)^ 1 * 1.1 * 2 ^ 1
    8. 而规定float类型有一个符号位(S),有8个指数位(E),和23个有效数字位(M)
    9. double类型有一个符号位(S),有11个指数位(E),和52个有效数字位(M)pre>

    边界值校验

    1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Demo {
    2. // 一个会员拥有产品的最多数量
    3. public final static int LIMIT = 2000;
    4. public static void main(String[] args) {
    5. // 会员当前用有的产品数量
    6. int cur = 1000;
    7. Scanner input = new Scanner(System.in);
    8. System.out.println("请输入需要预定的数量:");
    9. while (input.hasNextInt()) {
    10. int order = input.nextInt();
    11. if (order > 0 && order + cur <= LIMIT) {
    12. System.out.println("你已经成功预定:" + order + " 个产品");
    13. } else {
    14. System.out.println("超过限额,预定失败!");
    15. }
    16. }
    17. }
    18. }

原因:

数字越界使校验条件失效,输入2147483647的边界值

建议:

如果一个方法接收的是int类型的参数,那么以下三个值是必须测试的:

提防包装类型的null值

  1. class="prettyprint hljs cpp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static int testMethod(List list) {
  2. int count = 0;
  3. for (int i : list) {
  4. count += i;
  5. }
  6. return count;
  7. }
  8. public static void main(String[] args) {
  9. List list = new ArrayList();
  10. list.add(1);
  11. list.add(2);
  12. list.add(null);
  13. System.out.println(testMethod(list));
  14. }

原因:

在程序for循环中,隐含了一个拆箱过程,在此过程中包装类型转换为了基本类型。我们知道拆箱过程是通过调用包装对象的intValue方法来实现的,由于包装类型为null,访问其intValue方法报空指针异常就在所难免了。

方案:

加入Null的校验。

用偶判断,不用奇判断

需要了解Java后者任意编程语言对于取余的算法实现。大家可以参考程序语言中的取余是如何实现的。

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
  2. Scanner input = new Scanner(System.in);
  3. System.out.println("请输入多个数字判断奇数偶数:");
  4. while (input.hasNextLine()) {
  5. int i = input.nextInt();
  6. String str = i + "->" + (i % 2 == 0 ? "偶数" : "奇数");
  7. // String str = i + "->" + (i % 2 == 1 ? "奇数" : "偶数");
  8. System.out.println(str);
  9. }
  10. }

谨慎包装类型的大小比较

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
  2. Integer i = new Integer(100);
  3. Integer j = new Integer(100);
  4. compare(i, j);
  5. }
  6. public static void compare(Integer i, Integer j) {
  7. System.out.println(i == j);
  8. System.out.println(i > j);
  9. System.out.println(i < j);
  10. }

运行结果:

问题:

方案:

问题清楚了,修改总是比较容易的,直接使用Integer的实例compareTo方法即可,但是这类问题的产生更应该说是习惯问题,只要是两个对象之间的比较就应该采用相应的方法,而不是通过Java的默认机制来处理,除非你确定对此非常了解。

优先使用整型池

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
  2. Scanner input = new Scanner(System.in);
  3. while (input.hasNextInt()) {
  4. int tempInt = input.nextInt();
  5. System.out.println("\n=====" + tempInt + " 的相等判断=====");
  6. // 两个通过new产生的对象
  7. Integer i = new Integer(tempInt);
  8. Integer j = new Integer(tempInt);
  9. System.out.println(" new 产生的对象:" + (i == j));
  10. // 基本类型转换为包装类型后比较
  11. i = tempInt;
  12. j = tempInt;
  13. System.out.println(" 基本类型转换的对象:" + (i == j));
  14. // 通过静态方法生成一个实例
  15. i = Integer.valueOf(tempInt);
  16. j = Integer.valueOf(tempInt);
  17. System.out.println(" valueOf产生的对象:" + (i == j));
  18. }
  19. }

现象:

大于127的数字和128和555的比较过程中产生的却不是同一个对象。

说明:

127的包装对象是直接从整型池中获得的,不管你输入多少次127这个数字,获得的对象都是同一个,那地址自然是相等的。而128、555超出了整型池范围,是通过new产生一个新的对象,地址不同,当然也就不相等了。

整型池的好处:

提高了系统性能,同时也节约了内存空间

优先选择基本类型

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Demo7 {
  2. public static void main(String[] args) {
  3. Demo7 c = new Demo7();
  4. int i = 140;
  5. // 分别传递int类型和Integer类型
  6. c.testMethod(i);
  7. c.testMethod(new Integer(i));
  8. }
  9. public void testMethod(long a) {
  10. System.out.println("基本类型的方法被调用");
  11. }
  12. public void testMethod(Long a) {
  13. System.out.println("包装类型的方法被调用");
  14. }
  15. }

原则:

使用包装类型确实有方便的方法,但是也引起一些不必要的困惑,比如我们这个例子,如果testMethod()的两个重载方法使用的是基本类型,而且实参也是基本类型,就不会产生以上问题,而且程序的可读性更强。自动装箱(拆箱)虽然很方便,但引起的问题也非常严重,我们甚至都不知道执行的是哪个方法。

其他建议:

如果需要使用高效的包装类集合,推进使用fastutil。Maven坐标:

  1. <pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><dependency>
  2. <groupId>it.unimi.dsigroupId>
  3. <artifactId>fastutilartifactId>
  4. <version>8.5.8version>
  5. dependency>pre>

不要随便设置随机种子

原则:

因为产生的随机数的种子被固定了,在Java中,随机数的产生取决于种子,随机数和种子之间的关系遵从以下两个原则:

  1. 种子不同,产生不同的随机数
  2. 种子相同,即使实例不同也产生相同的随机数

看完上面两个规则,我们再来看以下这个例子。

  1. class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public static void main(String[] args) {
  2. //Random r = new Random();
  3. Random r = new Random(1000);//产生的随机数的种子被固定了
  4. for(int i = 1; i <= 4; i++){
  5. System.out.println("第" + i + "次:" + r.nextInt());
  6. }
  7. }

会发现问题就出在有参构造上,Random类的默认种子(无参构造)是System.nonoTime()的返回值(JDK1.5版本以前默认种子是System.currentTimeMillis()的返回值),注意这个值是距离某一个固定时间点的纳秒数,不同的操作系统和硬件有不同的固定时间点,也就是说不同的操作系统其纳秒值是不同的,而同一个操作系统纳秒值也会不同,随机数自然也就不同了.

顺便说下,System.nonoTime不能用于计算日期,那是因为"固定"的时间是不确定的,纳秒值甚至可能是负值,这点与System.currentTiemMillis不同。

new Random(1000)显示的设置了随机种子为1000,运行多次,虽然实例不同,但都会获得相同的四个随机数,所以,除非必要,否则不要设置随机种子。

结束语

本文简单介绍了部分在实际开发中经常会使用到的一些改善Java代码的小技巧或者规范。Java中还有很多很多类似的知识点,不断学习不断成长。

  • 相关阅读:
    使用DISM 删除系统的保留空间
    阿里P8架构师Spring源码阅读心得,都记录在这份PDF文档里面了
    Go 文件操作
    Python实现人脸识别检测,对主播进行颜值排行
    ChatGPT与讯飞星火实测对比
    键盘win键无法使用,win+r不生效、win键没反应、Windows键失灵解决方案(亲测可以解决)
    Java实现前后端任意参数类型转换(Date、LocalDateTime、BigDecimal)
    North American Hardwoods Identification Using Machine-Learning(SCI 读书笔记)
    怎么把图片批量旋转?简单三招就可以实现
    【ESP32】VScode + platformIO 环境搭建
  • 原文地址:https://blog.csdn.net/weixin_62421895/article/details/125998740