Type Erasure 86 What is Type Erasure? The way for the Java - - PDF document

type erasure
SMART_READER_LITE
LIVE PREVIEW

Type Erasure 86 What is Type Erasure? The way for the Java - - PDF document

Type Erasure 86 What is Type Erasure? The way for the Java compiler to generate byte code which implements our Generic Types & Generic Methods Type


slide-1
SLIDE 1

TypeErasure

  • 86
slide-2
SLIDE 2

WhatisTypeErasure?

87

ThewayfortheJavacompilerto generatebytecodewhichimplementsourGenericTypes& GenericMethods Typeerasureensuresthat• nonewclassesarecreatedforparameterizedtypes consequently,genericsincurnoruntimeoverhead

slide-3
SLIDE 3

WhatdoesitentailtoapplyTypeErasure?

88

Toimplementgenerics,theJavacompilerappliestypeerasureto:

  • 1. Replacealltypeparametersingenerictypeswiththeirboundsor

Objectifthetypeparametersareunbounded.Theproducedbytecode, therefore,containsonlyordinaryclasses,interfaces,andmethods

  • 2. Inserttypecastsifnecessarytopreservetypesafety
  • 3. Generatebridgemethodstopreservepolymorphisminextended

generictypes

slide-4
SLIDE 4

TypeErasureof GenericTypes

public class Node<T> { private T data; private Node<T> next; public Node(T data, Node<T> next) } this.data = data; this.next = next; } public T getData() { return data; } // ... }

89

Let sstartw/aclass usinganonbounded typeparameter

slide-5
SLIDE 5

TypeErasureofGenericTypes

public class Node { private Object data; private Node next; public Node(Object data, Node next) { this.data = data; this.next = next; } public Object getData() { return data; } // ... }

90

Whathappensduring TypeErasure? Tisunbounded replacew/!Object"

slide-6
SLIDE 6

public class Node<T extends Comparable<T>> { private T data; private Node<T> next; public Node(T data, Node<T> next) { this.data = data; this.next = next; } public T getData() { return data; } // ... }

91

TypeErasureofGenericTypes

Whatifourclassuses aboundedtype parameter?

slide-7
SLIDE 7

TypeErasureofGenericTypes

public class Node { private Comparable data; private Node next; public Node(Comparable data, Node next) { this.data = data; this.next = next; } public Comparable getData() { return data; } // ... }

92

Whathappensduring TypeErasure? JavareplacesTby leftmost/1st bound class:comparable

slide-8
SLIDE 8

TypeErasureof GenericMethods

// Counts the number of occurrences of elem in anArray public static <T> int count(T[] anArray, T elem) { int cnt = 0; for (T e : anArray) if (e.equals(elem)) ++cnt; return cnt; }

93

Object[] Object Object

slide-9
SLIDE 9

Example

// Let’s consider a few classes class Shape {/*…*/} class Circle extends Shape {/*…*/} class Rectangle extends Shape {/*…*/} // Define generic method to draw different shapes public static <T extends Shape> void draw(T shape) {/*…*/} // Type Erasure T replaced by Shape public static void draw(Shape shape) {/*…*/}

94

slide-10
SLIDE 10

TypeErasure

EffectofTypeErasure&BridgeMethods

95

slide-11
SLIDE 11

Let sconsiderthese2classes!

public class Node<T> { public T data; public Node(T data) { this.data = data; } public void setData(T data) { System.out.println("Node.setData"); this.data = data; } }

96

public class MyNode extends Node<Integer> { public MyNode(Integer data) { super(data); } public void setData(Integer data) { System.out.println("MyNode.setData"); super.setData(data); } }

slide-12
SLIDE 12

Now,let sconsiderthefollowingcode!

MyNode mn = new MyNode(5); Node n = mn; // n is of raw type Node // compiler throws unchecked warning n.setData("Hello"); Integer x = mn.data; // ClassCastException is thrown

97

Node n = (MyNode)mn; Integer x = (String)mn.data;

HerecomestheTypeErasure•

Compileraddscastsincemn is

  • fclassMyNode &MyNode

extendsanerasedtype ErasedfieldisoftypeObject

slide-13
SLIDE 13

Explanation" whathappenswhenrunningthiscode?

MyNode mn = new MyNode(5); Node n = (MyNode)mn; n.setData("Hello"); Integer x = (String)mn.data;

98

setData(Object)isexecutedonMyNode object MyNode classinheritedsetData(Object)from Node afteritwastypeerased InsidesetData(Object)fromNodeclass•

  • datafieldisassignedtheString reference

Usemn toaccesstheobject sdatafield• Resultexpectedtobeinteger sincemn is MyNode whichis Node<Integer> assignStringtoInteger? ClassCastException fromcastinsertedby Javacompiler

slide-14
SLIDE 14

Introducing!BridgeMethods

99

slide-15
SLIDE 15

AfterTypeErasure,our2previousclassesbecome!

public class Node { public Object data; public Node(Object data) { this.data = data; } public void setData(Object data) { System.out.println("Node.setData"); this.data = data; } }

100

public class MyNode extends Node { public MyNode(Integer data) { super(data); } public void setData(Integer data) { System.out.println("MyNode.setData"); super.setData(data); } }

// Bridge method generated public void setData(Object data) { setData((Integer) data); }

+