The correct one is the letter C and it has been tested. The reason for presenting the hashcode is because the MyString object does not have the to String method, whereas the Stringbuilder does, which is why it printed correctly.
Answer is C.
Code -
class MyString {
String msg;
MyString(String msg) {
this.msg=msg;
}
}
// Since both classes are in same package
public class Test {
public static void main(String[] args) {
System.out.println("Hello "+ new StringBuilder("Java SE 8"));
System.out.println("Hello "+ new MyString("Java SE 8"));
}
}
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
iSnover
Highly Voted 1 year, 3 months agoDriftKing
Most Recent 5 months agoAnkit1010
11 months, 4 weeks agoUAK94
1 year, 3 months ago