String型とObject型の比較

String型の値とObject型の値をequalsメソッドで比較します。
falseになったような気がしましたが、検証してみるとtrueが返ります。


検証
public class Test {
    public static void main(String[] args) {
        Object obj = "0";
        String str = new String("0");
        if(obj.equals(str)){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
    }
}

検証結果
true

初版2008/01/11 :最終更新2008/01/11
HOME