For a modern Java 8+ approach, you can use Streams to transform the string into an array in a clean and efficient way.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String word = "name";
String[] letters = word.chars()
.mapToObj(c -> String.valueOf((char) c))
.toArray(String[]::new);
System.out.println(Arrays.toString(letters));
}
}
Why use this?
- Modern and concise—uses Java Streams for an elegant solution.
- Efficient processing—can easily add transformations while converting characters.
When to avoid?
- If you’re not familiar with Java Streams or working with older Java versions.
- Slightly more complex than
split("")for simple use cases