What are Generics? Pls Explain

#2
Generics:
It allows us to provide type safety ...
If we want particular type we may use this concept..

Simple array list Pgm
Import java.util.*;
Class Test {
public static void main(String args[]) {
ArrayList a=new ArrayList();
a.add("a");
a.add("b");
a.add("c");
a.add("43");
a.add("45");
System.out.println(a);
System.out.println(a.size()); } }

Pgm by using Generics

Import java.util.*;
Class Test {
public static void main(String args[]) {
ArrayList<Integer> a=new ArrayList<Integer>();/* Now by writing Integer it accepts only Integer values not char values */
a.add("a");
a.add("b");
a.add("c");
a.add("43");
a.add("45");
System.out.println(a);
System.out.println(a.size()); } }

Output:
[43,45]
 
#3
This will give compilation error at
a.add("a");
a.add("b");
a.add("c");
a.add("43");
a.add("45");

Because you are adding String but this generic of type Integer
i.e
ArrayList<Integer> a=new ArrayList<Integer>();
it willl allow only Integer .Integer can be primitive or wrapper

Example :
a.add(11);
a.add(new Integer(16));
 
#4
we can specify types of object going to store in the collection . so when we retrieve the object no need to type cast .

Example :


List<String> strings = new ArrayList<String>();

strings.add("a String");

String aString = strings.get(0);
 
#5
Generics came in java from the Generic Programming Style of computer programming under it the algorithm is designed in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameter..
for this in java 5 in 2004, sun micro-system took this concept to make the OOPS in java more dynamic and developer-helping..

Generics helps the programmer to store the data into a collection-type of similar fashion only ... the benefit is that , it gives compile time safety to the source code in the big project ...

We have to put <>(diamond) having data type(s)..



Example:
List<String> v = new ArrayList<String>();

v.add("ABC"); // OK

v.add(new Integer(10)); // not OK, error in this fashion



hope this helps..
byee
 
#6
When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
 
#7
In Java, "Generics" are parameters for sorts. This may sound befuddling yet here is a basic clarification. On the off chance that you were making a class that numerous different classes will acquire from, you need to make it wide and adaptable. So you would utilize a "Non specific" sort to make case factors.
 
Top