1

I want this code to print true. I saw this question when passing my OCA7 exam. I know the thing about the string pool and all and I also know the difference between equals() and ==.

The exam question didn't offer the .equals() option. It only said to figure out a way to make this code to return true by comparing a Stringbuilder with a String "==" operator. I had to figure out 1 option among 4 other that all had used the "==". No equals –

package test.test;

public class SomeClass{

    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("abcd");

        String str = sb.toString();
        System.out.println(sb.toString() == str);

    }
}
Community
  • 1
  • 1
AchillesVan
  • 4,156
  • 3
  • 35
  • 47

0 Answers0