Discussion
What will be the output of the program?
public class Snippet16
{
void execute()
{
String s = "ab";
StringBuilder sb = new StringBuilder("ab");
System.out.println(s.equals(sb.toString()) + " " + s.equals(sb));
}
public static void main(String[] args)
{
Snippet16 demo = new Snippet16();
demo.execute();
}
}
Answer: D
Converting the builder to a String gives a value equal to s, so the first test is true. The second passes the builder itself, and String.equals returns false for anything that is not a String, whatever it contains. StringBuilder does not override equals either.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development