From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Generic methods in Java

Generic methods in Java - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Generic methods in Java

- [Instructor] Generic methods in Java are methods that allow you to create a new type parameter just for that method. This is useful if you are writing a method but want to be flexible about the type of objects you can pass in. In this example, I have an array of characters called charArray. I also have an array of booleans called boolArray, and an array of integers called intArray. Then, I have created a method called arrayToList that iterates through all of the objects in an array and adds them to a list. I want to be able to pass any of these arrays into the method, even though they are of different types. So the first thing I have tried is making the arguments of the array to list method object types. In Java, object is a parent type of all other object types, so I can pass in arrays and lists of any types to this method. In the main method, I have used the array to list method to create a list of characters from charArray, a list of booleans from boolArray, and a list of…

Contents