Category: 06. Java Generics
-
Bounded Type Parameters
There may be times when you’ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for. To declare a bounded type…
-
Raw Types
A raw type is an object of a generic class or interface if its type arguments are not passed during its creation. Following example will showcase above mentioned concept. Example Create the following java program using any editor of your choice. GenericsTester.java This will produce the following result. Output
-
Parameterized Types
A Generic class can have parameterized types where a type parameter can be substituted with a parameterized type. Following example will showcase above mentioned concept. Example Create the following java program using any editor of your choice. GenericsTester.java This will produce the following result. Output
-
Multiple Type Parameters
A Generic class can have muliple type parameters. Following example will showcase above mentioned concept. Example Create the following java program using any editor of your choice. GenericsTester.java This will produce the following result. Output
-
Methods
You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods − Example Following example illustrates how we can print an array…
-
Type Inference
Type inference represents the Java compiler’s ability to look at a method invocation and its corresponding declaration to check and determine the type argument(s). The inference algorithm checks the types of the arguments and, if available, assigned type is returned. Inference algorithms tries to find a specific type which can fullfill all type parameters. Compiler…
-
Type Parameter Naming Conventions
By convention, type parameter names are named as single, uppercase letters so that a type parameter can be distinguished easily with an ordinary class or interface name. Following is the list of commonly used type parameter names − Following example will showcase above mentioned concept. Example Create the following java program using any editor of…
-
Classes
A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section. The type parameter section of a generic class can have one or more type parameters separated by commas. These classes are known as parameterized classes or parameterized types because they accept one or…
-
Environment Setup
Local Environment Setup JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine. System Requirement JDK 1.5 or above. Memory No minimum requirement. Disk Space No minimum requirement. Operating System No minimum requirement. Step 1: Verify Java Installation in Your Machine First of all, open the…
-
Overview
It would be nice if we could write a single sort method that could sort the elements in an Integer array, a String array, or an array of any type that supports ordering. Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with…