The following statement doesn't work in Java, but works in C:
char c[] = "abcdefghijklmn";
What's wrong?
Does the char array can only be initialized as following?
char c[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'};
If you don't want to use String toCharArray(), then yes, a char array must be initialized like any other array-
char[] c = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'};
Try this:
You can initialize it from a String:
However, if what you need is a string, you should simply use a string:
The literal "abcdefghijklmn" is a String object in Java. You can quickly convert this into a char array by using the String toCharArray() method.
Try this:
You could use
if you don't mind creating an unnecessary String.
Unlike in C, Strings are objects, and not just arrays of characters.
That said, it's quite rare to use char arrays directly. Are you sure you don't want a String instead?