Generic Methods in Java

Article by: Manish Methani

Last Updated: October 23, 2021 at 8:04am IST
1 min 17 sec read

What are generic methods ?

Suppose you have an integer array of 4 elements

Integer[] iray = {10,20,30,40};

And one character array of 4 characters

Character[] cray = {'c','o','d','z'}

Now if you want to print the elements in both of these arrays you have to write two different print methods. And Suppose in future there is also Float[] array and double[] array too. Then think you have to write a print method to display the elements of array four times.

So here, Generic Methods comes into existence. One method can handle different types . Thats why it is called as generic Type.

Program before using Generic Methods

import java.util.*;

public class JavaGenericMethods {
  public static void main(String[] args)
  {
    Integer[] iray = {10,20,30,40};
    Character[] cray = {'c','o','d','z'};
    Double[] dray = {10.2,12.3,14.6};
    
    printElements(iray);
    printElements(cray);
    printElements(dray);
  }

  
  public static void printElements(Integer[] i)
  {
    for(Integer x: i )
    {
      System.out.printf("%s ", x);
    }
    System.out.println();
  }
  
  public static void printElements(Character[] c)
  {
    for(Character x: c )
    {
      System.out.printf("%s ", x);
    }
    System.out.println();
  }
  
  public static void printElements(Double[] i)
  {
    for(Double x: i )
    {
      System.out.printf("%s ", x);
    }
    System.out.println();
  }
}

Output :-

10 20 30 40 
c o d z 
10.2 12.3 14.6 

Program after using Generic Methods

import java.util.*;

public class JavaGenericMethods {
  public static void main(String[] args)
  {
    Integer[] iray = {10,20,30,40};
    Character[] cray = {'c','o','d','z'};
    Double[] dray = {10.2,12.3,14.6};
    
    printElements(iray);
    printElements(cray);
    printElements(dray);
  }

  
  public static  void printElements(T[] i)
  {
    for(T x: i )
    {
      System.out.printf("%s ", x);
    }
    System.out.println();
  }
}

Output :-

10 20 30 40 
c o d z 
10.2 12.3 14.6 

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer