How to convert an Array to String in Java?

How to convert an Array to String in Java?

In this post, you'll learn the various methods to convert an Array to String in Java

Below are the various methods to convert an Array to String in Java:

1. Arrays.toString() method:

Arrays.toString() method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space). It returns “null” if the array is null.

// Java program to demonstrate 
// working of Arrays.toString() 

import java.io.*; 
import java.util.*; 

class GFG { 
    public static void main(String[] args) 
    { 

        // Let us create different types of arrays and 
        // print their contents using Arrays.toString() 
        boolean[] boolArr 
            = new boolean[] { true, true, false, true }; 
        char[] charArr 
            = new char[] { 'g', 'e', 'e', 'k', 's' }; 
        double[] dblArr 
            = new double[] { 1, 2, 3, 4 }; 
        int[] intArr 
            = new int[] { 1, 2, 3, 4 }; 
        Object[] objArr 
            = new Object[] { 1, 2, 3, 4 }; 

        System.out.println( 
            "Boolean Array: "
            + Arrays.toString(boolArr)); 
        System.out.println( 
            "Character Array: "
            + Arrays.toString(charArr)); 
        System.out.println( 
            "Double Array: "
            + Arrays.toString(dblArr)); 
        System.out.println( 
            "Integer Array: "
            + Arrays.toString(intArr)); 
        System.out.println( 
            "Object Array: "
            + Arrays.toString(objArr)); 
    } 
} 

Output

Boolean Array: [true, true, false, true]
Character Array: [g, e, e, k, s]
Double Array: [1.0, 2.0, 3.0, 4.0]
Integer Array: [1, 2, 3, 4]
Object Array: [1, 2, 3, 4]

2. StringBuilder append(char[]):

The java.lang.StringBuilder.append(char[]) is the inbuilt method which appends the string representation of the char array argument to this StringBuilder sequence.

// Java program to illustrate the 
// StringBuilder.append(char[]) method 

import java.lang.*; 

public class Geeks { 

    public static void main(String[] args) 
    { 

        StringBuilder sbf 
            = new StringBuilder("We are geeks "); 
        System.out.println(sbf); 

        // Char array 
        char[] astr 
            = new char[] { 'G', 'E', 'E', 'k', 'S' }; 

        // Appends string representation of char 
        // array to this String Builder 
        sbf.append(astr); 
        System.out.println("Result after"
                        + " appending = "
                        + sbf); 

        sbf = new StringBuilder("We are -"); 
        System.out.println(sbf); 

        // Char array 
        astr = new char[] { 'a', 'b', 'c', 'd' }; 

        /* Appends string representation of char 
                array to this StringBuilder */
        sbf.append(astr); 
        System.out.println("Result after appending = " + sbf); 
    } 
} 

Output:

We are geeks 
Result after appending = We are geeks GEEkS
We are -
Result after appending = We are -abcd

java arrays string

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java String Class Example | String Class in Java Tutorial

Java String is an inbuilt class that deals with all that is written in double quotation marks, or other words string is a collection of characters.

Java String Programs For Beginners | Java String Examples | Java Training

Java String Programs will talk about top 10 important programs in java strings with examples.

Java String length() | How To Find String Length In Java

Java String length(). Java provides a in-built java.lang.String.length() method. Length of a String is essentially the number of 16-bit Unicode characters.

Java String – String Functions In Java With Examples

In this post, we will be discussing about a new concept, Java String. String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.