String is palindrome

MY CODE IS GIVING ERRORS IN SOME TEST CASES WHICH I AM NOT ABLE TO THINK. PLEASE HELP.

Hi @mananaroramail,
There is no code available to me to review . Please save your code in a online coding blocks ide and share the url here.

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); StringBuilder sb = new StringBuilder(str); StringBuilder revstr = sb.reverse(); boolean bool = true; if(sb.equals(revstr)) { System.out.print(bool); } else { bool = false; System.out.print(bool); } } }

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
StringBuilder sb = new StringBuilder(str);
StringBuilder revstr = sb.reverse();
boolean bool = true;
if(sb.equals(revstr))
{
System.out.print(bool);
}
else
{
bool = false;
System.out.print(bool);
}
}
}

i have commented the corrections in the code

" .equals " works only for strings.
But StringBuilder ( say sb ) is formed in heap and outside INTERN POOL .
Also when we use the statement String str = new String( " any string " )
It is also formed in heap and outside INTERN POOL.
So why can’t we use .equals for sb ???
as it works perfectly for str.
IS THIS ATTRIBUTE RELATED TO MUTABILITY OF STRINGS ???

No it’s actually the way .equals is written for sb. In sb , .equals method actually compares the address while in string it compares the

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.