How to return a boolean true value when comparing a String with a StringBuilder without using equals?

46 views Asked by At

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);

    }
}
0

There are 0 answers