In this core java tutorial we will learn How to convert char to String in Java. We will also learn how to convert char array to String in Java with program and examples. Read when to convert to primitive type i.e int and wrapper classes i.e Integer in java.
Also read : How to convert Map to List in java
How to convert char to String in Java >
- Program/Example 1 to convert char to String in Java (Most efficient way)
- Program/Example 2 to convert char to String in Java
- Program/Example 3 to convert char to String in Java
- Program/Example 4 to convert char to String in Java (least efficient way)
How to convert char array to String in Java >
- Program/Example 5 to convert char array to String in Java
- Program/Example 6 to convert char array to String in Java
Program/Example 1 to convert char to String in Java (Most efficient way)
String.valueOf method returns the String form of char.
public class ConvertCharToStringExample1 {
public static void main(String[] args) {
char ch='a';
String string = String.valueOf(ch);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = a
*/
|
I will recommend you to use this method to convert char to string in java. It is the fastest and the most efficient way to convert char to string in java.
What does String.valueOf method do internally to convert char to String in Java?
String.valueOf method internally do following steps
- creates char array of single element.
- And then calls new String(charArray, true).
So, let’s see code of valueOf method in java.lang.String api.
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
|
Program/Example 2 to convert char to String in Java
Character.toString method can also be used to convert char to string in java
public class ConvertCharToStringExample2 {
public static void main(String[] args) {
char ch='a';
String string = Character.toString(ch);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = a
*/
|
Program/Example 3 to convert char to String in Java
Declaring Character constructor and then using toString method can also be used to convert char to string in java
public class ConvertCharToStringExample3 {
public static void main(String[] args) {
char ch='a';
String string = new Character(ch).toString();
System.out.println("string = "+string);
}
}
/*OUTPUT
string = a
*/
|
Program/Example 4 to convert char to String in Java (least efficient way)
Using this way is the least efficient of the ways to convert char to string in java.
public class ConvertCharToStringExample4 {
public static void main(String[] args) {
char ch='a';
String string = "" + ch; //Internally it forms
// new StringBuilder().append("").append(ch).toString();
System.out.println("string = "+string);
}
}
/*OUTPUT
string = a
*/
|
But, how "" + ch works internally? And why it's not efficient way to convert char to string in java?
"" + ch internally will expand to
new StringBuilder().append("").append(ch).toString();
Because new StringBuilder calls the StringBuilder constructor and forms StringBuilder with initial capacity of 16 characters (char array of 16 characters). So, for storing just one character we will end up occupying space of 16 characters.
So, let’s see code of StringBuilder constructor in java.lang.StringBuilder api.
public StringBuilder() {
super(16);
}
|
Now, let’s convert char array to String in Java
Program/Example 5 to convert char array to String in Java
public class ConvertCharToStringExample5 {
public static void main(String[] args) {
char[] charArray = {'a','b','c'};
String string = new String(charArray);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = abc
*/
|
Or simply you may use declare array at runtime >
String string = new String(new char[]{'a','b','c'});
Program/Example 6 to convert char array to String in Java
public class ConvertCharToStringExample6 {
public static void main(String[] args) {
char[] charArray = {'a','b','c'};
String string = String.valueOf(charArray);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = abc
*/
|
Or simply you may use declare array at runtime >
String string = String.valueOf(new char[]{'a','b','c'});
Summary -
In this core java tutorial we learned conversion of char to String in Java. We also learned how to convert char array to String in Java with program and examples.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>