C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We assign the first element of the array to the value "A" and print the modified array with Arrays.toString.
String: We convert the char array to a string with "new String." This invokes the String constructor.
Java program that uses toCharArray
import java.util.Arrays;
public class Program {
public static void main(String[] args) {
String letters = "abc";
// Convert String to a char array.
char[] array = letters.toCharArray();
System.out.println(Arrays.toString(array));
// Modify the array.
array[0] = 'A';
System.out.println(Arrays.toString(array));
// Get the String.
String result = new String(array);
System.out.println(result);
}
}
Output
[a, b, c]
[A, b, c]
Abc