This article will help you explore a concept that is hard to notice but holds immense value in the programming world, I am referring to Converting Char To String In Java. Following pointers will be covered in this article,
The need to convert a char to string arises often while writing a program in java. There are multiple methods by which it can be implemented, some of which are:
- By using String.valueOf(char) method of String class
- By using Character.toString(char) method of Character class
Moving on with this article on Char to string in Java
Using String.valueOf(char ch):
This method of String class can be used to convert a specified char to a String. Char is accepted as an argument, and the string equivalent of the argument is returned.
public class CharToString{ public static void main(String args[]){ //given char char character = 'S'; //char to string conversion String string = String.valueOf(character); //displaying the string System.out.println("String after conversion : "+string); }a }
Output:
String after conversion: S
Moving on with this article on Char to string in Java
Using Character.toString(char ch):
This method of the Character class can also be used to convert the specified character to a string.
public class CharToString{ public static void main(String args[]){ // char charcater with the value 'Q' char character = 'Q'; //char to String using toString() method String string = Character.toString(character); //Value of the string after conversion is "Q" System.out.println("String after conversion : "+string); } }
Output:
String after conversion : Q
Java provides the user with efficient ways to convert a char to a string with ease.
Thus we have come to an end of this article. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.