Integer value comparison

后端 未结 7 1282
北恋
北恋 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:14

    You can also use equals:

     Integer a = 0;
    
     if (a.equals(0)) {
         // a == 0
     }
    

    which is equivalent to:

     if (a.intValue() == 0) {
         // a == 0
     }
    

    and also:

     if (a == 0) {
    
     }
    

    (the Java compiler automatically adds intValue())

    Note that autoboxing/autounboxing can introduce a significant overhead (especially inside loops).

提交回复
热议问题