Signup/Sign In
PUBLISHED ON: JANUARY 29, 2021

How to convert Char to String in Java

In this post, we are going to convert char to String in Java. char is one of the Java data types that is used to store a single character while String is a sequence of characters.

To convert char type to string, we can use the valueOf() method of String class or toString() method of the Character class. Character is a wrapper class in Java that is used to handle char type objects.

The valueOf() method of String class is used to get a string from char. It takes a single argument and returns a string of the specified type.

The toString() method of the Character class returns a string of char type value.

We used plus (+) operator as well to convert char to string because this operator used to concatenate two objects and returns a string.

Time for an Example:

Let's take an example to convert char to a string. Here, we are using the valueOf() method of String class that returns a string.

public class Main {
	public static void main(String[] args){
		char ch = 's';
		System.out.println(ch);
		// char to String
		String str = String.valueOf(ch);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


s
s
java.lang.String

Example:

Let's take another example to get a string from a char type. Here, we are using the toString() method of the Character class that returns a string by converting char.

public class Main {
	public static void main(String[] args){
		char ch = 's';
		System.out.println(ch);
		// char to String
		String str = Character.toString(ch);
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


s
s
java.lang.String

Example:

In this example, we are using plus(+) operator to concatenate char value with a string value. The plus operator returns a string value after concatenation.

public class Main {
	public static void main(String[] args){
		char ch = 's';
		System.out.println(ch);
		// char to String
		String str = ""+ch;
		System.out.println(str);
		System.out.println(str.getClass().getName());
	}
}


s
s
java.lang.String



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.