1
    String sb1 = new String("Soft");
    String sb2 = new String("Soft");

    System.out.println("ANS1->" +sb1 == sb2);
    System.out.println(sb1 == sb2 + " After result");
    System.out.println("ANS2->" +sb1.equals(sb2));

This leads to output as below, but i dont understand why "ANS1" and "After result" texts are not displayed. Kindly help on this.

false

ANS2->true

2
  • 4
    @Biffen - Nope. It isn't Commented Mar 26, 2015 at 11:43
  • try to use equals instead of == as u used in your third output Commented Mar 26, 2015 at 11:45

2 Answers 2

5

Because : "ANS1->" +sb1 == sb2 ==> ("ANS1->" +sb1) == sb2. Now, the compiler does this and prints false because ("ANS1->" +sb1) !=sb2.

Even : System.out.println("ANS1->" +sb1 == sb1); prints false :P

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks lot.. :) Correct way to code is "System.out.println("ANS1->" +(sb1== sb2));"
1

In addition to TheLostMinds´ answer:

System.out.println("ANS1->" + (sb1 == sb2));
System.out.println((sb1 == sb2) + " After result");

Now you see the "lost" strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.