Search code examples
javaloopsif-statementautoboxing

How can "a <= b && b <= a && a != b" be true?


Here is the code i have to figure it out how is it possible. I have a clue but i do not know how to do it. I think it is about negative and positive numbers and maybe the variable modifiers as well. I am a beginner i looked the solution everywhere but i could not find anything usable.

the question is that: You need to declare and initialize the two variables. The if condition must be true.

the code:

if( a <= b && b <= a && a!=b){
       System.out.println("anything...");
}

I appreciate you taking the time.


Solution

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