LAST UPDATED: NOVEMBER 24, 2020
Java Character toString(char c) Method
Java toString(char c)
method is a part of the Character
class. This method returns the equivalent String object of the specified character value. The result is a string of length consisting solely of the specified char.
This method does not handle supplementary characters.
This method is used to convert char value into String.
Syntax:
public static String toString(char c)
Parameters:
The parameter passed is the char value whose equivalent String is to be returned.
Returns:
Returns the String equivalent of the character value passed as a parameter.
Example 1:
Here, the character values are converted into its equivalent String representations.
public class StudyTonight
{
public static void main(String[] args)
{
char ch1 = 'A';
char ch2 = 't';
System.out.println("Equivalent String is : "+Character.toString(ch1));
System.out.println("Equivalent String is : "+Character.toString(ch2));
}
}
Equivalent String is : A
Equivalent String is : t
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value ");
Scanner sc = new Scanner(System.in);
char ch = sc.next().charAt(0);
String s = Character.toString(ch); //converting to string
System.out.println("String value is : "+ s);
}
catch(Exception e)
{
System.out.println("Invalid input!!");
}
}
}
Enter the value m
String value is : m
***********************
Enter the value J
String value is : J
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.