Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.
Generics are a facility of generic programming that were added to the Java p...
Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.
Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow “a type or method to operate on objects of various types while providing compile-time type safety”
The Java collections framework supports generics to specify the type of objects stored in a collection instance.
Consider the following example:
Although the code is compiled without error, it throws a runtime exception (java.lang.ClassCastException) when
executing the second last statement of code. This type of problem can be avoided by using generics
With Generics:
Compiling this fragment with J2SE 5.0 (or later) will yield acompile-timeerror because the
compiler will detect thatlist.get(2)returnsStringinstead ofInteger
Entry<String, String> grade = newEntry<String, String>("Mike", "A");
Entry<String, Integer> mark = newEntry<String, Integer>("Mike", 100);
System.out.println("grade: " + grade);
System.out.println("mark: " + mark);
Entry<Integer, Boolean> prime =
newEntry<>(13, true); //Diamond Operator
if(prime.getValue())
System.out.println(prime.getKey() + " is prime.");
else
System.out.println(prime.getKey() + " is not prime.");
Output:
grade: (Mike, A)
mark: (Mike, 100)
13 is prime.
Note: If we remove the first<Type>in the above method, we will get compilation
error (cannot find symbol 'Type') since it represents the declaration of the symbol.
In many cases the user of the method need not indicate the type parameters, as
they can be inferred:
Entry<String, String> pair = Entry.twice("Hello");
The parameters can be explicitly added if needed:
Entry<String, String> pair = Entry.<String>twice("Hello");
Programs that uses Generics has got many benefits over non-generic code.
1.Code Reuse: We can write a method/class/interface once and use for any type we want.
2.Type Safety : Generics make errors to appear compile time than at run time (It’s always
better to know problems in your code at compile time rather than making your code fail
at run time).
3.Individual Type Casting is not required
ArrayList<String> al= new ArrayList<String> ();
al.add(“Gurpreet");
// Typecasting is not needed
String s1 = al.get(0);
4. Implementing generic algorithms: By using generics, we can implement algorithms that
work on different types of objects and at the same they are type safe too.
What is Collection in java?
Collection represents a single unit of objects i.e. a group.
What is framework in java?
•provides readymade architecture.
•represents set of classes and interface.
•is optional.
What is Collection framework?
Collection framework represents a unified architecture for storing and manipulating group of
objects. It has:
•Interfaces and its implementations i.e. classes
•Algorithm