java

How to Convert Char to String in Java

There are several ways to convert Char to String in Java. First, String is made up of an array of characters in Java. Then char is a 16-bit or 2-byte unsigned data type.

We can convert a Char to a String using 2 methods:

  • Method 1: Using the toString() Method
  • Method 2: Using the valueOf() Method
 

Method 1: Using the toString() Method
public class CharToString {
 
	public static void main(String[] args)
	{
		char charVar = 'c';
		String strVar = Character.toString(charVar);
		System.out.println("Convert the character 'c' to String: "+strVar);
	}
 
}

Output:

Convert the character 'c' to String: c

 

Method 2: Using the valueOf() Method
public class CharToString {
 
	public static void main(String[] args)
	{
		char charVar = 'c';
		strVar = String.valueOf(charVar);
		System.out.println("Convert the character 'c' to String: "+strVar);
	}
 
}

Output:

Convert the character 'c' to String: c
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *