Convert Char Array to String

JavaJavaBeginner
Practice Now

Introduction

In Java, a char array is a collection of characters, while a String is a sequence of characters and a class in Java. It's common to need to convert a char array to a string, and there are several ways to do this.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117418{{"`Convert Char Array to String`"}} java/classes_objects -.-> lab-117418{{"`Convert Char Array to String`"}} java/class_methods -.-> lab-117418{{"`Convert Char Array to String`"}} java/modifiers -.-> lab-117418{{"`Convert Char Array to String`"}} java/oop -.-> lab-117418{{"`Convert Char Array to String`"}} java/identifier -.-> lab-117418{{"`Convert Char Array to String`"}} java/arrays -.-> lab-117418{{"`Convert Char Array to String`"}} java/data_types -.-> lab-117418{{"`Convert Char Array to String`"}} java/output -.-> lab-117418{{"`Convert Char Array to String`"}} java/variables -.-> lab-117418{{"`Convert Char Array to String`"}} java/string_methods -.-> lab-117418{{"`Convert Char Array to String`"}} java/system_methods -.-> lab-117418{{"`Convert Char Array to String`"}} end

Set up the Project

Assuming we are in the ~/project/ directory, Let's create a new Java file called ConvertCharArrayToString.java with the following command:

touch ConvertCharArrayToString.java

Then open the file in your preferred text editor.

Using String Constructor

The String class has a constructor that accepts a char array and creates a string object.

  1. Create a char array with some characters.
char[] ch = {'a', 'b', 'c', 'd', 'e'};
  1. Pass the char array to the constructor of the String class.
String str = new String(ch);
  1. Print the result and class name.
System.out.println(str);
System.out.println(str.getClass().getName());
  1. Save and close the file.
:wq

The complete code should look like this:

public class ConvertCharArrayToString {
    public static void main(String[] args) {
        char[] ch = {'a', 'b', 'c', 'd', 'e'};
        String str = new String(ch);
        System.out.println(str);
        System.out.println(str.getClass().getName());
    }
}

To run the code, compile the Java file and then execute the compiled file with the following commands in the terminal:

javac ConvertCharArrayToString.java
java ConvertCharArrayToString

Using valueOf() Method

The valueOf() method of the String class converts a char array to a string object. Here's how to use it:

  1. Create a char array with some characters.
char[] ch = {'a', 'b', 'c', 'd', 'e'};
  1. Pass the char array to the valueOf() method of the String class.
String str = String.valueOf(ch);
  1. Print the result and class name.
System.out.println(str);
System.out.println(str.getClass().getName());
  1. Save and close the file.
:wq

The complete code should look like this:

public class ConvertCharArrayToString {
    public static void main(String[] args) {
        char[] ch = {'a', 'b', 'c', 'd', 'e'};
        String str = String.valueOf(ch);
        System.out.println(str);
        System.out.println(str.getClass().getName());
    }
}

To run the code, compile the Java file and then execute the compiled file with the following commands in the terminal:

javac ConvertCharArrayToString.java
java ConvertCharArrayToString

Using copyValueOf() Method

The copyValueOf() method of the String class can also be used to convert a char array to a string object. Here's how to use it:

  1. Create a char array with some characters.
char[] ch = {'a', 'b', 'c', 'd', 'e'};
  1. Pass the char array to the copyValueOf() method of the String class.
String str = String.copyValueOf(ch);
  1. Print the result and class name.
System.out.println(str);
System.out.println(str.getClass().getName());
  1. Save and close the file.
:wq

The complete code should look like this:

public class ConvertCharArrayToString {
    public static void main(String[] args) {
        char[] ch = {'a', 'b', 'c', 'd', 'e'};
        String str = String.copyValueOf(ch);
        System.out.println(str);
        System.out.println(str.getClass().getName());
    }
}

To run the code, compile the Java file and then execute the compiled file with the following commands in the terminal:

javac ConvertCharArrayToString.java
java ConvertCharArrayToString

Summary

In this lab, we explored three methods to convert a char array to a string in Java. We learned how to use the constructor, valueOf(), and copyValueOf() methods of the String class.

By following these steps, you can write Java code that will convert a char array to a string using any of these methods.

Other Java Tutorials you may like