beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Convert char to String with examples

By Chaitanya Singh | Filed Under: Java Conversion

In this tutorial, we will see how to convert a char to string with the help of examples.

There are two ways you can do char to String conversion –
1. Using String.valueOf(char ch) method
2. Using Character.toString(char ch) method

Java char to String example using String.valueOf(char ch)

We can use the valueOf(char ch) method of String class to convert a passed char ch to a String. This method accepts char as an argument and returns the string equivalent of the argument.

In the following example we have a char ch with the value 'P' and we are converting this to a String str using the String.valueOf() method. The value of the String after conversion is "P".

public class JavaExample{  
    public static void main(String args[]){  
	//given char
	char ch = 'P';
		
	//char to string conversion
	String str = String.valueOf(ch);  
		
	//displaying the string
	System.out.println("String after conversion is: "+str);    
   }
}

Output:
Java char to String conversion example

Java char to String conversion using Character.toString(char ch)

We can also use the toString(char ch) method of Character class to convert the passed char ch to a String. Similar to String.valueOf(char ch) method, this method also accepts char as a parameter and returns an equivalent String.

In the following example we are converting a char to String using Character.toString(char ch) method.

public class JavaExample{  
   public static void main(String args[]){  
	// char ch with the value 'Z'
	char ch = 'Z';
		
	//char to String using toString() method
	String str = Character.toString(ch);  
		
	//Value of the str after conversion is "Z"
	System.out.println("String after conversion is: "+str);    
   }
}

Output:
Java convert char to String example

❮ PreviousNext ❯

Leave a Reply Cancel reply

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

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap