How to convert a char array into a string in Java

 Posted On  | Yashwant Chavan 

In this tutorial you will learn how to convert a char array into a string using Java API's

Char Array to String

Below Java program, convert a char array to a String value.

java.lang.String.String(char[] value) - Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

String java.lang.String.valueOf(char[] data) - Returns the string representation of the char array argument.

package com.technicalkeeda.app;

public class CharArrayToString {
    public static void main(String args[]) {

        char[] chars = {'h','e','l','l','o'};
        String hello = new String(chars);
        System.out.println(hello);

        String helloValueOf = String.valueOf(chars);
        System.out.println(helloValueOf);
    }
}

output

hello
hello


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0298 seconds.