Convert char array to string in java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn convert char array to string in java.

convert char array to string in java

Convert char array to string in java

To convert char array to string java we will be creating string object by passing char array name to the constructor. Here’s the java program on how to convert char array to string,

Also read – convert binary number to decimal number in java

public class CharArrayToString
{
   public static void main(String[] args)
   {
      char charArray[] = new char[]{'F','l','o','w','e','r','B','r','a','c','k','e','t','s'};

      String str = new String(charArray);
      System.out.println(str);
   }
}

Output:

FlowerBrackets


Now we are going to see how to convert a char array to a string in java using valueof() method of String class,

public class CharArrayToStringDemo
{
   public static void main(String[] args)
   {
      char charArray[] = new char[]{'F','l','o','w','e','r','B','r','a','c','k','e','t','s'};
      
      String str = String.valueOf(charArray); 
      System.out.println(str);
   }
}

Output:

FlowerBrackets

Related Posts