Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Generics Part 1-Introduction to Java Programming-Lecture Slides, Slides of Java Programming

This is an Introductory course of Java Web Programming focusing on writing maintainable extensible code, methods of debugging, logging and profiling. The Java Technology used is J2EE an Enterprise Application Development tool. This lecture includes: Java, Generics, Parameterize, Errors, Compile, Time, Instantiation, Casting, Class, Interface, Stack

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa 🇮🇳

4.7

(3)

62 documents

Partial preview of the text

Download Java Generics Part 1-Introduction to Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Ad^ d JAd^ d J^

ii

Advanced JavaAdvanced Java

ProgrammingProgrammingLectureLecture--0202--Java GenericsJava Generics

docsity.com

Objectives^ €^ To^ know^ the^ benefits^ of^

generics € To use generic classes and interfaces

Object^ es^ €^ To^ use^ generic^ classes^ and

interfaces €^ To^ declare^ generic^ classes

and^ interfaces €^ To^ understand^ why^ generic

types^ can^ improve^ reliability

and readabilityreadability € To^ declare^ and^ use^ generic^ methods

and^ bounded^ generic^ types €^ To^ use^ raw^ types^ for^ backward

compatibility €^ To know wildcard types and understand why they are necessary€^ To^ know^ wildcard^ types^ and

understand^ why^ they^ are^ necessary €^ To^ convert^ legacy^ code^ using

JDK^ 1.5^ generics €^ To^ understand^ that^ generic

type^ information^ is^ erased^ by

the compiler^ and^ all^ instances^ of

a^ generic^ class^ share^ the^ same

runtime p^ g class^ file € To know^ certain^ restrictions^ on^ generic

types^ caused^ by^ type^ erasure €^ To^ design^ and^ implement

generic^ matrix^ classes

2 docsity.com

Java^ Generics^ Intro^ ^ You may be familiar with similar constructs from^ You^ may^ be^ familiar

with^ similar^ constructs

from other^ languages,^ most

notably^ C++^ templates.

`^ there^ are^ both^ similarities
and^ important^ differences
`^ Generics^ allow^ you^ to^ abstract
over^ types

List^ myIntList =^ new^ LinkedList();

//^1 y^

() myIntList.add(new^ Integer(0));

//^2 Integer^ x^ =^ (Integer)^ myIntList.iterator().next();

//^3 docsity.com

Why do you Get a warning Why^ do^ you^ Get^ a^ public class ShowUncheckedWarning {

warning

public^ class^ ShowUncheckedWarning
public^ static^ void^
main(String[]^ args)
java.util.ArrayList
list^ = new java.util.ArrayList();

list.add("Java^ Programming");}} }^ To understand the compileTo

understand the compilewarning on this line, you need tolearn JDK 1.5 generics.

5 learn JDK 1.5 generics.

docsity.com

Fix^ the^ Warning^ t e^ a^ g public class ShowUncheckedWarning { public^ class ShowUncheckedWarning

public static void main(String[] args) {java.util.ArrayList list =^ new java.util.ArrayList();list.add("Java Programming");
`

} }^ No compile warning on this line.

7 docsity.com

What^ is^ Generics? €^ Generics^ is^ the^ capability

to^ parameterize^ types.

at s Ge e cs Generics^ is the capability to parameteri e types. With^ this^ capability,^ you

can^ define^ a^ class^ or

a method^ with^ generic

types^ that^ can^ be^ substituted^ b^ h^ l using^ concrete^ types

by^ the^ compiler. € For example, you^ may^ define^ a^ generic^

stack^ class that stores the elements of a generic type Fromthat^ stores^ the^ elements

of^ a^ generic^ type.^ From this^ generic^ class,^ you^

may^ create^ a^ stack^ object for^ holding^ strings^ and

a^ stack^ object^ for^ holdingg g j^

g numbers.^ Here,^ strings

and^ numbers^ are^ concrete types^ that^ replace^ the

generic^ type.^8

docsity.com

Generic^ Type package java.lang;^

package java.lang;

Ge e c^ ype public interface Comaprable {public int compareTo(Object o)}

public interface Comaprable {public int compareTo(T o)} (a) Prior to JDK 1.^

(b) JDK 1.5 Generic InstantiationGeneric Instantiation

Runtime error Comparable c^ =^ new^ Date();^

Comparable^ c^ =^ new^ Date(); Comparable^ c^ new^ Date();System.out.println(c.compareTo("red"));(a)^ Prior^ to^ JDK^ 1.

Comparable^ c^ =^ new^ Date();System.out.println(c.compareTo("red"));(b)^ JDK^ 1.5 Improves reliabilityCompile error^10

docsity.com

Generic^ ArrayList

in^ JDK^ 1.5 java.util.ArrayList

+ArrayList()

java.util.ArrayList+ArrayList()

Ge e c^ ay^ st

J^5

+ArrayList()+add(o: Object) : void+add(index: int, o: Object) : void+clear(): void

+ArrayList()+add(o: E) : void+add(index: int, o: E) : void+clear(): void +clear(): void+contains(o: Object): boolean+get(index: int) : Object+indexOf(o: Object) : int

+clear(): void+contains(o: Object): boolean+get(index: int) : E+indexOf(o: Object) : int +indexOf(o: Object) : int+isEmpty(): boolean+lastIndexOf(o: Object) : int+remove(o: Object): boolean

+indexOf(o: Object) : int+isEmpty(): boolean+lastIndexOf(o: Object) : int+remove(o: Object): boolean remove(o: Object): boolean+size(): int+remove(index: int) : boolean+set(index: int, o: Object) : Object

remove(o: Object): boolean+size(): int+remove(index: int) : boolean+set(index: int, o: E) : E^11 (^ ,^ j^ )^ j^

(^ ,^ ) (a) ArrayList before JDK 1.^

(b) ArrayList in JDK 1.5docsity.com

Declaring Generic Classes and Interfaces Declaring^ Generic^ GenericStack

Classes^ and^ Interfaces

-list: java.util.ArrayList+GenericStack()^

An array list to store elements.Creates an empty stack. +getSize(): int+peek(): E+pop(): E

Returns the number of elements in this stack.Returns the top element in this stack.p p()Returns and removes the top element in this stack. +push(o: E): E+isEmpty(): boolean

p Adds a new element to the top of this stack.Returns true if the stack is empty.

G^ i S^ k

13

GenericStack

docsity.com

Generic^ Methods^ public^ static^ ^ void

print(E[]^ list)^ { for (int i = 0; i < list.length; i++)

Ge e c^ et ods^ for^ (int^ i^ 0;^ i^

<^ list.length;^ i^ ) System.out.print(list[i]^ +^ "^ ");System.out.println();}} public static void print(Object[] list) {public static void print(Object[]^ list)^ { for (int i = 0; i <^ list.length;^ i++) System.out.print(list[i]^ +^ "^ ");System.out.println();}

14 docsity.com