23
while( i <= j && i >= j && i != j) {}

how to declare i and j to make it be an infinite loop ?

// it's an interview question I met.

it's asking what's the declarations of i and j, make it be always true.

And I cant make it out by declaring i and j as number types. What other types can meet it ?

Alan
  • 497
  • 1
  • 3
  • 9

3 Answers3

41
Integer i=new Integer(1000);
Integer j=new Integer(1000);

System.out.println((i<=j)+" "+(i>=j)+" "+(i!=j));

i and j will be automatically unboxed to ints for <= and >=, but not for !=. i and j are different instances, but have the same int value. That's why all three comparisons will return true.

Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
  • 2
    nice, I think it's the correct answer. At least it're returning true.// what a tricky interview question !!! – Alan Nov 04 '11 at 20:57
  • 1
    +1 `i <= j && i >= j && i != j` [evaluates to `true` in this case then](http://ideone.com/G1xjn) – Martin Smith Nov 04 '11 at 20:57
  • 1
    +1 for accurate answer, but why would i and j not be unboxed for i!=j? Isn't i!=j supposed to check the values of i and j? Also what other languages this is true for? – SpeedBirdNine Nov 06 '11 at 19:16
  • 1
    @SpeedBirdNine It wouldn't be possible to do `i!=null` otherwise. Sometimes you want to compare instances instead of values. – Piotr Praszmo Nov 06 '11 at 19:32
  • 1
    @SpeedBirdNine More importantly i!=j was already valid code for Integer i and j, returning false for different objects. It is vitally important to Java that additions to the language do not change the behaviour of existing code. – DJClayworth Nov 16 '11 at 19:19
3

This works too ("on my machine"):

Integer a = 128, b = 128;

whereas this won't work:

Integer a = 127, b = 127;

Auto-boxing an int is syntactic sugar for a call to Integer.valueOf(int). This function uses a cache for values from -128 to 127, inclusive. It may cache other values, but in my case, it doesn't.

Thus, the assignment of 128 doesn't have a cache hit; it creates a new Integer instance with each auto-boxing operation, and the reference comparison a != b is true. The assignment of 127 has a cache hit, and the resulting Integer objects are really the same instance from the cache. So, the reference comparison a != b is false.

What I really want to point out is to beware of reference comparison with auto-boxing. A more likely real-world problem is that you expect a == b is true because they were assigned the same (auto-boxed) value, you run some unit tests that confirm your expectation, and then your code fails "in the wild" when some counter exceeds the upper limit of the cache. Fun times!

Wolf
  • 9,679
  • 7
  • 62
  • 108
erickson
  • 265,237
  • 58
  • 395
  • 493
  • +1 Great addition to [Banthar's answer](http://stackoverflow.com/a/8015528/2932052)! – Wolf May 26 '14 at 11:36
-2

Any equal value of 'i' and 'j' will reveal true with the given statement, say:

Integer i = new Integer(1);
Integer j = new Integer(1);

while( i <= j && i >= j && i != j) {}

The magic is with used operator! In case of != operator the compiler takes the operands as objects(including their values) whereas in case of >= or <= the compiler takes the operands value only. Thus, the above statement returns true.

Elias Hossain
  • 4,410
  • 1
  • 19
  • 33