-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Tuesday, August 25, 2020

Java Program To Convert Char Array To String

  • August 25, 2020
  • by

 


Java Program To Convert Char Array To String

In this Java Program, we convert the char array to String. 

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Logic : 1. Using String object: 
                 String str = new String(ch);

             2. Using ValueOf method.
                 String str2 = String.valueOf(ch);

Step 1 : Create an array of char and store the characters in it.

Step 2 : Create an object of String as str and store the characters of ch in it.

Step 3 : Output the object str.

Step 4 : Create object of String str and store characters in it by using ValueOf() method.

Step 5 : Output the object str.
              
Program :

public class CharArrayToString 
    {
        public static void main(String args[])
        {
        //Method 1 : Using String object
         char[] ch = { 'h', 'a','v','e','á','g', 'o', 'o', 'd', 'd', 'a', 'y'};
         String str = new String(ch);
         System.out.println(str);
         
         //Method 2 : Using valueOf method
         String str2 = String.valueOf(ch);
         System.out.println(str2);
        }
}

Output :

haveágoodday
haveágoodday

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