0

I saw this exercise on a Java test, which finally led me to ask for ideas because there are some solutions I tried, but obviously won't solve the problem, due to Math conditions.

So the exercise:

Define S and T variables as it results an endless while loop below!

while(s <= t && s >= t && s != t) 
{

}
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69

2 Answers2

1

think about OOP, in fact 0!=0 is false, but new Integer(0) != new Integer(0) is true because it's not the same reference. so Integer S= new Integer(0); Integer T = new Integer(0); should resolve your problem

CLAIN Cyril
  • 123
  • 9
  • 1
    I was about the answer the question but Clain did it perfectly. You should refer to his answer. – Fenzox Sep 11 '20 at 14:59
  • Using `Integer.valueOf` with any value smaller -128 or bigger 127 should also work, as only number in between are part of the Integer cache and will reuse the same objects. – OH GOD SPIDERS Sep 11 '20 at 15:02
1

here is answer : This is not possible with primitive types. You can achieve it with boxed Integers:

Integer a = new Integer(1); Integer b = new Integer(1); The <= and >= comparisons will use the unboxed value 1, while the != will compare the references and will succeed since they are different objects. you can check farther details in this link How can "a <= b && b <= a && a != b" be true?