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

前端 未结 3 1185
北恋
北恋 2020-12-25 14:15
while( i <= j && i >= j && i != j) {}

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

// it\'s an interview q

3条回答
  •  自闭症患者
    2020-12-25 14:53

    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.

提交回复
热议问题