Java Zero to All All questionsPractise free
Interview questions › Practice Programs — Master Every Pattern › Strings — Practice Programs

How do you reverse a string in Java, and which approach is best?

Level I · BeginnerStrings — Practice Programs
Short answer

Idiomatic and fastest is new StringBuilder(s).reverse().toString().

Explanation

For an interview showing you understand the mechanics, use a char array with two pointers swapping ends inward: O(n) time, O(n) space for the array, O(1) extra beyond it. Recursion is elegant but risks StackOverflow on very long strings and uses O(n) stack. Strings are immutable so you always build a new one; there is no true in-place reverse of a String.

Common mistakes
  • Reversing with string concatenation in a loop.
  • Forgetting surrogate pairs and emoji.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Strings — Practice Programs.

Practise free

More on Strings — Practice Programs