🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the Arrays toString() method in Java programming and how to use it with an example.
1. Arrays toString() Method Overview
Definition:
The Arrays.toString() method returns a string representation of the contents of the specified array. This method is designed for converting arrays that are not deeply nested. For deeply nested arrays, consider using Arrays.deepToString().
Syntax:
String toString(int[] a) // Overloaded for other primitive types and Object
Parameters:
- a: The array whose string representation is to be returned.
Key Points:
- This method is overloaded to handle different types of arrays including arrays of objects and arrays of primitive types.
- Returns "null" if the array is null.
- It provides a clear, comma-separated representation of the array's contents, enclosed in square brackets.
- For arrays of objects, it calls the toString method on each element. If the element is null, it prints "null".
2. Arrays toString() Method Example
import java.util.Arrays;
public class ToStringExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
System.out.println("Numbers array: " + Arrays.toString(numbers));
System.out.println("Names array: " + Arrays.toString(names));
}
}
Output:
Numbers array: [1, 2, 3, 4, 5] Names array: [Alice, Bob, Charlie]
Explanation:
The Arrays.toString() method is utilized to get a string representation of the specified array's contents. In the example, we convert an integer array and a string array to their string representations and print them out. The method provides a clean and readable way to view the contents of an array.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment