Multiple choice string

The code snippetif( "Welcome".trim() == "Welcome".trim() )System.out.println("Equal");elseSystem.out.println("Not Equal");will

  1. A. Compile and display “Equal”

  2. B. Compile and display “Not Equal”

  3. C. Cause a compiler error

  4. D. Compile and display NULL

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In Java, the trim() method returns a canonical string from the string pool if the resulting string is a literal. However, because trim() creates a new string object, == usually compares references. In many JVM implementations, this specific literal comparison might return true, but equals() is the proper way to compare content.