Home » Core Java » Char Array to String Java Example

About Yatin

Yatin
The author is graduated in Electronics & Telecommunication. During his studies, he has been involved with a significant number of projects ranging from programming and software engineering to telecommunications analysis. He works as a technical lead in the information technology sector where he is primarily involved with projects based on Java/J2EE technologies platform and novel UI technologies.

Char Array to String Java Example

Hello readers! In this tutorial we feature a comprehensive Char Array to String Java Example. We will learn different ways for this convertion. 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 converting a character array to a String.

Download
You can download the full source code of this example here: Char Array to String Java Example
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of