Java list of generic types
Generics in java were introduced as one of features in JDK 5. Personally, I find the angular brackets <> used in generics very fascinating and it always forces me to have another thought where I use it OR see it written in somebody elses code. To be very frank, I have been using generics since a long time now but still I feel not fully confident to use it blindly. In this tutorial, I will be covering everything I find useful with java generics, and things related to them. If you think that I can use more precise words at any part of the tutorial, or an example can be added or simply you do not agree with me; drop me a comment. I will be glad to know your point of view. Show Java Generics is a technical term denoting a set of language features related to the definition and use of generic types and methods . In java, Generic types or methods differ from regular types and methods in that they have type parameters.
Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. A class like LinkedList 1) Why Generics?If you closely look at java collection framework classes then you will observe that most classes take parameter/argument of type Object and return values from methods as Object. Now, in this form, they can take any java type as argument and return the same. They are essentially heterogeneous i.e. not of a particular similar type. Programmers like us often wanted to specify that a collection contains elements only of a certain type e.g. Integer or String or Employee. In the original collection framework, having homogeneous collections was not possible without adding extra checks before adding some checks in code. Generics were introduced to remove this limitation to be very specific. They add this type checking of parameters in your code at compile-time, automatically. This saves us writing a lot of unnecessary code which actually does not add any value in run-time if written correctly.
Without this type of safety, your code could have infected by various bugs that get revealed only in runtime. Using generics, makes them highlighted in compile time itself and make you code robust even before you get the bytecode of your java source code files.
So now we have a fair idea of why generics are present in java in the first place. The next step is to get some knowledge about how they work in java. What actually happens when you use generics in your source code. 2) How Generics works in JavaIn the heart of generics is type safety. What exactly is type safety? Its just a guarantee by compiler that if correct Types are used in correct places then there should not be any ClassCastException in runtime. A usecase can be list of Integer i.e. List Another important term in java generics is type erasure. It essentially means that all the extra information added using generics into source code will be removed from bytecode generated from it. Inside bytecode, it will be old java syntax which you will get if you dont use generics at all. This necessarily helps in generating and executing code written prior to java 5 when generics were not added in language. Lets understand with an example. ListWhen you write above code and compile it, you will get below error: The method add(Integer) in the type List The second part is getting the byte code after removing the second line from the above example. If you compare the bytecode of the above example with/without generics, then there will not be any different. Clearly compiler removed all generics information. So, the above code is very much similar to the below code without generics. List list = new ArrayList(); list.add(1000);
3) Types of Generics?Now we have some understanding of what generics are all about. Now start exploring other important concepts revolving around generics. I will start by identifying the various ways, generics can be applied into sourcecode. Generic Type Class or InterfaceA class is generic if it declares one or more type variables. These type variables are known as the type parameters of the class. Lets understand with an example. DemoClass is a simple java class, which has one property t (can be more than one also); and type of property is Object. class DemoClass { private Object t; public void set(Object t) { this.t = t; } public Object get() { return t; } }Here we want that once initialized the class with a certain type, class should be used with that particular type only. e.g. If we want one instance of class to hold value t of type String, then programmer should set and get the only String type. Since we have declared property type to Object, there is no way to enforce this restriction. A programmer can set any object, and can expect any return value type from get method since all java types are subtypes of Object class. To enforce this type restriction, we can use generics as below: class DemoClassNow we can be assured that class will not be misused with wrong types. A sample usage of DemoClass will look like this: DemoClassThe above analogy is true for the interfaces as well. Lets quickly look at an example to understand, how generics type information can be used in interfaces in java. //Generic interface definition interface DemoInterfaceI hope, I was enough clear to put some light on generic classes and interfaces. Now its time to look at generic methods and constructors. Generic Type Method or ConstructorGeneric methods are much similar to generic classes. They are different only in one aspect that the scope of type information is only inside the method (or constructor). Generic methods are methods that introduce their own type parameters. Lets understand this with an example. Below is a code sample of a generic method that can be used to find all occurrences of a type parameter in a list of variables of that type only. public staticIf you pass a list of String and another string to search in this method, it will work fine. But if you will try to find an Number into list of String, it will give compile-time error. The same as above can be an example of a generic constructor. Lets take a separate example for a generic constructor as well. class DimensionIn this example, Dimension classs constructor has the type information also. So you can have an instance of dimension with all attributes of a single type only. 4) Generic Type ArraysArray in any language have same meaning i.e. an array is a collection of similar type of elements. In java, pushing any incompatible type in an array on runtime will throw ArrayStoreException. It means array preserve their type information in runtime, and generics use type erasure or remove any type of information in runtime. Due to the above conflict, instantiating a generic array in java is not permitted. public class GenericArrayIn the same line as above generic type classes and methods, we can have generic arrays in java. As we know that an array is a collection of similar type of elements and pushing any incompatible type will throw ArrayStoreException in runtime; which is not the case with Collection classes. Object[] array = new String[10]; array[0] = "lokesh"; array[1] = 10; //This will throw ArrayStoreExceptionThe above mistake is not very hard to make. It can happen anytime. So its better to provide the type information to array also so that error is caught at compile time itself. Another reason why arrays do not support generics is that arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is, Object[] is a supertype of String[] and a string array can be accessed through a reference variable of type Object[]. Object[] objArr = new String[10]; // fine objArr[0] = new String();5) Generics with WildcardsIn generic code, the question mark (?), called the wildcard, represents an unknown type. A wildcard parameterized type is an instantiation of a generic type where at least one type argument is a wildcard. Examples of wildcard parameterized types are Collection and Pair Having wild cards at difference places have different meanings as well. e.g.
A wildcard parameterized type is not a concrete type that could appear in a new expression. It just hints the rule enforced by java generics that which types are valid in any particular scenario where wild cards have been used. For example, below are valid declarations involving wild cards: Collection coll = new ArrayListAnd below are not valid uses of wildcards, and they will give compile-time error. List list = new ArrayListWildcards in generics can be unbounded as well as bounded. Lets identify the difference in various terms. Unbounded wildcard parameterized typeA generic type where all type arguments are the unbounded wildcard "? without any restriction on type variables. e.g. Bounded wildcard parameterized typeBounded wildcards put some restrictions over possible types, you can use to instantiate a parametrized type. This restriction is enforced using keywords super and extends. To differentiate more clearly, lets divide them into upper bounded wildcards and lower bounded wildcards. Upper bounded wildcardsFor example, say you want to write a method that works on List Lower bounded wildcardsIf you want a generic expression to accept all types which are super type of a particular type OR parent class of a particular class then you will use a lower bound wildcard for this purpose, using super keyword. In below given example, I have created three classes i.e. SuperClass, ChildClass and GrandChildClass. There relationship is shown in code below. Now, we have to create a method which somehow get a GrandChildClass information (e.g. from DB) and create an instance of it. And we want to store this new GrandChildClass in an already existing list of GrandChildClasses. Here problem is that GrandChildClass is subtype of ChildClass and SuperClass as well. So any generic list of SuperClasses and ChildClasses is capable of holding GrandChildClasses as well. Here we must take help of lower bound wildcard using super keyword. package test.core; import java.util.ArrayList; import java.util.List; public class GenericsExample6) What is not allowed to do with Generics?So far we have learned about a number of things which you can do with generics in java to avoid many ClassCastException instances in your application. We also saw the usage of wildcards as well. Now its time to identify some tasks which are not allowed to do in java generics. a) You cant have static field of typeYou can not define a static generic parameterized member in your class. Any attempt to do so will generate compile-time error: Cannot make a static reference to the non-static type T. public class GenericsExampleb) You can not create an instance of TAny attempt to create an instance of T will fail with error: Cannot instantiate the type T. public class GenericsExamplec) Generics are not compatible with primitives in declarationsYes, its true. You cant declare generic expression like List or Map d) You cant create Generic exception classSometimes, the programmer might be in need of passing an instance of generic type along with exception being thrown. This is not possible to do in Java. // causes compiler error public class GenericExceptionWhen you try to create such an exception, you will end up with message like this: The generic class GenericException may not subclass java.lang.Throwable. Thats all for now closing the discussion on java generics this time. I will come up with more interesting facts and features related to generics in the coming posts. Drop me a comment if something is unclear /OR you have any other questions. Happy Learning !! Was this post helpful?Let us know if you liked the post. Thats the only way we can improve. Yes No |