Integer value comparison

后端 未结 7 1285
北恋
北恋 2021-02-02 09:31

I\'m a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code:

if (count.         


        
7条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 10:11

    well i might be late on this but i would like to share something:

    Given the input: System.out.println(isGreaterThanZero(-1));

    public static boolean isGreaterThanZero(Integer value) {
        return value == null?false:value.compareTo(0) > 0;
    }
    

    Returns false

    public static boolean isGreaterThanZero(Integer value) {
        return value == null?false:value.intValue() > 0;
    }
    

    Returns true So i think in yourcase 'compareTo' will be more accurate.

提交回复
热议问题