To reverse a string in Java, you can convert the string into a character array, then loop through the array backwards to build a new string. This method utilizes basic string and array manipulation techniques without relying on built-in methods.
In depth
Reversing a string in Java can be accomplished by converting the string into a character array because strings in Java are immutable. The idea is to loop through the character array from the last index to the first and concatenate each character to a new StringBuilder object. This way, we efficiently build the reversed string without needing additional libraries or built-in functions. It's important to handle edge cases, such as when the input string is null or empty, to avoid exceptions. This technique provides a good exercise in understanding how strings and arrays work in Java.
Another consideration is performance: in terms of time complexity, this approach runs in O(n) time, where n is the length of the string, as we have to visit each character once. However, it’s important to note that concatenating strings directly in a loop can lead to inefficiencies due to string immutability in Java. Using a StringBuilder is a best practice because it minimizes the overhead associated with creating multiple string instances.
Example scenario
In a web application, you may need to reverse user input for a specific feature, such as displaying a username in reverse for a fun 'guess the name' game. By implementing a string reversal function using character arrays and StringBuilder, you ensure that your application remains efficient and responsive, even when users input long strings. This operation can be critical in user-facing features where performance is essential.
Common mistakes
A common mistake is to use the string concatenation operator (+) inside a loop to build the reversed string. This approach is inefficient because it creates multiple intermediary string objects, which increases memory consumption and runtime. Another mistake is not accounting for null or empty inputs, which can lead to NullPointerExceptions and runtime errors. Always ensure to validate inputs before processing them.
Follow-up questions
- What are some other ways to reverse a string in Java?
- How would you handle null or empty strings when reversing?
- Can you explain the difference between using StringBuilder and directly concatenating strings?
- What is the time complexity of your approach?

Want all 2,000 questions in one PDF?
- Short answers, in-depth explanations, common mistakes and follow-ups
- 89 topics, from Beginner to Architect, with a clickable contents list
- 226 extra questions that are not on this website
- Free Bonus Pack: 30-day study plan, behavioral questions, salary scripts
Includes 226 extra questions that are not on this site, plus a free Bonus Pack.
Get both books for $9.99 (save $2.99):
The material stays free to read online. This is the organised, offline, print-ready PDF edition. Secure payment by PayPal; your personal download link is emailed after payment is confirmed. Digital product, no shipping.