- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use overloaded methods to print array of different types in Java
In method overloading, the class can have multiple methods with the same name but the parameter list of the methods should not be the same.
Overloaded methods can be used to print an array of different types in Java by making sure that the parameter list of the methods contains different types of arrays that can be printed by the method.
A program that demonstrates this is given as follows −
Example
public class Demo { public static void arrPrint(Integer[] arr) { System.out.print("
The Integer array is: "); for (Integer i : arr) System.out.print(i + " "); } public static void arrPrint(Character[] arr) { System.out.print("
The Character array is: "); for (Character i : arr) System.out.print(i + " "); } public static void arrPrint(String[] arr) { System.out.print("
The String array is: "); for (String i : arr) System.out.print(i + " "); } public static void arrPrint(Double[] arr) { System.out.print("
The Double array is: "); for (Double i : arr) System.out.print(i + " "); } public static void main(String args[]) { Integer[] iarr = { 8, 1, 5, 3, 9 }; Character[] carr = { 'A', 'B', 'C', 'D', 'E' }; String[] sarr = { "Jane", "Amy", "John", "Tim", "Sara" }; Double[] darr = { 7.3, 5.9, 2.5, 3.7, 1.4 }; arrPrint(iarr); arrPrint(carr); arrPrint(sarr); arrPrint(darr); } }
Output
The Integer array is: 8 1 5 3 9 The Character array is: A B C D E The String array is: Jane Amy John Tim Sara The Double array is: 7.3 5.9 2.5 3.7 1.4
- Related Articles
- Java Program to Use Different Types of a Collection
- Hiding of all overloaded methods in base class in C++
- Golang program to use different types of a collection
- Different Methods to find Prime Number in Java
- Different methods to append a single character to a string or char array in Java
- Types of Array in Java
- When to use vararg methods in Java?
- C++ program to print values of different data types provided as input
- Different ways to print exception messages in Java
- What are the different types of keywords in Java?
- What are the different types of classes in Java?
- What is the use of default methods in Java?
- Pass long parameter to an overloaded method in Java
- What are the different ways for a method to be overloaded in C#?
- How to print a byte array in Java?

Advertisements