In this tutorial you will learn about the Java Program to convert char Array to String and its application with practical example.
Java Program to convert char Array to String
In this tutorial, we will learn to create a Java program that will convert char Array to String using Java programming.
Prerequisites.
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Conditional Statements in Java programming.
Program to convert char Array to String.
In this program, First, we will declare the array in the java program. Then we will define a function to convert a character array to a string. Then we will return the string to the main function. At last, we will print the string to the user.
With the help of this program, we can convert char Array to String.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Initializing the array of character. 3. Passing that array to the function. 4. Converting the character array to string. 5. Returning the string to the main function for printing. 6. End the program. |
Program to convert the character to string:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Java program to Convert Character Array to String // Using copyOf() method ofArrays class // Main class of program class CharArrayToString { // Method 1 // To convert a character // array to a string using the constructor public static String toString(char[] a) { // Creating object of String class String string = new String(a); return string; } // Main driver method public static void main(String args[]) { // Declaring the required variable for the program. // Character array. char ch[] = {'g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g', ' ', 'I','T','S',' ','W','3','A','D','D','A'}; //Printing the covverted string from the array throug the fucntion // Printing converted string from character array System.out.println(toString(ch)); } } |
Output:-
character to string output.
In the above program, we have first initialized the required variable.
- ch[] = it will hold a character array declared.
Passing that character array to the function for conversion.
A user-defined function that will convert the string from the Character array.
Printing the string by calling the function from the logical code.