In this example, you will see how to create a character array of a string in java.
Create a character array of a string in java.
In section, you will see how to create a character array of a string in java. A string is combination of character. It is available in java.lang package. The toCharArray() is method of string class. It converts a string into character array in java.
Code:CharArrayOfString.java
public class CharArrayOfString {
public static void main(String[] args) {
// Create a string
String str = "Hello JAVA";
// Display the value of string
System.out.println("Length of string : " + str.length());
System.out.println("String : " + str);
// Create character array of string
char[] charArray = str.toCharArray();
// Calculate length of character array
int len = charArray.length;
// Display the length of character array.
System.out.println("Length of character array : " + len);
System.out.println("Element of character : ");
for (int i = 0; i < len; i++) {
System.out.print(charArray[i]);
}
}
}
Output:
|
Length of string : 10
String : Hello JAVA Length of character array : 10 Element of character : Hello JAVA |

[ 0 ] Comments