What is the difference between these lines?
Why toString()
?
Here I take the number from the array and compare with the reverse (I check for polindrom)
But if you remove toString()
at the end of the condition, it will not work?
for (int i = 0; i < n; i++) { if (Integer.toString(arr[i]).equals(new StringBuffer().append(Integer.toString(arr[i])).reverse().toString())) System.out.println(arr[i]); }
But another example without toString()
works:
System.out.print(new StringBuffer().append("Hello").reverse());
elloH
System.out.print
doestoString()
for you. - PetSerAl 7:55 pmif (anObject instanceof String) { ... } else return false;
, since StringBuilder comes, that's why the condition is false. You can use contentEquals - Drawn Raccoon