Java日常使用容易出错的几个地方(一)

equals方法的使用
  • Objectequals使用不当会出现空指针的情况

    package com.company;
    import java.math.BigDecimal;
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    package com.company;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            String s = null;
            //System.out.println(s.equals("abc"));  会报java.lang.NullPointerException的错误
            System.out.println("abc".equals(s));
        }
    }
    
  • 推荐使用java.util.Objectsequals方法

    package com.company;
    
    import java.util.Objects;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            String s = null;
            System.out.println(Objects.equals(s,"abc"));
            System.out.println(Objects.equals("abc",s));
            //System.out.println(s.equals("abc"));
            //System.out.println("abc".equals(s));
        }
    }
    //看下源码就知道它不需要考虑空指针问题
        /**
         * Returns {@code true} if the arguments are equal to each other
         * and {@code false} otherwise.
         * Consequently, if both arguments are {@code null}, {@code true}
         * is returned and if exactly one argument is {@code null}, {@code
         * false} is returned.  Otherwise, equality is determined by using
         * the {@link Object#equals equals} method of the first
         * argument.
         *
         * @param a an object
         * @param b an object to be compared with {@code a} for equality
         * @return {@code true} if the arguments are equal to each other
         * and {@code false} otherwise
         * @see Object#equals(Object)
         */
        public static boolean equals(Object a, Object b) {
            return (a == b) || (a != null && a.equals(b));
        }
    
整型包装类值的比较
  • 包装类型的比较

    package com.company;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            int i1 = 15;
            Integer i2 = 15;
            Integer i3 = new Integer(15);
            System.out.println(i1 == i2);//i1和i2都是引用编译时期存放在常量池中的地址  true
            System.out.println(i2 == i3);//i3是在堆中新new出来的 所以地址不等于常量池中的地址 false
            //不在-128~127范围内,Integer都会在堆上新创建一个对象,不会复用已有的对象
            int n1 = 130;
            int n2 = 130;
            Integer n3 = 130;
            Integer n4 = 130;
            System.out.println(n1 == n2);//true
            System.out.println(n3 == n4);// false 在-128~127之内equals和==的结果一样  在这个范围之外如果仅仅比较值是否一样需要用equals来比较   巨坑
        }
    }
    
BigDecimal
  • 比较大小

    package com.company;
    
    import java.math.BigDecimal;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            BigDecimal a = new BigDecimal("0.9");
            BigDecimal b = new BigDecimal("0.99");
            //-1 表示 a 小于 b,0 表示 a 等于 b , 1表示 a 大于 b
            System.out.println(a.compareTo(b));// -1
        }
    }
    
  • 保留n位小数

    package com.company;
    
    import java.math.BigDecimal;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            BigDecimal m = new BigDecimal("3.1415926");
            BigDecimal n = m.setScale(4,BigDecimal.ROUND_HALF_UP);
            System.out.println(n);// 3.1416
        }
    }
    
  • 初始化

    package com.company;
    
    import java.math.BigDecimal;
    
    /**
     * @author 晓果冻
     * @version 1.0
     * @date 2021/6/23 7:45
     */
    public class Demo {
        public static void main(String[] args) {
            Double d1 = 3.145926d;
            BigDecimal b1 = new BigDecimal(d1);//这种方式会丢失精度
            System.out.println(b1);//3.145925999999999778111714476835913956165313720703125
            BigDecimal b2 = new BigDecimal(String.valueOf(d1));
            BigDecimal b3 = BigDecimal.valueOf(d1);//BigDecimal.valueOf(d1)内部执行了Double的toString。Double的toString会自行截断
            BigDecimal b4 = new BigDecimal(d1.toString());
            System.out.println(b4);////3.145926
            System.out.println(b2);//3.145926
            System.out.println(b3);//3.145926
        }
    }
    

Q.E.D.


一个热爱生活的95后精神小伙