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 

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

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
include_once 'codzify-footer.php'; ?>