In Java, converting the characters to strings is useful to save the memory as strings are memory efficient. These strings can store bulk data without taking up much space/memory. The characters, however, store each character individually making the code complex, and memory is unnecessarily assigned.
In such cases, this conversion is needed to improve the coding efficiency and use the individual characters to form a single string. This approach enables the programmer to modify the code flexibly without making many changes in the code.
How the Conversion From “char” to String is Carried Out?
In Java, the character value is specified as “char” which is a primitive data type. On the other hand, a string is a non-primitive data type and is defined as a “String”.
The character values are stored in single quotes i.e., ‘a’, ‘A’ whereas the String values are stored in double quotes i.e., “Lisa”, “John” etc.
Below is the conversion process from “char” to “String” in Java:
Input Character Value
char x = ‘a’;
Output String Value
“a”
How to Convert Java char to String?
A “char” can be converted to a “String” in Java via the below techniques:
- Concatenation.
- “String.valueOf()” Method.
- “Character.toString()” Method.
Approach 1: Convert char to String in Java Using Concatenation
Concatenation is the approach where a value is added/appended to another value.
Example 1: Convert a Lowercase Character to String in Java
The below code converts a lowercase character to a string by concatenating an empty string with the character:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
char x = 'a';
String y = x + "";
System.out.println("Character to String Conversion -> "+y);
}}
Code Explanation
- Initialize the given lowercase character value.
- Concatenate this character value with an empty string and store the result in a String type variable.
- Display the converted string value.
Output
To confirm the conversion, the class type of the resultant value can also be logged on the console by including the following code line:
System.out.println(y.getClass());
Output
Example 2: Convert Uppercase Character to String in Java
In this example, an uppercase character will be converted to a string instead:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
char x = 'A';
String y = x + ""; System.out.println("Character to String Conversion -> "+y);
System.out.println(y.getClass());
}}
Code Explanation
Initialize an uppercase character and similarly concatenate an empty string with it. After that, return the corresponding string value and display its class.
Output
Approach 2: Convert char to String in Java Using the “String.valueOf()” Method
This method converts the values of multiple primitive types to a string.
Syntax
public static String valueOf(dt x)
Syntax Explanation
- “dt” represents the type that can be any primitive data type.
- “x” is the value to be transformed/converted to a string.
The following code uses the discussed method to convert both the lowercase and uppercase character values to the string:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
char x = 'a';
char y = 'A';
String con1 = String.valueOf(x);
String con2 = String.valueOf(y);
System.out.println("String Value After Conversion -> "+con1);
System.out.println("String Value After Conversion -> "+con2);
System.out.println("First Value Class -> "+con1.getClass());
System.out.println("Second Value Class -> "+con2.getClass());
}}
Code Explanation
- Initialize the lower and upper case characters.
- Use the “String.valueOf()” method to convert both the “char” type values to string.
- Display the converted string values and return their classes.
Output
Approach 3: Convert char to String in Java Using the “Character.toString()” Method
This method gives a string object against the specified character value.
Syntax
Character.toString(x)
Here, “x” is the character to be converted to a string.
Return Value
It gives the corresponding string value of the given character.
The below code demonstration uses this method to give a string against the passed character value:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
char x = 'a';
char y = 'A';
String con1 = Character.toString(x);
String con2 = Character.toString(y);
System.out.println("String Value After Conversion -> "+con1);
System.out.println("String Value After Conversion -> "+con2);
System.out.println("First Value Class -> "+con1.getClass());
System.out.println("Second Value Class -> "+con2.getClass());
}}
Code Explanation
- Declare the “char” variable having the lower and upper case character values.
- After that, use the “Character.toString()” method to convert both characters to string.
- Finally, apply the “getClass()” method to return the classes of the resultant values to confirm the conversion.
Output
How to Convert a Character Instance to String in Java?
A character instance can also be converted to a string using the discussed “Character.toString()” method. Below is the code example:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
Character x = new Character('a');
System.out.println("Character Instance Value -> "+x);
System.out.println("Original Value Class -> "+x.getClass());
String y = Character.toString(x);
System.out.println("\nString Value After Conversion -> "+y);
System.out.println("Resultant Value Class -> "+y.getClass());
}}
Code Explanation
- Create a character instance/object having the given character and display it.
- Get the class of the character instance.
- Apply the “Character.toString()” method that takes the character instance and converts it to a string.
- Display the class of the converted string.
Output
How to Convert a Character Class(Non-Primitive) Value to String in Java?
A character value defined as a non-primitive type can also be converted to a string using the “String.valueOf()” method:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
Character x = 'a';
System.out.println("Default Value Class Before Conversion -> "+x.getClass());
String y = String.valueOf(x);
System.out.println("String Value After Conversion -> "+y);
System.out.println("Resultant Value Class After Conversion -> "+y.getClass());
}}
Code Explanation
- Store the character value in the non-primitive “Character” class and get its class.
- Use the “String.valueOf()” method to get the string value against the given character.
- Get the class of the converted value.
Output
How to Convert a Character Array to String in Java?
A character array can also be converted to a single string by creating a String instance and passing the character array in it as its argument:
package jbArticles;
import java.util.Arrays;
public class Fibonacci {
public static void main(String args[]){
char[] x = {'a', 'b', 'c', 'd'};
System.out.println("Character Array -> "+Arrays.toString(x));
String y = new String(x);
System.out.println("After conversion to String -> "+y);
}}
Code Explanation
- Import the specified package to work with Arrays.
- Declare a character array having the given characters and return this array as a string.
- Create a String instance and pass the character array in it as an argument.
- Display the converted single string value.
Output
How to Convert a String to char in Java?
To convert any string back to “char”, consider the following code example:
package jbArticles;
public class Fibonacci {
public static void main(String args[]){
String x = "David";
System.out.println("Given String: "+x);
System.out.println("Converted Characters: ");
for (int i=0;i<x.length();i++) {
char y = x.charAt(i);
System.out.println(y);
} }}
Code Explanation
- Initialize the string and display it.
- Use the “for” loop to iterate the string characters using the “length()” method.
- Display each character individually and store them in a “char” type variable to apply the conversion.
- Return the converted characters from a string.
Output
Conclusion
A “char” can be converted to a String in Java by concatenating the character with an empty string simply. This is the most effective approach as it consumes less memory. However, this conversion can also be achieved using other built-in methods as well. These characters can be lower or uppercase. However, a character instance, character array, or a non-primitive type character can also be converted to a string.