错误之Integer之间的数值大小比较

在 java api 中Integer这个类很简单,但是,但是,但是一定要留心。

之前在项目开发中,犯了两个句柄(引用)比较的错误,而且信心百倍,深信不疑,总感觉自己是对的,怀疑过 java 开发环境(idea),怀疑过......甚至怀疑过java api (你说二不二?)。

这种东西很容易犯错误,而且不好检查出来,我觉得不好检查出来的原因是:1,太简单没用心  2,而且在四五千行的代码中,根本不值得怀疑。

不允许


        Integer x = 100001;
        Integer y = 100001;

        if (x.intValue() == y) {
            System.out.println("a");
        } else {
            System.out.println("b");
        }
        //输出结果是:a

        if (x == y) {
            System.out.println("c");
        } else {
            System.out.println("d");
        }
        //输出结果是:d
        
        Integer c = 2;
        Integer d = 2;
        if (c == d) {
            System.out.println("c == d");
        } else {
            System.out.println("c != d");
        }
        
        //输出结果是 c == d



你可能感兴趣的:(错误之Integer之间的数值大小比较)