We've moved! — MindVault360 is now HexLab. Better design, more content & premium notes.

Visit HexLab →

MindVault360 has moved!

We've upgraded to HexLab — a faster, more professional platform with better content, premium notes, and a modern design.

Visit us at hexlab

Monday, January 6, 2025

Genric Methods


(Tap the post to see more)


Generic allows us to write a method that can work with different types while providing compile time safely.


Example program:

package com.Genric;

public class GenericMethods {


    // Generic method example

    public <T> void printArray(T[] array) {

        for (T element : array) {

            System.out.print(element + " ");

        }

        System.out.println();

    }


    public static void main(String[] args) {

        // Example usage of the generic method

        Integer[] intArray = {1, 2, 3, 4, 5};

        String[] stringArray = {"one", "two", "three", "four", "five"};


        GenericMethods genericMethods = new GenericMethods();

        genericMethods.printArray(intArray);

        genericMethods.printArray(stringArray);

    }

}


← Back Next →

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home