23

I had this interview question some years ago but I haven't found the answer yet.

What should be x and y to make a infinite loop?

while (x <= y&& x >= y && x != y) {

}

We tried with Nan,infinity+/-,null

float vs int.

bitli
  • 575
  • 2
  • 14
  • What is the type of x and y. Are they primitives or reference type or they are allowed to be anything? – prashant Apr 12 '13 at 11:53
  • 2
    You want two instances of the same number. the `<=` and `>=` cancel each other out as long `x is equal to y numerically` but `x==y` is a reference check, ergo two instances will return true on `x!=y` even though equal by value. – Shark Apr 12 '13 at 11:53
  • 9
    YAUIQ - yet another useless interview question. Interesting, even entertaining, I have no objection to putting it here for amusement and even learning. But I don't think it helps anyone evaluate how good a programmer employee the interviewee will be. – arcy Apr 12 '13 at 11:57
  • @rcook Ya beat me to it :) – Deepak Bala Apr 12 '13 at 12:02
  • 1
    And of [how to declare i and j to make it be an infinite loop?](http://stackoverflow.com/q/8015146/73226) – Martin Smith Apr 12 '13 at 12:15
  • Please check this, already answered [link][1] [1]: http://stackoverflow.com/questions/15025275/how-can-i-define-variables-to-make-an-infinity-while-loop-with-these-conditions – mreaevnia Apr 12 '13 at 12:42
  • Interesting that the original had -3 rating, but this gets +15... – yshavit Apr 12 '13 at 13:48

4 Answers4

37

You need two variables which are comparable, have the same value, but represent different instances, for example:

Integer x = new Integer(0);
Integer y = new Integer(0);

x <= y and y <= x are both true because the Integer are unboxed, however the instance equality x == y is false.

Note that it works with Float, Long and Double too, and any value (not just 0) works.


You can also play with the intricacies of your JVM - they generally cache integer up to 127 only, so this would work too:

Integer x = 128;
Integer y = 128;

(but it would not with 127).

Or more simply, since Doubles are generally not cached at all:

Double x = 0d;
Double y = 0d;
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 3
    ... comparable with `<=` and `>=`, ... which are only the primitive wrappers (excl. Boolean). – Bernhard Barker Apr 12 '13 at 11:54
  • That is some devilish trickery with boxing. Good answer. Folks reading this should also try `Integer.valueOf(0)` and see how that affects the result. – Deepak Bala Apr 12 '13 at 12:01
  • I don't think caching will be a problem unless you use`Integer.valueOf`. A new Integer should be a new Integer, always. – yshavit Apr 12 '13 at 13:11
  • 1
    @yshavit in `Integer x = 1; Integer y = 1;`, `x` and `y` are the same (cached) instance. When creating the integer with `new` a new instance is returned each time. – assylias Apr 12 '13 at 14:30
  • 1
    @assylias Yes, I should I should said "`Integer.valueOf` _or autoboxing_", and I should have backticked the first instance of `new Integer`. The takeaway is: don't comment on SO posts first thing in the morning on an iPad. ;) – yshavit Apr 12 '13 at 15:51
  • @assylias +1 for mentioning about the JVM intricacies about `Integer` . But I did not understand Why `Double`'s are not cached at all. Can you please explain? – Geek Sep 27 '13 at 13:30
  • 1
    @Geek it is in the [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7): *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.* - a given JVM can go further and cache more but is not required to. In practie, it does not make much sense to cache doubles: `127d` is probably not used much more often than say `0.123d`. Whereas 1-127 (int) are fairly common (ASCII)... – assylias Sep 27 '13 at 13:43
  • @assylias thanks for the follow-up comment. I can not give you another +1 though unfortunately. – Geek Sep 27 '13 at 14:00
5

you have to create two Integer Objects, for example:

Integer x = new Integer(2);
Integer y = new Integer(2);

Because x and y are Objects and no ordinal types, you get an infinite loop.

Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
2

You've got your answer, I just wanted to say how I got to the same one. In the normal world such a test would be useless, there is no way for two number to work like that. So that means it HAS to be some java specific.

x and y could be either simple types - which makes it impossible right away.

x and y could be objects. But what objects are compared with <= or >=? Only 'boxed' numbers. Thus the answer comes up really fast.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

Here it is.

Integer x =1;
Integer y = new Integer(1);
while(x <= y&& x >= y && x != y) {
    System.out.println("Success");
}
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47