meljun cortes jedi slides-intro2-chapter13-an introduction to generics
Post on 05-Apr-2018
239 Views
Preview:
TRANSCRIPT
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
1/31
Introduction to Programming 2 1
13 An Introduction toGenerics
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
2/31
Introduction to Programming 2 2
Topics
Why Generics?
Declaring a Generic Class
"Primitive" Limitation
Constrained Generics
Declaring a Generic Method
Java Generics and Collections
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
3/31
Introduction to Programming 2 3
Generics
Included in Java's latest release
Problem with typecasting:
Downcasting is a potential hotspot forClassCastException
Makes our codes wordier
Less readable
Destroys benefits of a strongly typed language
Example: ArrayListobject
String myString = (String) myArrayList.get(0);
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
4/31
Introduction to Programming 2 4
Generics
Why generics?
Solve problem with typecasting
Benefits: Allow a single class to work with a wide variety of types
Natural way of eliminating the need for casting
Preserves benefits of type checking
Example: ArrayListobject
//myArrayList is a generic object
String myString = myArrayList.get(0);
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
5/31
Introduction to Programming 2 5
Generics
Caution:
Integer data = myArrayList.get(0);
Removal of downcasting doesn't mean that you could assignanything to the return value of the getmethod and do away withtypecasting altogether
Assigning anything else besides a String to the output of the getmethod will cause a compile time type mismatch
found: java.lang.String
required: java.lang.Integer
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
6/31
Introduction to Programming 2 6
Generics
1 //Code fragment
2 ArrayList genArrList =
3 new ArrayList();
4 genArrList.add("A generic string");
5 String myString = genArrList.get(0);
6 //int myInt = genArrList.get();
7 JoptionPane.showMessageDialog(this, myString);
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
7/31
Introduction to Programming 2 7
Declaring a Generic Class
For the previous code fragment to work, we should havedefined a generic version of the ArrayListclass
Java's newest version already provides users with genericversions of all Java Collection classes
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
8/31
Introduction to Programming 2 8
Declaring a Generic Class
1 class BasicGeneric {
2 private A data;
3 public BasicGeneric(A data) {
4 this.data = data;
5 }
6 public A getData() {
7 return data;
8 }
9 }
10 //continued...
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
9/31
Introduction to Programming 2 9
Declaring a Generic Class
11 public class GenSample {
12 public String method(String input) {
13 String data1 = input;
14 BasicGeneric basicGeneric = new
15 BasicGeneric(data1);
16 String data2 = basicGeneric.getData();
17 return data2;
18 }
19 //continued...
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
10/31
Introduction to Programming 2 10
Declaring a Generic Class
20 public Integer method(int input) {
21 Integer data1 = new Integer(input);
22 BasicGeneric basicGeneric = new
23 BasicGeneric (data1);
24 Integer data2 = basicGeneric.getData();
25 return data2;
26 }
27 //continued...
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
11/31
Introduction to Programming 2 11
Declaring a Generic Class
20 public static void main(String args[]) {
21 GenSample sample = new GenSample();
22 System.out.println(sample.method(
23 "Some generic data"));
24 System.out.println(sample.method(1234));
25 }
26 }
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
12/31
Introduction to Programming 2 12
Declaring a Generic Class
Output:
Some generic data
1234
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
13/31
Introduction to Programming 2 13
Declaring a Generic Class
Declaration of the BasicGeneric class:
class BasicGeneric
Contains type parameter:
Indicates that the class declared is a generic class Class does not work with any specific reference type
Declaration of field:
private A data;
The field data is of generic type, depending on the data type that theBasicGenericobject was designed to work with
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
14/31
Introduction to Programming 2 14
Declaring a Generic Class
Declaring an instance of the class
Must specify the reference type to work with
Examples:
BasicGeneric basicGeneric = newBasicGeneric(data1);
Class works with variables of type String
BasicGeneric basicGeneric = new
BasicGeneric(data1);
Class works with variables of type Integer
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
15/31
Introduction to Programming 2 15
Declaring a Generic Class
Declaration of the getData method:
public A getData() {
return data;
} Returns a value of type A, a generic type
The method will have a runtime data type
After you declare an object of type BasicGeneric, A is bound to aspecific data type
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
16/31
Introduction to Programming 2 16
Declaring a Generic Class
Instances of the BasicGenericclass
BasicGeneric basicGeneric = new
BasicGeneric(data1);
String data2 = basicGeneric.getData(); basicGeneric is bound to String type
No need to typecast
BasicGeneric basicGeneric = new
BasicGeneric(data1);
Integer data2 = basicGeneric.getData();
basicGeneric is bound to Integer type
No need to typecast
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
17/31
Introduction to Programming 2 17
Generics:
"Primitive" Limitation Java generic types are restricted to reference types andwon't work with primitive data types
Example:
BasicGeneric basicGeneric = new
BasicGeneric(data1);
Solution:
Wrap primitive types first
Can use wrapper types as arguments to a generic type
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
18/31
Introduction to Programming 2 18
Compiling Generics
To compile using JDK (v. 1.5.0):
javac -version -source "1.5" -sourcepath src -dclasses src/SwapClass.java
where
srcrefers to the location of the java source code
class refers to the location where the class file will be stored
Example:
javac -version -source "1.5" -sourcepath c:\temp
-d c:\temp c:/temp/MyFile.java
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
19/31
Introduction to Programming 2 19
Constrained Generics
Preceding example:
Type parameters of class BasicGenericcan be of any referencedata type
May want to restrict the potential type instantiations of ageneric class
Can limit the set of possible type arguments to subtypes of a giventype bound
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
20/31
Introduction to Programming 2 20
Constrained Generics
Limiting type instantiations of a class
Use the extends keyword in type parameter
class ClassName
Example: generic ScrollPane class Template for an ordinary Containerdecorated with scrolling functionality
Runtime type of an instance of this class will often be a subclass ofContainer
The static or general type is Container
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
21/31
Introduction to Programming 2 21
Constrained Generics
1 class ScrollPane {
2 ...
3 }
4 class TestScrollPane {
5 public static void main(String args[]) {
6 ScrollPane scrollPane1 =
7 new ScrollPane();
8 // The next statement is illegal
9 ScrollPane scrollPane2 =
10 new ScrollPane();
11 }
12 }
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
22/31
Introduction to Programming 2 22
Constrained Generics
Gives added static type checking
Guarantee that every instantiation of the generic type adheres toassigned bounds
Can safely call any methods found in the object's static type
No explicit bound on the parameter
Default bound is Object
An instance can't invoke methods that don't appear in the Objectclass
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
23/31
Introduction to Programming 2 23
Declaring a Generic Method
Java also allows us to declare a generic method
Generic Method
Polymorphic methods
Methods parameterized by type
Why generic method?
Type dependencies between the arguments and return value arenaturally generic
But the generic nature change from method call to method callrather than class-level type information
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
24/31
Introduction to Programming 2 24
Declaring a Generic Method
1 class Utilities {
2 /* T implicitly extends Object */
3 public static ArrayList make(T first) {
4 return new ArrayList(first);
5 }
6 }
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
25/31
Introduction to Programming 2 25
Declaring a Generic Method
Java also uses a type-inference mechanism
Automatically infers the types of polymorphic methods based on thetypes of arguments
Lessens wordiness and complexity of a method invocation
To construct a new instance ofArrayList, we wouldsimply have the following statement:
Utilities.make(Integer(0));
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
26/31
Introduction to Programming 2 26
Java Generics and
Collections
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
27/31
Introduction to Programming 2 27
Java Generics and
Collections1 import java.util.*;2
3 class LinkedListDemo {
4 public static void main(String args[]) {5 LinkedList list =
new LinkedList ();
7 list.add(new Integer(1));
8 list.add(new Integer(2));
9 list.add(new Integer(1));
10 System.out.println(list);
11 //continued...
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
28/31
Introduction to Programming 2 28
Java Generics and
Collections13 LinkedList list2 =14 new LinkedList (list);
15 System.out.println(list2);
16 }17 }
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
29/31
Introduction to Programming 2 29
Java Generics and
CollectionsOutput:
[1, 2, 1]
[1, 2, 1]
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
30/31
Introduction to Programming 2 30
Summary
Why Generics?
Declaring a Generic Class
classClassName {...
}
"Primitive" Limitation
-
7/31/2019 MELJUN CORTES JEDI Slides-Intro2-Chapter13-An Introduction to Generics
31/31
Introduction to Programming 2 31
Summary
Constrained Generics
class ClassName
Declaring a Generic Method Example:
public static ArrayList make(T first) {
return new ArrayList(first);
}
Java Generics and Collections
top related