Comparing String and Integer with equals

后端 未结 4 1648
一个人的身影
一个人的身影 2021-01-13 23:02

The output of the below code is false

String str = \"3456\";
String str1 = \"3456\";
System.out.println(Integer.valueOf(str).equals(str1));
         


        
4条回答
  •  一生所求
    2021-01-13 23:34

    Usually, when implementing equals(), one of the first things to do is to check whether the objects are of one and the same type.

    public boolean equals(Object obj) {
        if (!(obj instanceof SomeType)) return false;
        ...
    }
    

    This is also applied in the Integer and String classes, which answers the question why do you receive false as a result.

提交回复
热议问题