Java Program to Print Array
Updated on Mar 28, 2025 | 6 min read | 5.6k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 6 min read | 5.6k views
Share:
Table of Contents
An array in Java is a collection of similar items that are referred to a single name. Java arrays are slightly different from the arrays of other object-oriented programming languages. Some important facts about the arrays used in Java are:
Check out our free technology courses to get an edge over the competition.
Now that we know how to print an array in Java, we also need to understand the why. Java offers the Array data structure to hold various components of the same data type. Contiguous memory is used to store the items. The array’s elements must be printed in order to show similar contents.
When you understand Java how to print array, you need to remember these important facts.
With a small array’s printing come lesser differences in performance. However, when we deal with large arrays, the time involved in printing can be significant. Therefore, when printing arrays, assessing the performance implications of the approach you opt for is vital.
For instance, the for-loop method prints each element one by one. It can be inefficient for large arrays because it involves iterating all array elements. Comparatively, the Arrays.toString() method converts the array to a string representation and prints it more efficiently as it avoids iterating over all array elements. It is noteworthy that even this method encounters limitations when we deal with multidimensional arrays. When learning how to print string array in Java, remember the Arrays.deepToString() method is best suitable for multidimensional arrays as it gives a string representation of the array that includes all nested arrays. It is far more efficient than using nested for-loops for printing.
Additionally, third-party libraries such as Apache Commons Lang or Guava can provide extra formatting options as well as performance optimization that are often useful for printing large arrays.
Typically, the output of an array is printed as a series of elements that are separated by commas and stay enclosed in square brackets. There are several ways to alter the formatting of the output when printing Java arrays, for instance
Additionally, the String.format() method helps specify the number of decimal places to display. Thus, these methods help control the output’s spacing, precision, and alignment and make it easier to read and understand.
A data structure that stores the items of the same data type is called an array in Java. The array elements are allocated in a contiguous storage location. So, a fixed set of items can be stored in an array. There are several ways in which an array can be printed. They are:
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
The ‘for-each’ loop in Java is also used to iterate over an array of items. It works based on the number of elements in the array. For each iteration, one element is returned by the loop in the defined variable. The syntax of ‘for-each’ loop is as follows:
for(Type var:array)
This static method in Java is of array class and is a constituent of java.util package. It consists of several methods that can manipulate an array. The syntax of the Arrays.toString() method in Java is as mentioned below.
public static String toString(int[] a)
This method takes an array with any primitive type as an input argument. It returns an array in a string form and consists of a list of elements of the array. The conversion of the array’s elements into a string is done by the String.valueOf(int).
When this method is invoked, a multidimensional array is converted into a string of array elements. This function needs an input argument in the form of an array. It returns the array representation in the form of a string. The syntax of this method in Java is as specified below.
public static String deepToString(Object[] a)
This is also a static Java method of Arrays class that belongs to the package java.util. It acts as a connection between the collection based and the array-based API. The syntax in which this method is used in a Java code is mentioned below.
public static <T> List<T>asList(T…a)
This method also enables the user in developing a list of fixed sizes that can be initialized to accommodate many elements.
List<T>obj=Arrays.toString(array[] a
This method returns the input array in the list form.
The Java Iterator is an interface constituted in the java.util package. The creation of an Iterator can be done by invoking the iterator() method. This function is with the collection interface of Java and the returned value is also an iterator.
Apart from printing an array, the Java compiler performs various other array functions, a few of which are listed below.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources