Is it OK to compare an int and a long in Java Is it OK to compare an int and a long in Java java java

Is it OK to compare an int and a long in Java


Yes, that's fine. The int will be implicitly converted to a long, which can always be done without any loss of information.


You can compare long and int directly however this is not recommended.
It is always better to cast long to integer before comparing as long value can be above int limit

long l = Integer.MAX_VALUE;       //2147483647int i = Integer.MAX_VALUE;        //2147483647System.out.println(i == l);       // true l++;                             //2147483648 i++;                             //-2147483648System.out.println(i == l);       // falseSystem.out.println(i == (int)l);  // true