Core Java

Char Array to String Java Example

Hello readers! In this tutorial, we feature an example on how to convert a Char Array to String in Java. We will learn different ways of this conversion. Meanwhile, you can also check the String to Char Array Example.

1. Introduction

Java programming offers possible ways to convert a character array (char []) to String. A new String is allocated that represents the sequence of characters contained in the char array. There are three ways to perform this conversion in Java i.e.

Char Array to String Java
  • Using String object
  • Using valueOf() or copyValueOf() method
  • Using a StringBuilder object

To start with the captioned tutorial, we are hoping that users at present have their preferred Ide installed on their machines. For easy usage, I am using Eclipse Ide on a Windows operating system.

2. Char Array to String Java Example

In this example, we’ll demonstrate the above-mentioned ways to perform the character array to String conversion. For a better understanding, developers can execute the below code in Eclipse Ide.

2.1 Char Array to String Java Conversion

Let us understand this with the help of a simple code snippet.

CharArrayToStringExample

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.java.basics;
 
// Class to demonstrate the Char Array to String conversion
public class CharArrayToStringExample {
 
    public static void main(String[] args) {
 
        // Initializing a sample character array.
        char[] charArray = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!' };
 
        // Approach #1- Using the String object to pass "char array" into the constructor.
        String strObject = new String(charArray);
        System.out.println(strObject);
 
        System.out.println("\n");
 
        // Approach #2 - Using the "valueOf()" or "copyValueOf()" method.
        String strValueOf = String.valueOf(charArray);              // String.copyValueOf(charArray);
        System.out.println(strValueOf);
 
        System.out.println("\n");
 
        // Approach #3 - Using the StringBuilder object.
        // We'll perform iteration on char array and append it to the StringBuilder and call "toString()" method
        // to print the value.
        StringBuilder builder = new StringBuilder();
        for(char c : charArray)
            builder.append(c);
 
        System.out.println(builder.toString());
    }
}

If everything goes well, the following output will be printed in the Ide console.

Output

1
2
3
4
5
hello world!
 
hello world!
 
hello world!

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

3. Conclusion

In this tutorial, we learned how to convert a character array to a String. Developers can download the sample application as an Eclipse project in the Downloads section.

4. Download the Eclipse Project

This was an example of how to convert a char array to a String in Java.

Download
You can download the full source code of this example here: Char Array to String Java Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button