-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Friday, June 5, 2020

Java Program To Convert Char To String

  • June 05, 2020
  • by

Java Program To Convert Char To String

In this Java Program, we convert char to string by using toString() and valueOf() methods.

Logic : Method 1: using toString() method
             String str = Character.toString(ch);

            Method 2: using valueOf() method
            String str2 = String.valueOf(ch);

Step 1 : For method 1, create variable ch and store character a in it.

Step 2 : Use the logic of method 1 and store it in the variable str.

Step 3 : Print the result by using System.out.print method

Step 4 : For method 2, use the logic of method 2 and store it in the variable str2.

Step 5 : Print the result by using System.out.print method

Program :

public class CharToString 
    {
        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

For execution of the same do visit my YouTube Channel:


A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages