Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Arrays.toString() in Java with Examples
Next article icon

Arrays.toString() in Java with Examples

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Arrays.toString() method belongs to the Arrays class in Java. It converts an array into its string representation consisting of a list of the array's elements. In the case of an Object Array, if the array contains other arrays as elements, their string representation shows memory addresses instead of contents.

Example:

Below is the simplest way to print an array as a string using Arrays.toString() method:

Java
import java.util.Arrays;  public class ArrayToString {     public static void main(String[] args) {                  // create an integer array         int[] n = {1, 2, 3, 4,};          // print the array using          // Arrays.toString()         System.out.println(Arrays.toString(n));     } } 

Output
[1, 2, 3, 4] 

Table of Content

  • Syntax of Arrays.toString() method
  • Examples of Arrays.toString() in Java
    • Using Arrays.toString() with Primitive Arrays
    • Using Arrays.toString() with Object Arrays

Syntax of Arrays.toString() method

public static String toString(int[] array)

public static String toString(Object[] array)

Parameters: array: The array, whose string representation to return.

Return Type:

  • String representation of the array's elements.
  • If the array is null, it returns the string "null".

Examples of Arrays.toString() in Java

Using Arrays.toString() with Primitive Arrays

When we need to print or log the contents of a primitive array, we use Arrays.toString() method that converts a primitive array into a string representation.

Java
// Java program to demonstrate  // working of Arrays.toString() // with primitive arrays import java.util.Arrays;  class GFG {     public static void main(String[] args) {                  // create different types of arrays and         // print their contents using Arrays.toString()         boolean[] arr1 = new boolean[] { true, true, false, true };         byte[] arr2 = new byte[] { 10, 20, 30 };         char[] arr3 = new char[] { 'g', 'e', 'e', 'k', 's' };         double[] arr4 = new double[] { 1, 2, 3, 4 };         float[] arr5 = new float[] { 1, 2, 3, 4 };         int[] arr6 = new int[] { 1, 2, 3, 4 };         long[] arr7 = new long[] { 1, 2, 3, 4 };         Object[] arr8 = new Object[] { 1, 2, 3, 4 };         short[] arr9 = new short[] { 1, 2, 3, 4 };          System.out.println(Arrays.toString(arr1));         System.out.println(Arrays.toString(arr2));         System.out.println(Arrays.toString(arr3));         System.out.println(Arrays.toString(arr4));         System.out.println(Arrays.toString(arr5));         System.out.println(Arrays.toString(arr6));         System.out.println(Arrays.toString(arr7));         System.out.println(Arrays.toString(arr8));         System.out.println(Arrays.toString(arr9));     } } 

Output
[true, true, false, true] [10, 20, 30] [g, e, e, k, s] [1.0, 2.0, 3.0, 4.0] [1.0, 2.0, 3.0, 4.0] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] 


Using Arrays.toString() with Object Arrays

When we want to print an array of objects, and the objects either override toString() method, we use Arrays.toString() method that invokes the toString() method of each object and provides their string representations. If not overridden, it defaults to the class name and memory address.

Java
// Java program to demonstrate  // working of Arrays.toString() // for user defined objects import java.util.Arrays;  class Main {     public static void main(String[] args) {                Student[] arr = { new Student(1, "a", "UP"),                         new Student(2, "b", "MP"),                         new Student(3, "c", "Delhi") };          System.out.println(Arrays.toString(arr));     } }  // class to represent a student class Student {        // instance variables     int r;     String n, a;      // Constructor     public Student(int r, String n, String a)     {         this.r = r;         this.n = n;         this.a = a;     }      // Overriding the toString() method to return      // student details in a formatted string     @Override     public String toString()     {          return "Roll No: " + this.r + ", Name: " + this.n + ", Address: " + this.a;     } } 

Output
[Roll No: 1, Name: a, Address: UP, Roll No: 2, Name: b, Address: MP, Roll No: 3, Name: c, Address: Delhi] 

Next Article
Arrays.toString() in Java with Examples

S

Shikhar Goel
Improve
Article Tags :
  • Misc
  • Java
  • Java-Arrays
Practice Tags :
  • Java
  • Misc

Similar Reads

    Class toString() method in Java with Examples
    The toString() method of java.lang.Class class is used to convert the instance of this Class to a string representation. This method returns the formed string representation. Syntax: public String toString() Parameter: This method does not accept any parameter. Return Value: This method returns the
    1 min read
    Currency toString() Method in Java with Examples
    The toString() Method of Currency class in Java is used to retrieve the currency code of this currency which is actually the official ISO 4217 currency code in a string format Syntax: CURRENCY.toString() Parameters: This method does not accept any parameters. Return Value: This method returns the IS
    2 min read
    Byte toString() method in Java with examples
    The toString() method of Byte class is a built in method in Java which is used to return a String value. public String toString() Syntax: ByteObject.toString() Return Value: It returns a String object, the value of which is equal to the value of the ByteObject. Below is the implementation of toStrin
    2 min read
    AbstractSet toString() method in Java with Example
    The toString() method of Java AbstractSet is used to return a string representation of the elements of the Collection. The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is us
    2 min read
    ArrayList toArray() method in Java with Examples
    The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a para
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences