charAt vs toCharArray
Shubham Kumar- 20 Jul, 2026

String as you know is immutable in java. When you try to manipulate a String object, Java creates a new String out of it. And object creation is expensive. There is a general rule, when you just want to read a character at any index use charAt and when you need to modify the string itself, like rearranging, sorting, creating sub string, breaking it into an array of characters using toCharArray is very cost efficient.
toCharArray takes O(n) time for construction of array and O(n) space is also occupied.