Convert a character array to a String in Java

Google Translate Icon

This post will discuss how to convert a primitive character array to a string using plain Java.

1. Using String Constructor

The String class contains several overloaded versions of its constructor. One such constructor is String(char[]) that accepts a character array as a parameter. It allocates a new string that represents the sequence of characters contained in the character array argument.

Download  Run Code

Output:

Java

2. Using String.valueOf() method

Java SE String class provides static valueOf(char[]) method that returns the string representation of the character array argument.

Download  Run Code

Output:

Java

3. Using String.copyValueOf() method

String class also has copyValueOf(char[]) that is equivalent to valueOf(char[]) which is discussed in last section. It returns a string that contains the characters of the specified character array.

Download  Run Code

Output:

Java

4. Using Java 8

In Java 8, we can do something like by using streams.

Download  Run Code

Output:

Java

 
Please note that in all approaches discussed above, the contents of the character array are copied. That means that any subsequent modification of the character array will not affect the returned string.

That’s all about converting a character array to a Java String.

 
Related Post:

Convert a String to Character array in Java

 
Reference: String Javadoc (Java Platform SE 8)

Rate this post

Average rating 4.66/5. Vote count: 29

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!