Java Compare 2 integers with equals or ==?

后端 未结 4 841
清歌不尽
清歌不尽 2020-12-19 04:55

i am very very new to Java and i would like to know how can i compare 2 integers? I know == gets the job done.. but what about equals? Can this compare 2 integers? (when i s

4条回答
  •  盖世英雄少女心
    2020-12-19 05:24

    int is primitive type.This itself having value but Integer is object and it is having primitive int type inside to hold the value. You can do more operations like compare,longValue,..more by Using wrapper Integer.

    == for Integer will not work the rang above -128 and 127. Integer hold cache value upto this range only in memory. More than this range you have to equals() method only to check Integer wrapper class.

    equals() method will check the value stored in the reference location.

提交回复
热议问题