Skip to content
Related Articles

Related Articles

Java | Functions | Question 9
  • Last Updated : 12 Jan, 2016

Predict the output of the following program.




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");
    }
}

(A)

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


Answer: (A)

Explanation:
obj.hashCode() function returns a 32-bit hash code value for the object ‘obj’.
obj1.equals(obj2) function returns true if value of obj1 is equal to obj2.
obj1 == obj2 returns true if obj1 refers to same memory address as obj2.


Quiz of this Question
Please comment below if you find anything wrong in the above post

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :