Character array to String conversion


This java code example shows how to convert an array of characters to a String.
To accomplish this we can utilize one of the String class many contructors, which takes an array of characters as parameter.


/**
 * Main.java
 *
 * @author www.javadb.com
 */

public class Main {
    
    /**
     * Converts an array of characters to a String
     */

    public void convertCharArrayToString() {
        
        char[] charArray = new char[] {'a', 'b', 'c'};
        String str = new String(charArray);
        
        System.out.println(str);
    }
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().convertCharArrayToString();
    }
}


When execute the code will print out the created String.


abc