📜  Java |功能|问题9

📅  最后修改于: 2021-06-29 21:06:49             🧑  作者: Mango

预测以下程序的输出。

class Test
{
  
    public static void main(String[] args)
    {
        String obj1 = new String("geeks");
        String obj2 = new String("geeks");
  
        if(obj1.hashCode() == obj2.hashCode())
            System.out.println("hashCode of object1 is equal to object2");
  
        if(obj1 == obj2)
            System.out.println("memory address of object1 is same as object2");
  
        if(obj1.equals(obj2))
            System.out.println("value of object1 is equal to object2");
    }
}

(一种)

hashCode of object1 is equal to object2
value of object1 is equal to object2

(B)

hashCode of object1 is equal to object2
memory address of object1 is same as object2
value of object1 is equal to object2

(C)

memory address of object1 is same as object2
value of object1 is equal to object2

答案: (A)
解释:
obj.hashCode()函数返回对象’obj’的32位哈希码值。
如果obj1的值等于obj2,则obj1.equals(obj2)函数返回true。
如果obj1指向与obj2相同的内存地址,则obj1 == obj2返回true。这个问题的测验
如果您在以上帖子中发现任何错误,请在下面发表评论