Lecture 13

# G double to int dan grossman downcast dan sgrossmanof

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ML Variable Bindings ML Variable Bindings appropriate types –  Check: exp1 Lecture 2Course ompa9ble types and exp 1have c Mechanics •  Types are Dompa9ble with anp c an Grossman D o Grossman Fall •  Example: 42Fall 2011 fails, 202011 21.9999 passes + true + –  Compute: Inferred type of expression is a func9on of the operator and operand types •  Example: 20 + 21.999 has type double, 42 + “, the answer” has type String (in Java). Winter 2013 UW CSE 401 (Michael Ringenburg) 14 A Sampling of Seman9c Checks and Computa9ons CSE341: CSE341: Programming Languages •  Assignment: exp1 = exp2 Programming Languages L constant –  Check: exp1 is Lssignable (not aecture 1 or expression) a ecture 1 Course –  Check: eCourse xp2 have (assignment- )compa9ble types xp1 and e Mechanics Mechanics •  Iden9cal, or ML Variable Bindings ML Variable Bindings •  Type of exp2 can be (automa9cally) converted to exp1 (e.g., char to int), or Dan Grossman Dan Grossman •  Type of exp2 is a subclass of type Ff exp1 (can be decided at oall 2011 compile 9me) Fall 2011 •  Example: x + 5 = 4 fails, x = 42 passes if x in an integer or double, fails if x is a boolean •  Ex: Object a = new Integer(); Number b = a; also fails (a’s sta0c type not a subclass of b’s type).! –  Compute: Inferred type is type of exp1 Winter 2013 UW CSE 401 (Michael Ringenburg) 15 A Sampling of Seman9c Checks and Computa9ons CSE341: Programming CSE341: Programming Languages Languages •  Cast: (exp1) exp2 –  Check: exp1 iLecture 1 Lecture 1 s a type Course Mechanics –  Check: exp2 either Course Mechanics M •  Has ML Variable Bindings same type as exp1 L Variable Bindings •  Can be converted to type exp1 (e.g., double to int) Dan Grossman •  Downcast: Dan sGrossmanof exp1 is a uperclass Fall 2011 Fall run9me error is exp2 isn’t really an exp1, e.g., –  May generate a2011 animal a = new animal(); dog d = (dog)a; where dog extends animal. •  Upcast: is the same or a subclass of exp1 –  Compute: Inferred type is exp1 Winter 2013 UW CSE 401 (Michael Ringenburg) 16 A Sa...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online