为什么java每次执行顺序都不一样 & 值比较错误

Why java execution order is different each time & Value comparison is wrong

我对 System.err.print() 执行控制有疑问。语句的打印顺序因每次执行而异。我有两个问题。

代码如下

public class AutoBoxingProblems {

    public static void main(String[] args) {

        problem1();

        problem2();

   }

    private static void problem2() {
        System.out.println("Problem2");
        // Be very careful, Even values are same but two different objects, this will give you equal
        Integer i1 = 100;
        Integer i2 = 100;

        if (i1 == i2) {
            System.err.println("i1 and i2 is equal  ==> Problem2");
        } else {
            System.err.println("i1 and i2 is not equal  ==> Problem2");
        }
    }

    private static void problem1() {
        System.out.println("Problem1");
        // Be very careful, Even values are same, this will give you unequal
        Integer i1 = 260;
        Integer i2 = 260;
        if (i1 == i2) {
            System.err.println("i1 and i2 is equal  ==> Problem1");
        } else {
            System.err.println("i1 and i2 is not equal  ==> Problem1");
        }
    }
}



  //Output
    //Some times
    Problem1
    Problem2               
    i1 and i2 is not equal ==> Problem1 
    i1 and i2 is equal ==> Problem2

    //Some times
    Problem1
   i1 and i2 is not equal ==> Problem1 
   i1 and i2 is equal ==> Problem2
   Problem2

问题1:为什么每次执行打印语句的顺序都不一样?

问题2:为什么一种方法打印值相等而另一种方法打印值不相等? (为了比较值,我们应该只使用 'equals'。但是为什么 '==' 运算符表现得很奇怪?)

i1 和 i2 是 Integer 对象,所以你必须比较调用正确的方法和不使用 ==

打印顺序看起来是随机的,因为 system.err 有自己的流 您需要添加刷新以强制在

之后每次打印流
System.err.println("i1 a

.....

来自JLS 5.1.7. Boxing Conversion

If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

您正在为值 100 获取相同的对象,因为 JVM 缓存了它。
来自 Immutable Objects / Wrapper Class Caching

256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer:

这就是为什么下面的陈述是正确的:

Integer i1 = 100; // return  Cached object
Integer i2 = 100; // return Cached object
if (i1 == i2) { //both object are same that's why its true

对于 Integer i1 = 260; 这是 return 新对象,所以 if (i1 == i2) 是错误的。

Integer i1 = 260; // return new object
Integer i2 = 260; // return new object
if (i1 == i2) { // both object are different that's why false

问题1

因为System.err.printlnSystem.out.println使用不同的线程。所以他们可以随时打印,但是每个流中的打印顺序应该相同意味着首先是问题 1 然后是问题 2

int 类型是一个原始类型,如果你声明它像

,你可以使用 if (i1 == i2) 比较它
    int i1 = 100;
    int i2 = 100;

而 'Integer' 类型是一个对象,可以使用 .equals 来检查对象,例如

 if (i1.equals(i2){...}

但如果它们引用相同的对象,则可能 return 为真。

那么使用==有什么问题呢?

Simply it compares object references and checks to see if the two operands point to the same object not equivalent objects.

Integer 是 java 中的一个对象。当你比较

if (i1 == i2)

那么你是在比较他们的参考资料,而不是那里的价值。您应该改用原始数据类型 'int'。

问题1:为什么每次执行打印语句的顺序都不一样?

您正在使用两个不同的输出流 System.outSystem.err

输出流被缓存,所以所有的写入都进入这个内存缓冲区。沉寂了一段时间,居然写出来了。

Java: System.out.println and System.err.println out of order

Java IO: System.in, System.out, and System.error