Convert Character Array to String Example Java Program


Aim

To convert a character array to string. This can be achieved by two methods.

Procedure

  • By initializing a new instance of string using character array like 'new String(character_array)'
  • By using a default method in String package like 'String.valueOf(character_array)'

Syntax

new String(character_array)
(OR)
String.valueOf(character_array)

Example Program

public class CharacterArrayToString {
    public static void main(String[] args) {
        char[] array = new char[]{'L','I','T','T','L','E','D','R','O','P','S'};
        
        System.out.println("Converting character array to string");
        
        String methodOne = new String(array);
        System.out.println("Result by method 1 : "+methodOne);
        
        String methodTwo = String.valueOf(array);
        System.out.println("Result by method 2 : "+methodTwo);
    }
}

Sample Output

Converting character array to string
Result by method 1 : LITTLEDROPS
Result by method 2 : LITTLEDROPS