I am interested to know which version of the program is the best execution time.
Both options look easy to implement. But what is better to use and in what cases?
String reverse:
public static String reverse(String s)
{
String rev = "";
for (int i = s.length() - 1; i >= 0; i--)
rev += s.charAt(i);
return rev;
}
StringBuilder reverse:
public static String reverse(String s)
{
StringBuilder rev = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--)
rev.append(s.charAt(i));
return rev.toString();
}
user2271868
source
share