<s>
Generics	O
are	O
a	O
facility	O
of	O
generic	B-Language
programming	I-Language
that	O
were	O
added	O
to	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
in	O
2004	O
within	O
version	O
J2SE	B-Language
5.0	O
.	O
</s>
<s>
They	O
were	O
designed	O
to	O
extend	O
Java	B-Language
's	O
type	O
system	O
to	O
allow	O
"	O
a	O
type	O
or	O
method	O
to	O
operate	O
on	O
objects	O
of	O
various	O
types	O
while	O
providing	O
compile-time	B-Language
type	I-Language
safety	I-Language
"	O
.	O
</s>
<s>
The	O
aspect	O
compile-time	B-Language
type	I-Language
safety	I-Language
was	O
not	O
fully	O
achieved	O
,	O
since	O
it	O
was	O
shown	O
in	O
2016	O
that	O
it	O
is	O
not	O
guaranteed	O
in	O
all	O
cases	O
.	O
</s>
<s>
The	O
Java	B-Language
collections	I-Language
framework	I-Language
supports	O
generics	O
to	O
specify	O
the	O
type	O
of	O
objects	O
stored	O
in	O
a	O
collection	O
instance	O
.	O
</s>
<s>
In	O
1998	O
,	O
Gilad	O
Bracha	O
,	O
Martin	O
Odersky	O
,	O
David	O
Stoutamire	O
and	O
Philip	O
Wadler	O
created	O
Generic	B-Language
Java	I-Language
,	O
an	O
extension	O
to	O
the	O
Java	B-Language
language	I-Language
to	O
support	O
generic	B-Language
types	I-Language
.	O
</s>
<s>
Generic	B-Language
Java	I-Language
was	O
incorporated	O
in	O
Java	B-Language
with	O
the	O
addition	O
of	O
wildcards	B-Language
.	O
</s>
<s>
According	O
to	O
Java	B-Language
Language	I-Language
Specification	I-Language
:	O
</s>
<s>
A	O
type	B-Application
variable	I-Application
is	O
an	O
unqualified	O
identifier	O
.	O
</s>
<s>
Type	B-Application
variables	I-Application
are	O
introduced	O
by	O
generic	B-Language
class	I-Language
declarations	O
,	O
generic	O
interface	B-Language
declarations	O
,	O
generic	O
method	O
declarations	O
,	O
and	O
by	O
generic	O
constructor	O
declarations	O
.	O
</s>
<s>
A	O
class	O
is	O
generic	O
if	O
it	O
declares	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
.	O
</s>
<s>
These	O
type	B-Application
variables	I-Application
are	O
known	O
as	O
the	O
type	B-Language
parameters	I-Language
of	O
the	O
class	O
.	O
</s>
<s>
It	O
defines	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
that	O
act	O
as	O
parameters	O
.	O
</s>
<s>
A	O
generic	B-Language
class	I-Language
declaration	O
defines	O
a	O
set	O
of	O
parameterized	B-Language
types	I-Language
,	O
one	O
for	O
each	O
possible	O
invocation	O
of	O
the	O
type	B-Language
parameter	I-Language
section	O
.	O
</s>
<s>
All	O
of	O
these	O
parameterized	B-Language
types	I-Language
share	O
the	O
same	O
class	O
at	O
runtime	O
.	O
</s>
<s>
An	O
interface	B-Language
is	O
generic	O
if	O
it	O
declares	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
.	O
</s>
<s>
These	O
type	B-Application
variables	I-Application
are	O
known	O
as	O
the	O
type	B-Language
parameters	I-Language
of	O
the	O
interface	B-Language
.	O
</s>
<s>
It	O
defines	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
that	O
act	O
as	O
parameters	O
.	O
</s>
<s>
A	O
generic	O
interface	B-Language
declaration	O
defines	O
a	O
set	O
of	O
types	O
,	O
one	O
for	O
each	O
possible	O
invocation	O
of	O
the	O
type	B-Language
parameter	I-Language
section	O
.	O
</s>
<s>
All	O
parameterized	B-Language
types	I-Language
share	O
the	O
same	O
interface	B-Language
at	O
runtime	O
.	O
</s>
<s>
A	O
method	O
is	O
generic	O
if	O
it	O
declares	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
.	O
</s>
<s>
These	O
type	B-Application
variables	I-Application
are	O
known	O
as	O
the	O
formal	O
type	B-Language
parameters	I-Language
of	O
the	O
method	O
.	O
</s>
<s>
The	O
form	O
of	O
the	O
formal	O
type	B-Language
parameter	I-Language
list	O
is	O
identical	O
to	O
a	O
type	B-Language
parameter	I-Language
list	O
of	O
a	O
class	O
or	O
interface	B-Language
.	O
</s>
<s>
A	O
constructor	O
is	O
generic	O
if	O
it	O
declares	O
one	O
or	O
more	O
type	B-Application
variables	I-Application
.	O
</s>
<s>
These	O
type	B-Application
variables	I-Application
are	O
known	O
as	O
the	O
formal	O
type	B-Language
parameters	I-Language
of	O
the	O
constructor	O
.	O
</s>
<s>
The	O
form	O
of	O
the	O
formal	O
type	B-Language
parameter	I-Language
list	O
is	O
identical	O
to	O
a	O
type	B-Language
parameter	I-Language
list	O
of	O
a	O
generic	B-Language
class	I-Language
or	O
interface	B-Language
.	O
</s>
<s>
The	O
following	O
block	O
of	O
Java	B-Language
code	I-Language
illustrates	O
a	O
problem	O
that	O
exists	O
when	O
not	O
using	O
generics	O
.	O
</s>
<s>
Although	O
the	O
code	O
is	O
compiled	O
without	O
error	O
,	O
it	O
throws	O
a	O
runtime	O
exception	O
(	O
java.lang.ClassCastException	O
)	O
when	O
executing	O
the	O
third	O
line	O
of	O
code	O
.	O
</s>
<s>
This	O
type	O
of	O
logic	O
error	O
can	O
be	O
detected	O
during	O
compile	B-Application
time	I-Application
by	O
using	O
generics	O
and	O
is	O
the	O
primary	O
motivation	O
for	O
using	O
them	O
.	O
</s>
<s>
The	O
type	B-Language
parameter	I-Language
String	O
within	O
the	O
angle	O
brackets	O
declares	O
the	O
ArrayList	O
to	O
be	O
constituted	O
of	O
String	O
(	O
a	O
descendant	O
of	O
the	O
ArrayList	O
's	O
generic	O
Object	O
constituents	O
)	O
.	O
</s>
<s>
The	O
logical	O
flaw	O
in	O
the	O
third	O
line	O
of	O
this	O
fragment	O
will	O
be	O
detected	O
as	O
a	O
compile-time	B-Application
error	I-Application
(	O
with	O
J2SE	B-Language
5.0	O
or	O
later	O
)	O
because	O
the	O
compiler	O
will	O
detect	O
that	O
v.get(0 )	O
returns	O
String	O
instead	O
of	O
Integer	O
.	O
</s>
<s>
A	O
type	O
argument	O
for	O
a	O
parameterized	B-Language
type	I-Language
is	O
not	O
limited	O
to	O
a	O
concrete	O
class	O
or	O
interface	B-Language
.	O
</s>
<s>
Java	B-Language
allows	O
the	O
use	O
of	O
type	O
wildcards	B-Language
to	O
serve	O
as	O
type	O
arguments	O
for	O
parameterized	B-Language
types	I-Language
.	O
</s>
<s>
Wildcards	B-Language
are	O
type	O
arguments	O
in	O
the	O
form	O
"	O
<?>	O
"	O
;	O
optionally	O
with	O
an	O
upper	O
or	O
lower	O
bound	O
.	O
</s>
<s>
Given	O
that	O
the	O
exact	O
type	O
represented	O
by	O
a	O
wildcard	B-Language
is	O
unknown	O
,	O
restrictions	O
are	O
placed	O
on	O
the	O
type	O
of	O
methods	O
that	O
may	O
be	O
called	O
on	O
an	O
object	O
that	O
uses	O
parameterized	B-Language
types	I-Language
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
where	O
the	O
element	O
type	O
of	O
a	O
Collectionxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
is	O
parameterized	O
by	O
a	O
wildcard	B-Language
:	O
</s>
<s>
The	O
add( )	O
method	O
takes	O
arguments	O
of	O
type	O
E	O
,	O
the	O
element	O
type	O
of	O
the	O
Collectionxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
generic	O
interface	B-Language
.	O
</s>
<s>
To	O
specify	O
the	O
upper	O
bound	O
of	O
a	O
type	O
wildcard	B-Language
,	O
the	O
extends	O
keyword	O
is	O
used	O
to	O
indicate	O
that	O
the	O
type	O
argument	O
is	O
a	O
subtype	O
of	O
the	O
bounding	O
class	O
.	O
</s>
<s>
The	O
use	O
of	O
wildcards	B-Language
above	O
adds	O
flexibility	O
since	O
there	O
is	O
not	O
any	O
inheritance	O
relationship	O
between	O
any	O
two	O
parameterized	B-Language
types	I-Language
with	O
concrete	O
type	O
as	O
type	O
argument	O
.	O
</s>
<s>
If	O
it	O
did	O
,	O
it	O
would	O
be	O
possible	O
to	O
insert	O
a	O
Number	O
that	O
is	O
not	O
an	O
Integer	O
into	O
it	O
;	O
which	O
violates	O
type	B-Language
safety	I-Language
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
that	O
demonstrates	O
how	O
type	B-Language
safety	I-Language
would	O
be	O
violated	O
if	O
Listxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
:	O
</s>
<s>
The	O
solution	O
with	O
wildcards	B-Language
works	O
because	O
it	O
disallows	O
operations	O
that	O
would	O
violate	O
type	B-Language
safety	I-Language
:	O
</s>
<s>
To	O
specify	O
the	O
lower	O
bounding	O
class	O
of	O
a	O
type	O
wildcard	B-Language
,	O
the	O
super	O
keyword	O
is	O
used	O
.	O
</s>
<s>
The	O
mnemonic	O
PECS	O
(	O
Producer	O
Extends	O
,	O
Consumer	O
Super	O
)	O
from	O
the	O
book	O
Effective	O
Java	B-Language
by	O
Joshua	O
Bloch	O
gives	O
an	O
easy	O
way	O
to	O
remember	O
when	O
to	O
use	O
wildcards	B-Language
(	O
corresponding	O
to	O
covariance	O
and	O
contravariance	O
)	O
in	O
Java	B-Language
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
a	O
generic	B-Language
Java	I-Language
class	O
,	O
which	O
can	O
be	O
used	O
to	O
represent	O
individual	O
entries	O
(	O
key	O
to	O
value	O
mappings	O
)	O
in	O
a	O
map	B-Application
:	O
</s>
<s>
This	O
generic	B-Language
class	I-Language
could	O
be	O
used	O
in	O
the	O
following	O
ways	O
,	O
for	O
example	O
:	O
</s>
<s>
Thanks	O
to	O
type	O
inference	O
,	O
Java	B-Language
SE	I-Language
7	O
and	O
above	O
allow	O
the	O
programmer	O
to	O
substitute	O
an	O
empty	O
pair	O
of	O
angle	O
brackets	O
(	O
<>	O
,	O
called	O
the	O
diamond	O
operator	O
)	O
for	O
a	O
pair	O
of	O
angle	O
brackets	O
containing	O
the	O
one	O
or	O
more	O
type	B-Language
parameters	I-Language
that	O
a	O
sufficiently-close	O
context	O
implies	O
.	O
</s>
<s>
Here	O
is	O
an	O
example	O
of	O
a	O
generic	O
method	O
using	O
the	O
generic	B-Language
class	I-Language
above	O
:	O
</s>
<s>
In	O
many	O
cases	O
the	O
user	O
of	O
the	O
method	O
need	O
not	O
indicate	O
the	O
type	B-Language
parameters	I-Language
,	O
as	O
they	O
can	O
be	O
inferred	O
:	O
</s>
<s>
Generics	O
are	O
checked	O
at	O
compile-time	B-Application
for	O
type-correctness	O
.	O
</s>
<s>
The	O
generic	B-Language
type	I-Language
information	O
is	O
then	O
removed	O
in	O
a	O
process	O
called	O
type	B-General_Concept
erasure	I-General_Concept
.	O
</s>
<s>
The	O
compile-time	B-Application
check	O
guarantees	O
that	O
the	O
resulting	O
code	O
is	O
type-correct	O
.	O
</s>
<s>
Because	O
of	O
type	B-General_Concept
erasure	I-General_Concept
,	O
type	B-Language
parameters	I-Language
cannot	O
be	O
determined	O
at	O
run-time	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
an	O
ArrayList	O
is	O
examined	O
at	O
runtime	O
,	O
there	O
is	O
no	O
general	O
way	O
to	O
determine	O
whether	O
,	O
before	O
type	B-General_Concept
erasure	I-General_Concept
,	O
it	O
was	O
an	O
ArrayListxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1	O
.	O
</s>
<s>
Another	O
effect	O
of	O
type	B-General_Concept
erasure	I-General_Concept
is	O
that	O
a	O
generic	B-Language
class	I-Language
cannot	O
extend	O
the	O
Throwable	O
class	O
in	O
any	O
way	O
,	O
directly	O
or	O
indirectly	O
:	O
</s>
<s>
The	O
reason	O
why	O
this	O
is	O
not	O
supported	O
is	O
due	O
to	O
type	B-General_Concept
erasure	I-General_Concept
:	O
</s>
<s>
Due	O
to	O
type	B-General_Concept
erasure	I-General_Concept
,	O
the	O
runtime	O
will	O
not	O
know	O
which	O
catch	O
block	O
to	O
execute	O
,	O
so	O
this	O
is	O
prohibited	O
by	O
the	O
compiler	O
.	O
</s>
<s>
Java	B-Language
generics	I-Language
differ	O
from	O
C++	B-Application
templates	I-Application
.	O
</s>
<s>
Java	B-Language
generics	I-Language
generate	O
only	O
one	O
compiled	O
version	O
of	O
a	O
generic	B-Language
class	I-Language
or	O
function	O
regardless	O
of	O
the	O
number	O
of	O
parameterizing	O
types	O
used	O
.	O
</s>
<s>
Furthermore	O
,	O
the	O
Java	B-Language
run-time	O
environment	O
does	O
not	O
need	O
to	O
know	O
which	O
parameterized	B-Language
type	I-Language
is	O
used	O
because	O
the	O
type	O
information	O
is	O
validated	O
at	O
compile-time	B-Application
and	O
is	O
not	O
included	O
in	O
the	O
compiled	O
code	O
.	O
</s>
<s>
Consequently	O
,	O
instantiating	O
a	O
Java	B-Language
class	O
of	O
a	O
parameterized	B-Language
type	I-Language
is	O
impossible	O
because	O
instantiation	O
requires	O
a	O
call	O
to	O
a	O
constructor	O
,	O
which	O
is	O
unavailable	O
if	O
the	O
type	O
is	O
unknown	O
.	O
</s>
<s>
Because	O
there	O
is	O
only	O
one	O
copy	O
per	O
generic	B-Language
class	I-Language
at	O
runtime	O
,	O
static	B-General_Concept
variables	I-General_Concept
are	O
shared	O
among	O
all	O
the	O
instances	O
of	O
the	O
class	O
,	O
regardless	O
of	O
their	O
type	B-Language
parameter	I-Language
.	O
</s>
<s>
Consequently	O
,	O
the	O
type	B-Language
parameter	I-Language
cannot	O
be	O
used	O
in	O
the	O
declaration	O
of	O
static	B-General_Concept
variables	I-General_Concept
or	O
in	O
static	O
methods	O
.	O
</s>
<s>
Project	B-Language
Valhalla	I-Language
is	O
an	O
experimental	O
project	O
to	O
incubate	O
improved	O
Java	B-Language
generics	I-Language
and	O
language	O
features	O
,	O
for	O
future	O
versions	O
potentially	O
from	O
Java	B-Language
10	O
onwards	O
.	O
</s>
