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

user1472813 picture user1472813 · Jun 21, 2012 · Viewed 42k times · Source

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

long l = 800L
int i = 4

if (i < l) {
 // i is less than l
}

Answer

Jon Skeet picture Jon Skeet · Jun 21, 2012

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