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.
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.
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.
The String class has a constructor that accepts a char array and creates a string object.
char[] ch = {'a', 'b', 'c', 'd', 'e'};
String str = new String(ch);
System.out.println(str);
System.out.println(str.getClass().getName());
: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
The valueOf()
method of the String
class converts a char array to a string object. Here's how to use it:
char[] ch = {'a', 'b', 'c', 'd', 'e'};
valueOf()
method of the String
class.String str = String.valueOf(ch);
System.out.println(str);
System.out.println(str.getClass().getName());
: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
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:
char[] ch = {'a', 'b', 'c', 'd', 'e'};
copyValueOf()
method of the String
class.String str = String.copyValueOf(ch);
System.out.println(str);
System.out.println(str.getClass().getName());
: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
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.