Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java subtract two ints, result should be a minimum of zero

Tags:

java

I want to subtract one integer from another, and the result should floor at 0. So 2 minus 4 should equal 0. I could just do

int result = x - y;
if (result < 0) result = 0;

But is there a more elegant way?

like image 365
herpderp Avatar asked Mar 20 '26 03:03

herpderp


1 Answers

int result = Math.max(0, x - y);
like image 128
Thomas Eding Avatar answered Mar 21 '26 18:03

Thomas Eding