FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

How To Convert Char To String and a String to char in Java


We have following two ways for char to String conversion.

Method 1: Using toString() method

Method 2: Usng valueOf() method

Program to convert char to String

class CharToStringDemo
{
   public static void main(String args[])
   {
      // Method 1: Using toString() method
      char ch = 'a';
      String str = Character.toString(ch);
      System.out.println("String is: "+str);
 
      // Method 2: Using valueOf() method
      String str2 = String.valueOf(ch);
      System.out.println("String is: "+str2);
   }

Output

String is: a
String is: a

Converting String to Char

class StringToCharDemo
{
   public static void main(String args[])
   {
      // Using charAt() method
      String str = "Hello";
      for(int i=0; i

Output

Character at 0 Position: H
Character at 1 Position: e
Character at 2 Position: l
Character at 3 Position: l
Character at 4 Position: o

You may also Find this interesting

Program to take users input

Program to add two numbers

Program to find even number

Program to find odd numbers

Program to find even or odd numbers

Program to Display average of numbers

Program to Display Fabinocci Series

Program to Display Random Numbers

Program to Display Largest of Three numbers