1

I am having doubt on System.err.print() execution control. The order in which statements are printed is differs for each execution. I do have 2 questions.

Following is the code

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

Question1: Why in each execution, print statements order differs?

Question2: Why one method prints the values are equal and other method says not equal? (To compare values, we should use 'equals' only. But why '==' operator behaves weird?)

  • This might help you. http://stackoverflow.com/questions/3637936/java-integer-equals-vs – Kaustubh Khare Apr 22 '16 at 05:02
  • 3
    Possible duplicate of [Why does 128==128 return false but 127==127 return true when converting to Integer wrappers?](http://stackoverflow.com/questions/1700081/why-does-128-128-return-false-but-127-127-return-true-when-converting-to-integ). This is the dreaded autoboxing! – Ken Y-N Apr 22 '16 at 05:03
  • OK. Now I understood the answer(Caching Integer Objects) for 2nd question. But Why print order differs each time?(1st question) – Rajkumar Seenappa Apr 22 '16 at 05:08

5 Answers5

1

From 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.

You are getting same object for value 100 because JVM cache it.
From 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:

That's why below statement is true:

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

For Integer i1 = 260; it's return new object that why if (i1 == i2) is false.

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

Question1

Because System.err.println and System.out.println uses different threads. so they can print any time, but the order of print in each stream should be same means first problem1 then problem2

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0

i1 and i2 are Integer objects, so you must compare calling the correct methods and not using ==

The printing order looks random because the system.err has its own stream You need to add a flush to force the stream to be printed everytime after the

System.err.println("i1 a

....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

int type is a primitive, you can compare it using if (i1 == i2) if you've declared it like

    int i1 = 100;
    int i2 = 100;

whereas the 'Integer' type is an object and objects can be checked using .equals like

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

But may return true if those reference the same object.

So what's wrong in using == then ?

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

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
0

Integer is an object in java. When you are comparing

if (i1 == i2)

then you are comparing their references, not there values. You should use the primitive data type 'int' instead.

0

Question1: Why in each execution, print statements order differs?

You are using two different output stream System.out and System.err.

Output streams are cached so all the write goes into this memory buffer. After a period of quiet, they are actually written out.

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

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

Community
  • 1
  • 1
Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22