Java char array of character array and the String String type conversion method

  • 2020-04-01 04:32:53
  • OfStack

This article illustrates how to convert a char array to a String in Java. Share with you for your reference, as follows:

When programming in the Java language, using the "password field" jPasswordField component, you need to use the getPassword() method for that component if you want to get the password value. The getPassword() method of jPasswordField returns an array of type char that we often need to convert to String for operations such as password matching or password assignment. At this point, you need to convert an array of type char. Of course, it's not uncommon to encounter situations where String types are converted to char arrays.

The following code is the least verbose.

Ex. :


String strStringType="my string"; //Creates a string variable, strStringType
char[] chrCharArray; //Create a character array, chrCharArray
chrCharArray = strStringType.toCharArray(); //Converts a string variable to an array of characters
strStringType= String.valueOf(chrCharArray ); //Converts an array of characters to a string

Here, the key is two methods.

(1) the toCharArray() method of the String class, converts the String to an array of characters
(2) the valueOf() method of the String class, converts an array of type char to a String

I hope this article has been helpful to you in Java programming.


Related articles: