Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer value comparison with null vallue

Hi I had a question related to Integer comparison.

say I have two Integer count1 and count2, I want to implement the following action:

if (count1 bigger than count2)
    do something;
else
    do something else;

I know I can use count1.compareTo(count2) > 0. however, when count1 or count2 is a null value, program will return a NullPointerException. Is there a way to implement that if count1 or count2 is a null value, return false when doing comparison between count1 and count2?

like image 753
Vortex Avatar asked Dec 20 '25 23:12

Vortex


1 Answers

I think you want:

if (count1 != null && count2 != null && count1 > count2)
    do something;
else
    do something else;

Java will auto un-box the Integer objects to int primitive values to make the mathematical > comparison.

like image 97
Bohemian Avatar answered Dec 23 '25 11:12

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!