All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Char Array To String Java

Last Updated : Mar 11, 2024

Char Array To String Java

In this article we will show you the solution of char array to string java, as usual first we need to import package in case you are using eclipse then all program will import by itself during creation.

Here you need to use string class object to transfer the separate characters joined together to form a string and then we printed result at terminal.

Step By Step Guide On Char Array To String Java :-

The main class we named as ‘ChrStr_prgm’, there given main method definition. Within this you need to define char array with more than two characters values to showcase the process.

For that reason, we declared character array with seven letters. Then you might define string class object and you have to pass that array into string declaration. Then you will get converted string so we displayed end output to the user.

//Char Array To String
package java_prgms;
class ChrStr_prgm
 public static void main(String args[])
        char Arr[]={'w','e','l','c','o','m','e'};
        String str=new String(Arr);
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. The main class ‘ChrStr_prgm’ contains static main method. There you need to create character array, here we created array variable ‘Arr’ it contain values ‘{'w','e','l','c','o','m','e'}’.
  3. Then you have to implement string class object creation during this process you need to pass the character array variable ‘Arr’. Similarly, we did it in example while creating object ‘str’ we passed array ‘Arr’ into them.
  4. It will return the conversion output to object ‘str’, so now it stored output with ‘str’. Using println() we printed result at client end or terminal.
  5. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  6. Using toString() or valueOf() methods you can get this same result with effortless same as this example.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to do conversion of char array to string in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java ChrStr_prgm‘ line for gets the output. Then you can see output 'welcome’ at single stroke, your excepting output.

On eclipse just press of run button then you will get output inside terminal.

If you want to do changes then you can modify the character array values with some other character values. Then you will get output based on your modification.

I hope this article on char array to string java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪