Comparing Integer objects vs int

前端 未结 5 1636
余生分开走
余生分开走 2020-12-10 17:14

I fixed an endless loop by changing Integer to int in the following:

public class IntTest {
    public static void main(String[] args) {
        Integer x=-1         


        
5条回答
  •  一整个雨季
    2020-12-10 17:28

    Integer is an Object, and objects are compared with .equals(..)

    Only primitives are compared with ==

    That's the rule, apart from some exceptional cases, where == can be used for comparing objects. But even then it is not advisable.

提交回复
热议问题