Java program to convert a character array to string



The String class provides a constructor which accepts an array of characters and creates a String object.

To convert a character array to string simply pass this array to the constructor of the String class.

Example

Live Demo

public class CharArrayToString {
   public static void main(String args[]){
      char[] charArray = {'h', 'e', 'l', 'l', 'o'};
      String str = new String(charArray);
      System.out.println(str);
   }
}

Output

hello
Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician


Advertisements