How to define a char array in Java?

In Java, you can define a char array using the following syntax:

char[] charArray = new char[n];

n represents the length of the array. You can also directly initialize the values of a char array.

char[] charArray = {'a', 'b', 'c'};

After defining a char array, you can access and modify elements in the array using indexes, for example:

charArray[0] = 'd';
char firstChar = charArray[0];

It is important to note that a char array is a reference data type and a new char array object can be created using the new keyword.

char[] charArray = new char[3];
charArray[0] = 'a';
charArray[1] = 'b';
charArray[2] = 'c';
Leave a Reply 0

Your email address will not be published. Required fields are marked *