Page 1 of 1

StringBuilder

Posted: Sat Jan 14, 2017 11:07 pm
by sulakshana
Stringbuilder s1= new StringBuilder("meow");
Stringbuilder s2 = new StringBuilder("meow");
if(s1 ==s2)System.out.println("one");
if(s1.equals(s2))System.out.println("two");
if(s1== "meow")System.out.println("three");
if(s1.toString() == "meow")System.out.println("four");
The answers are
Do not compile
Statement that do not compile is removed it prints "two"...
How it can print two when equals does not compile in stringbuilder....

Re: StringBuilder

Posted: Sat Jan 14, 2017 11:52 pm
by admin
Which line doesn't compile? All classes inherit equals method from Object class so why do you think s1.equals(s2) should not compile and should not return true?