0
package wrapper;

public class IntegerEqualExam {
    public static void main(String[] args) {
        Integer i1 = new Integer(100);
        Integer i2 = new Integer(100);
        Integer i3 = new Integer(200);
        Integer i4 = new Integer("100");

        System.out.println("i1.equals(i2) = " + i1.equals(i2));
        System.out.println("i1.equals(i3) = " + i1.equals(i3));
        System.out.println("i1.equals(i4) = " + i1.equals(i4));


        if(i1 == i2)
            System.out.println("same reference");
        else
            System.out.println("different reference");

        System.err.println("error");
    }
}

I was just practicing Integer class, I found the System.err.println method by accident.

But I had some problem to understand this situation.

Whenever I run the this program, Result is changed.

Why did it happen?

유리한
  • 19
  • 1
  • 2
    Please dont show program code as a picture. It's very hard to understand what is happening then, since people usually want to copy the code and test the problem themselves. So please remove the pictures and paste your sourcecode. – hamena314 Apr 20 '17 at 15:22
  • 2
    Because the output of ``out`` and ``err`` is buffered in two different streams that get flushed to your console in an arbitrary order. – f1sh Apr 20 '17 at 15:23
  • Use `StringBuilder`, not `StringBuffer`. – Lew Bloch Apr 20 '17 at 15:42

0 Answers0