Given:
What is the result?
Given:
What is the result?
The result is false:true. In the code, the comparison p1 == p2 evaluates to false because p1 and p2 are two different objects, even though they have the same values for their fields. Object comparison with '==' checks for reference equality, not value equality. The comparison p1.name.equals(p2.name) evaluates to true because the equals method of the String class compares the actual content of the strings, and both names are 'Pen'.
Antwort C, ist richtig public class Product { int id; String name; public Product (int id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Product p1 = new Main (101, "Pen"); Product p2 = new Main (101, "Pen"); Product p3 = p1; boolean ans1 = p1 == p2; boolean ans2 = p1.name.equals(p2.name); System.out.println(ans1 + " : " + ans2); } } false : true
Correct.
Correct Answer is C.
correct answer is C: false:true
boolean ans1 = p1 == p2; // false - because p1 and p2 is two different objects boolean ans2 = p1.name.equals(p2.name); // true
correct one is C
C is correct
tested: class Product { int id; String name; public Product(int id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Product p1 = new Product(101, "Pen "); Product p2 = new Product(101, "Pen"); Product p3 = p1; boolean ans1 = p1 == p2; boolean ans2 = p1.name.equals(p2.name); System.out.print(ans1 + " " + ans2); } }
Unfortunately you added a space after Pen in p1 that's why the answer came out as false. answer is C
The answer is C:tested and proven
c is correct
c is correct
Answer is C. class Product { int id; String name; public Product(int id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Product p1 = new Product(101, "Pen"); Product p2 = new Product(101, "Pen"); Product p3 = p1; boolean ans1 = p1 == p2; boolean ans2 = p1.name.equals(p2.name); System.out.print(ans1 + " " + ans2); } }
Actually it's false false, because the first name has a white space "Pen ".. while the other name is "Pen"..
Oops ignore my comment...
Answer D false false checked class Product { int id; String name; public Product(int id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Product p1 = new Product(101, "Pen "); Product p2 = new Product(101, "Pen"); Product p3 = p1; boolean ans1 = p1 == p2; boolean ans2 = p1.name.equals(p2.name); System.out.print(ans1 + " " + ans2); } }
You have a white space in the first name "Pen "
equals method not overriden so should be false:false right?
ans2 is comparing the name property not the object itself so its true that "Pen".equals("Pen")
Yes, i am agree with you. But when I compiled the correct option is C only.
false false
Line 7 is comparing the objects, not the content of objects, so that is false.