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.

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