Java Generics: Bounded/Unbounded Wildcards, Raw Types, Ensure & Restrictions, Slides of Java Programming

An in-depth exploration of java generics, focusing on the concepts of bounded and unbounded wildcards, raw types, and ensure and restrictions. It covers the importance of wildcards, the differences between bounded and unbounded wildcards, the use of lower and upper bounds, and the advantages of using generic types instead of raw types. The document also explains the concept of type erasure and compile-time checking, as well as the restrictions on generics such as creating instances of generic types, generic array creation, and using generic types in static contexts.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

60 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B
ou
n
ded
Ge
n
e
ri
c
T
ype
p
ublicstaticvoidmain
(
Strin
g[]
ar
g
s
)
{
ou ded Ge e c ype
p(g[]g ) {
Rectanglerectangle=newRectangle(2,2);
Circle9circle=newCircle9(2);
System out println(
"
Same area?
"
+equalArea(rectangle circle));
System
.
out
.
println( Same
area?
+
equalArea(rectangle
,
circle));
}
bli tti EtdGtiObjtbl
pu
bli
cs
t
a
ti
c<
E
ex
t
en
d
s
G
eome
t
r
i
c
Obj
ec
t
>
b
oo
l
ean
equalArea(Eobject1,Eobject2){
returnobject1.getArea()==object2.getArea();
}
15
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Java Generics: Bounded/Unbounded Wildcards, Raw Types, Ensure & Restrictions and more Slides Java Programming in PDF only on Docsity!

Bounded

Generic

Type

public^ static

void^ main(String[]

args^ )^ {

ou ded Ge e c

ype

p^

(^ g[]^ g

) { Rectangle^ rectangle

=^ new^ Rectangle(2,

2);

Circle9^ circle

=^ new^ Circle9(2); System out println("Same area? " + equalArea(rectangle

circle));

System.out.println( Same

area?^ +^ equalArea(rectangle

,^ circle));

} bli^ t ti^

E^ t^ d^ G^

t i Obj^ t^ b

l

public^ static

boolean equalArea(E^ object1,

E^ object2)^ { return^ object1.getArea()

==^ object2.getArea(); }

Raw Type and backward Compatibility Raw^ Type // raw type

and^ backward

Compatibility

//^ raw^ type ArrayList^ list

=^ new^ ArrayList();

This^ is^ roughly

equivalent^

to ArrayList

list^ =^ new

ArrayList();

Make^ it^ Sa

fe

//^ Max1.java:

Find^ a^ maximum

object

a e t Sa epublic class Max1 { public^ class

Max1^ { /**^ Return

the^ maximum

between^ two

objects^ */

public^ static

>

E

p^

p max(E^ o1,^

E^ o2)^ { if^ (o1.compareTo(o2)

>^ 0) return^ o1;elsereturn o2;return^ o2; } } Max.max("Welcome", 23);

18

}

Wildcards Why^ wildcards

are^ necessary?

See^ this^ example.

Wildcards^ WildCardDemo1WildCardDemo1?

unbounded

wildcard

?^ t^ d^

T^ b

d d^

ild^ d

?^ extends

T^ b

ounded^ wildcard

?^ super^ T^

lower^ bound

wildcard

WildCardDemo2WildCardDemo

WildCardDemo3WildCardDemo

Avoiding

Unsafe^ Raw

Types

o d^ Use

g U sa e

a^ ypes

new^ ArrayList() Instead^ of new^ ArrayList();

TestArrayListNewTestArrayListNew

RunRun

Ensure and Restrictions on Generics Generics^ are

implemented

using^ an^ approach

called

Ensure^ and

Restrictions

on^ Generics

type^ erasure

.^ The^ compiler

uses^ the^ generic

type

information

to^ compile

the^ code,

but^ erases

it

ft^ d^

S^ th^

i^ i f^

ti^ i^ t

il bl

afterwards.

So^ the^ generic

information

is^ not^ available

at^ run^ time.

This^ approach

enables^ the

generic^ code

to

be backward

compatible with the legacy code that

be^ backward

‐compatible

with^ the^ legacy

code^ that

uses^ raw^ types.

22 docsity.com

Important

Facts

It^ is^ important

to^ note^ that

a^ generic

class^ is

h^ d b^

ll it^ i^ t^

dl^ f it

t^ l

po ta t^

acts

shared^ by

all^ its^ instances

regardless

of^ its^ actual

generic^ type.^ GenericStack

stack1^ =^ new

GenericStack();

GenericStack

stack2^ =^ new

GenericStack();GenericStack();

Although^ GenericStack and

GenericStack b^ h^ i l^

l^ G^ i S

k

are^ two^ types,

but^ there^ i

s^ only^ one^

class^ GenericStack

loaded^ into

the^ JVM.

Retractions

on^ Generics

€^ Restriction

1:^ Cannot^

Create^ an^ Instance

of^ a^ Generic

Type (i e^

new E())

et act o s o

Ge e cs

Type.^ (i.e.,^

new^ E()). €^ Restriction 2: Generic Array Creation is Not Allowed€^ Restriction

2:^ Generic

Array^ Creation

is^ Not^ Allowed

(i.e.,^ new^ E[100]). € Restriction

3:^ A^ Generic

Type^ Parameter

of^ a^ Class^ Is

Not^ Allowed

in^ a^ Static^

Context. €^ Restriction

4:^ Exception

Classes^ Cannot

be^ Generic.^25

UML^ DiagramU^ ag a^ GenericMatrix^ # add(element1:E, element2: E): E^ # multiply(element1: E, element2: E): E

IntegerMatrix

multiply(element1:

E, element2: E): E

zero(): E +addMatrix(matrix1: E[][], matrix2: E[][]): E[][]+multiplyMatrix(matrix1: E[][], matrix2: E[][]): E[][]+printResult(m1: Number[][], m2: Number[][],printResult(m1: Number[][], m2: Number[][],m3: Number[][], op: char): void

RationalMatrix^27

Generic^

Type € Objective:^ This^ example

gives^ two^ programs

that

yp utilize the^ GenericMatrix

class^ for^ integer

matrix

arithmetic^

and^ rational

matrix^ arithmetic. TestIntegerMatrixTestIntegerMatrix^

RunRun

IntegerMatrixIntegerMatrix

TestRationalMatrixTestRationalMatrix

RunRun

RationalMatrixRationalMatrix