How To Print Array With Elements In Java? [Solution + Example]
Maybe your like
Pages
- Home
- core java
- spring
- online courses
- thread
- java 8
- coding
- sql
- books
- oop
- interview
- certification
- free resources
- best
How to Print int array in Java - Examples
In order to print an integer array, all you need to do is call Arrays.toString(int array) method and pass your integer array to it. This method will take care of the printing content of your integer array, as shown below. If you directly pass int array to System.out.println(), you will only see the type of array and a random number. int[] primes = {5, 7, 11, 17, 19, 23, 29, 31, 37}; System.out.println("Prime numbers : " + primes); System.out.println("Real prime numbers : " + Arrays.toString(primes)); //Ok Output: Prime numbers : [I@5eb1404f Real prime numbers : [5, 7, 11, 17, 19, 23, 29, 31, 37]How to Print byte array in Java
the printing byte array is no different than printing int array, as Arrays class provides an overloaded method toString(byte[] bytes) to print contents of byte array in Java, as shown below. By the way, if you want to print byte array as Hex String then see this link. String random = "In Java programming langue, array is object"; byte[] bytes = random.getBytes(); System.out.println("What is inside bytes : " + bytes); System.out.println("Not visible, check closely .." + Arrays.toString(bytes)); Output What is inside bytes : [B@31602bbc Not visible, check closely ..[73, 110, 32, 74, 97, 118, 97, 32, 112, 114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 110, 97, 103, 117, 101, 44, 32, 97, 114, 114, 97, 121, 32, 105, 115, 32, 111, 98, 106, 101, 99, 116]How to Print String array in Java
Printing string array in Java is probably the easiest thing to do because Arrays class has another overloaded version of toString() to accept Object. This method calls toString() of Object to get a printable String. This can also be used to print the array of any arbitrary object in Java. The user-defined object must override the toString() method to show something reasonable on the console. String[] buzzwords = {"Java", "Android", "iOS", "Scala", "Python"}; System.out.println("Buzzing .." + buzzwords); System.out.println("Not buzzing? try again : " + Arrays.toString(buzzwords)); Output: Buzzing ..[Ljava.lang.String;@46f5331a Not buzzing? try again : [Java, Android, iOS, Scala, Python]How to Print Two Dimensional Array in Java
Arrays class provides a different method to print a two-dimensional array in Java, it’s called toDeepString(). It's capable of printing multi-dimensional array in Java and similar to toDeepEquals() which is used to compare multi-dimensional array in Java. This method is also overloaded and provides 8 + 1 primitive and object versions to accept boolean, byte, short, char, int, long, float, double and Object in Java. Here is an example of how to print a two-dimensional array in Java. String[][] phones = {{"Apple", "iPhone"}, {"Samsung", "Galaxy"}, {"Sony", "Xperia"}}; System.out.println("Hot phones .. " + phones); System.out.println("Not hot? See again.." + Arrays.deepToString(phones)); Output Hot phones .. [[Ljava.lang.String;@57398044 Not hot? See again..[[Apple, iPhone], [Samsung, Galaxy], [Sony, Xperia]] Complete Java Program to Print Array in Java
This is the full Java code of print different types of arrays in Java. As explained in this article, it prints integer, String, byte, and two-dimensional array using toString() and deepToString() method of java.util.Arrays class. You can copy-paste this program in your Java IDE and run it. Don't need any third-party libraries. import java.util.Arrays; /** * Java Program to print arrays in Java. We will learn how to print String, int, * byte and two dimensional arrays in Java by using toString() and * deepToString() method of Arrays class. * * @author http://java67.blogspot.com */ public class PrintArrayInJava{ public static void main(String args[]) { // Example 1 : print int array in Java int[] primes = {5, 7, 11, 17, 19, 23, 29, 31, 37}; System.out.println("Prime numbers : " + primes); // Not OK System.out.println("Real prime numbers : " + Arrays.toString(primes)); //Ok // Example 2 : print String array in Java String[] buzzwords = {"Java", "Android", "iOS", "Scala", "Python"}; System.out.println("Buzzing .." + buzzwords); System.out.println("Not buzzing? try again : " + Arrays.toString(buzzwords)); // Example 3 : print two dimensional array in Java String[][] phones = {{"Apple", "iPhone"}, {"Samsung", "Galaxy"}, {"Sony", "Xperia"}}; System.out.println("Hot phones .. " + phones); System.out.println("Not hot? See again.." + Arrays.deepToString(phones)); // Example 4 : print byte array in Java String random = "In Java programming langue, array is object"; byte[] bytes = random.getBytes(); System.out.println("What is inside bytes : " + bytes); System.out.println("Not visible, check closely .." + Arrays.toString(bytes)); } } Output: Prime numbers : [I@5eb1404f Real prime numbers : [5, 7, 11, 17, 19, 23, 29, 31, 37] Buzzing ..[Ljava.lang.String;@46f5331a Not buzzing? try again : [Java, Android, iOS, Scala, Python] Hot phones .. [[Ljava.lang.String;@57398044 Not hot? See again..[[Apple, iPhone], [Samsung, Galaxy], [Sony, Xperia]] What is inside bytes : [B@31602bbc Not visible, check closely ..[73, 110, 32, 74, 97, 118, 97, 32, 112, 114, 111, 103, 114, 97, 109, 109, 105, 110, 103, 32, 108, 97, 110, 97, 103, 117, 101, 44, 32, 97, 114, 114, 97, 121, 32, 105, 115, 32, 111, 98, 106, 101, 99, 116]And, here is a nice summary of how to print array in Java:
That's all about how to print array in Java. We have learned how to print objects of an array, instead of an array object, which is nothing but a hashCode. I really hope that Java should add a toString() in Array, instead of providing Arrays.toString(), don't know when they chose other parts. Nevertheless, toString() and toDeepString() from java.util.Arrays class is sufficient to print any kind of one-dimensional and two-dimensional array in Java. Though special care needs to take, while printing byte arrays, which requires byte to be encoded in Hex string. Labels: array, core java 6 comments:
samiullaApril 1, 2015 at 8:05 AMIn "How to Print byte array in Java", how to print the original string from byte[] ??
ReplyDeleteReplies
javin paulApril 1, 2015 at 8:09 AMHello @samiulla, you can convert byte array to String using new String(). If its coming from other host or encoded in some other encoding other than platform's default, provide that encoding e.g. "UTF-8" to String constructor.
DeleteReplies- Reply
samiullaApril 1, 2015 at 8:37 AMI have a JAXBElement element 'baseData' which has some base64 data in it. Am trying to read this using (new String(baseData.getData().getValue())). But all I get is non-readable characters. If I use just (baseData.getData().getValue()), I get the offset address. Am just lost since two days. Can you please help me with this? I want to read the Base64 data and print it.
DeleteReplies- Reply
Reply
UnknownOctober 25, 2015 at 10:53 PMI tried to find the method toDeepEquals() but success. there is a method Arrays.deepEquals().Please Update the articles for further/next reader. if i am right.Thanks and RegardsShesh Narayan Chakrapani.
ReplyDeleteReplies- Reply
UnknownOctober 3, 2016 at 12:49 AMAwesome example's
ReplyDeleteReplies- Reply
AnonymousOctober 12, 2021 at 7:20 PMHow print s2 : Array [] s1 = {"a",1};Array [] s2 = { "b" , s1}; System.out.println("... " + s2 ?
ReplyDeleteReplies- Reply
Feel free to comment, ask questions if you have any doubt.
Newer Post Older Post Home Subscribe to: Post Comments (Atom)Recommended Courses
- best python courses
- best java courses
- system design courses
- best spring courses
- best hibernate courses
- best design pattern courses
- best Linux courses
- best JavaScript courses
- best data structure and algorithms courses
- Best Multithreading Courses
- best MERN stack courses
- Best Git courses
- Best Microservice Courses
- Best DevOps Courses
- best MEAN stack Courses
- free Java courses
- free DSA courses
- free sql courses
- free Linux courses
- Free Docker courses
- free JUnit courses
Array Tutorials
- array - copy
- array - tutorial
- array - add/remove element
- array - linked list
- array - reverse
- array - sorting
- array - sum
- array - binary search
- array - vector
- array - remove
- array - reverse in place
- array - to list
- array - initialization
- array - insertion sort
- array - to string
- array - example
- array - data structure
- array - compare
- array - liner search
Categories
- .NET
- abstract class
- Affiliate marketing
- After Effects
- Agile
- AI Tools
- Amazon Web Service
- android
- Angular
- Anonymous class
- Ansible
- apache camel
- Apache kafka
- Apache spark
- app development
- array
- ArrayList
- Artificial Intelligence
- automation
- aws
- aws certification
- Azure Certifications
- backend development
- bash
- basics
- beginners
- best of java67
- best practices
- Big Data
- binary tree
- bit manipulation
- black friday deals
- Blockchain
- BlockingDeque
- books
- Bootstrap
- business analysis
- ByteByteGo
- C programming
- C++
- Career
- ChatGPT
- Chef
- cloud certification
- Cloud Computing
- Code Example
- Code Review
- codecademy
- Codemia
- CodeRabbit
- coding
- coding exercise
- Coding Interview
- Coding Problems
- Comparator
- computer science
- Computer Vision
- concurrency tutorial
- ConcurrentHashMap
- core java
- core java interview question answer
- course review
- Coursera
- courses
- crontab
- CSS
- Cyber Monday
- Cyber Security
- Data Analysis
- data science
- data structure and algorithm
- Data Visualization
- database
- datacamp
- date and time
- debugging
- deep learning
- default methods
- design pattern
- DevOps
- DevSecOps
- Distributed Systems
- Django
- docker
- double
- Drawing
- dyanmic programming
- dynamic Programming
- eBooks
- Eclipse
- EJB
- enum
- equals
- error and exception
- Ethical hacking
- Excel
- exception
- Exponent
- expressjs
- FAANG
- Figma
- Firebase
- flatmap
- float
- Flutter
- free resources
- freelancing
- Frontend Masters
- fun
- Fundamental
- fundamentals
- Game development
- garbage collection
- general
- Generics
- gifts
- git and github
- golang
- Google Cloud Certification
- Google Cloud Platform
- Gradle
- grails
- graph
- graphic design
- grep
- Groovy
- gRPC
- Hadoop
- HashMap
- HashSet
- haskell
- Hibernate
- Hibernate interview Question
- homework
- HTML
- HTTP
- HttpClient
- i
- interface
- Internet of Things (IoT)
- interview
- interview questions
- IT Certification
- J2EE
- Jackson
- java
- Java 5 tutorial
- java 7
- Java 8
- java 9
- java basics
- Java Certification
- Java collection tutorial
- java concurrency tutorial
- java design pattern
- Java Enum
- Java file tutorials
- Java Functional Programming
- Java Installation Guide
- Java Interview Question
- Java interview questions
- Java IO interview question
- java io tutorial
- java map tutorials
- java modules
- Java Multithreading Tutorial
- Java networking tutorial
- Java Operator tutorial
- Java programming Tutorial
- Java String tutorial
- Java7
- JavaScript
- JavaScript Interview Question
- JavaScript Tutorial
- JDBC
- JEE Interview Questions
- Jenkins
- JMS
- JPA
- jQuery
- JSON
- JSP
- JSP Interview Question
- JSTL
- JUnit
- JVM
- Keras
- keystore
- Kotlin
- kubernetes
- lambda expression
- Laraval
- learning
- linked list
- Linux
- Log4j
- logging
- Lombok
- LSAT
- Mac OS X
- machine learning
- Mathematics
- Matlab
- Maven
- MERN stack
- Messaging
- Microservices
- Microsoft
- Microsoft Azure Platform
- Microsoft Excel
- Microsoft Power BI
- Mockito
- MongoDB
- MysQL
- MySQL tutorial example
- nested class
- neural network
- Next.js
- NFT
- NLP
- Node.js
- nslookup
- object oriented programming
- OCAJP
- OCMJEA
- OCPJP
- offers
- Oracle
- Perl
- personal development
- Photoshop
- PHP
- pluralsight
- PostgerSQL
- postman
- Powerpoint
- programmers
- programming
- programming problems
- Project Management
- projects
- Prompt Engineering
- Python
- Pytorch
- Quarkus
- questions
- Queue
- R programming
- React
- React Hooks
- react native
- Record
- Recursion
- Redux
- regular expression example
- REST tutorials
- Review
- RoadMap
- Ruby
- Salesforce
- SAT
- Scala
- Scala Interview Questions
- Scanner
- scripting
- Scrum
- Scrum Master Certification
- Selenium
- SEO
- Serialization
- Servlet
- Servlet Interview Questions
- Set
- shell scripting
- smart contracts
- Snowflake SnowPro Certification
- soft link
- soft skills
- software architecture
- Solaris
- Solidity
- Sorting Algorithm
- Spark
- spring boot
- Spring Certification
- spring cloud
- spring data jpa
- spring framework
- spring interview question
- spring mvc
- spring security
- sql
- SQL interview Question
- SQL Joins
- SQL SERVER
- ssl
- Static
- Statistics
- Stream
- String
- Struts
- Swift
- swing
- switch case
- system design
- Tableau
- Tailwind
- TensorFlow
- ternary operator
- testing
- thread
- thread interview questions
- Time series analysis
- Tips
- tomcat
- tools
- tree
- TreeMap
- troubleshooting
- TypeScript
- Udacity
- Udemy
- UI and UX Design
- UML
- unit testing
- Unity 3D
- Unix
- unreal engine
- Video Editing
- Vuejs
- web design
- web development
- web scrapping
- Web Service
- Whizlabs
- Wix
- xml
- YAML
- ZTM Academy
Best System Design and Coding Interview Resources
System Design & Interview Prep
- ByteByteGo Lifetime Plan (50% OFF)
- Codemia Lifetime Plan (60% OFF)
- Exponent Annual Plan (70% OFF)
- Educative Premium Plus (55% OFF)
- DesignGurus All Course Bundle (55% OFF)
- Everything Java Interview Bundle (50% OFF)
- 101 Blockchain (50% OFF)
- Vlad Mihalcea's High Performance Bundle (50% OFF)
- Javarevisited Substack Subscription (50% OFF)
- Head First Software Architecture (Book)
Search This Blog
Best Online Learning Resources and Platforms
- Coursera Plus (40% OFF)
- Datacamp Sale (50% OFF)
- AlgoMonster Lifetime Plan (50% OFF)
- Udemy Sale (80% OFF)
- Baeldung (33% OFF)
- LabEx Sale (50% OFF)
- Codecademy Sale (60% OFF)
- Udacity Sale (50% OFF)
- ZTM Academy Sale (66% OFF)
- Frontend Masters Deal
- Whizlabs Deal (70% OFF)
Javarevisited
Loading...Spring Interview Prep List
- Spring Boot Interview questions
- Spring Cloud Interview questions
- Spring MVC Interview Questions
- Microservices Interview questions
- 10 Spring MVC annotations
- Spring Boot Courses
- Spring Framework Courses
Subscribe for Discounts and Updates
FollowInterview Questions
- core java interview questions
- SQL interview questions
- data structure interview question
- coding interview questions
- java collection interview questions
- java design pattern interview questions
- thread interview questions
- hibernate interview questions
- j2ee interview questions
- Spring Interview Questions
- object oriented programming questions
Followers
Blog Archive
- ► 2025 (554)
- ► December (3)
- ► November (9)
- ► October (72)
- ► September (18)
- ► July (71)
- ► June (103)
- ► May (67)
- ► April (25)
- ► March (18)
- ► February (79)
- ► January (89)
- ► 2024 (192)
- ► December (30)
- ► October (32)
- ► September (31)
- ► August (12)
- ► July (3)
- ► June (2)
- ► May (9)
- ► April (3)
- ► March (28)
- ► February (3)
- ► January (39)
- ► 2022 (164)
- ► December (18)
- ► October (1)
- ► September (1)
- ► August (45)
- ► July (27)
- ► June (11)
- ► May (19)
- ► April (16)
- ► March (12)
- ► February (6)
- ► January (8)
- ► 2021 (104)
- ► December (6)
- ► November (2)
- ► October (13)
- ► September (18)
- ► August (31)
- ► July (34)
- ► 2020 (10)
- ► August (2)
- ► July (1)
- ► June (1)
- ► April (3)
- ► March (1)
- ► February (2)
- ► 2019 (9)
- ► December (1)
- ► November (1)
- ► October (1)
- ► September (1)
- ► July (1)
- ► June (1)
- ► April (3)
- ► 2018 (9)
- ► November (1)
- ► April (8)
- ► 2017 (4)
- ► October (1)
- ► September (2)
- ► April (1)
- ► 2015 (8)
- ► July (1)
- ► June (1)
- ► May (1)
- ► February (5)
- ► 2012 (1)
- ► September (1)
Privacy
- Privacy Policy
- Terms & Conditions
Popular Posts
- 17 Free Java Programing Books for Beginners in 2025 - download, pdf and HTML
- How to fix "illegal start of expression" error in Java? Example
- 5 Examples of Formatting Float or Double Numbers to String in Java
- Top 10 Websites to Learn JavaScript Coding for FREE in 2025 - Best of Lot
- Top 10 Frequently asked SQL Query Interview Questions Answers
Subscribe
Get new posts by email:
SubscribeTag » How To Print An Array
-
Java Program To Write An Array Of Strings To The Output Console
-
Java Program To Print An Array - Programiz
-
What's The Simplest Way To Print A Java Array? - Stack Overflow
-
How To Print Array In Java - Javatpoint
-
Java Program To Print The Elements Of An Array - Javatpoint
-
Java Array Methods – How To Print An Array In Java - FreeCodeCamp
-
Program To Print Array In C - Tutorialspoint
-
How To Print An Array In Python - AskPython
-
How To Print Elements Of An Array In Java - Software Testing Help
-
How To Print An Array In Java - Linux Hint
-
How To Print An Array In Java - Stack Abuse
-
How To Print An Array In Java With Multiple Methods
-
Different Ways To Print The Elements Of An Array In C# - Code Maze
-
How To Print An Array In Java
samiulla
Anonymous