Некоторые общие предложения «хорошего кодирования»:
Я применил эти предложения к вашему готовому коду, см. Ниже:
public class StringTool {
public static String reverse(String source) {
// stop condition of the recursion
if (source.isEmpty()) {
return "";
}
int lastPosition = source.length() - 1;
String lastCharacter = source.charAt(lastPosition);
String restOfSource = source.substring(0, lastPosition);
// place the last character at the beginning and reverse the rest
// of the source recursively
return lastCharacter + reverse(restOfSource);
}
// test method
public static void main(String args[]) {
System.out.println(reverse("A STRING"));
}
}