Looking for ideas on writing about this topic Assessment of

Looking for ideas on writing about this topic;

Assessment of Java generics.

Solution

Generics

In java 5 genrics was introduced first time. Now it is the most widely used features of java language.
It is used to write program which comprises of only a single type data driven automatically.
Using Generics, it becomes possible to create a single class or method that automatically works with all types of data(Integer, String, Float etc). It also helps in code reuseability and code safety
________________________________________
Example of Generic class
class Genrics <R>
{
R ob; //an object of type R is declared
Genrics(R o) //constructor of class
{
ob = o;
}
public R getOb()
{
return ob;
}
}

class Test
{
public static void main (String[] args)
{
Gen < Integer> iob = new Genrics(50); //instance of Integer type Genrics Class.
int x = iob.getOb();
System.out.print in(x);
Gen < String> sob = new Genrics(\"Hello World\"); //instance of String type Genrics Class.
String str = sob.getOb();
}
}
Output :
50
Hello World
________________________________________
Generics Work Only with Objects
Generics works only with objects you cannor use primative datatype with genrics
Gen< int> iOb = new Genrics< int>(07); //Error, can\'t use primitive type
________________________________________
Generics Types of different Type Arguments are never same
In the example above we created two objects of class Genrics, one of type Integer, and other of type String
If type argument are different we cannot refer a genric type with other
iob = sob; //Absolutely Wrong
________________________________________
Generic Methods
You can also create generic methods that can be called with different types of arguments
________________________________________
Example of Generic method
class GenricsTest
{
static < V, T> void display (V v, T t)
{
System.out.println(v.getClass().getName()+\" = \" +v);
System.out.println(t.getClass().getName()+\" = \" +t);
}
public static void main(String[] args)
{
display(88,\"This is genric string\");
}
}
Output :
java lang.Integer = 88
Java lang.String = this is string
________________________________________
Generic Constructors
You can create genric constructor even class is not generic
Example of Generic Constructor
class Genrics
{
private double value;
< R extends Number> Gen(R ob)
{
value=ob.doubleValue();
}
void show()
{
System.out.println(value);
}
}

class Test
{
public static void main(String[] args)
{
Gen g = new Genrics(101);
Gen g1 = new Genrics(111.1f);
g.show();
g1.show();
}
}
Output :
101.0
111.1
________________________________________
Generic Interface
Like classes and methods, you can also create generic interfaces.
interface GenricsInterface< R>
{ .. }

Looking for ideas on writing about this topic; Assessment of Java generics.SolutionGenerics In java 5 genrics was introduced first time. Now it is the most wide
Looking for ideas on writing about this topic; Assessment of Java generics.SolutionGenerics In java 5 genrics was introduced first time. Now it is the most wide

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site