COMP 110 TYPES Instructor Prasun Dewan PREREQUISITES Interfaces

  • Slides: 55
Download presentation
COMP 110 TYPES Instructor: Prasun Dewan

COMP 110 TYPES Instructor: Prasun Dewan

PREREQUISITES Interfaces 2

PREREQUISITES Interfaces 2

OBJECTS VS. PRIMITIVE TYPES Instances of classes and interfaces are objects All other values

OBJECTS VS. PRIMITIVE TYPES Instances of classes and interfaces are objects All other values are primitives Primitive types are used to construct objects ~Atoms vs. molecules 3

OBJECT TYPES ABMICalculator ABMISpreadsheet 4

OBJECT TYPES ABMICalculator ABMISpreadsheet 4

PRIMITIVE TYPES int String 3+4 “three” + “four” 3– 4 “three” – “four” …

PRIMITIVE TYPES int String 3+4 “three” + “four” 3– 4 “three” – “four” … Overloaded operator int and String are different types 5

PRIMITIVE TYPES int double 3/4=0 3. 0 / 4. 0 = 0. 75 Overloaded

PRIMITIVE TYPES int double 3/4=0 3. 0 / 4. 0 = 0. 75 Overloaded arithmetic operator 6

KINDS OF TYPES types Object types Primitive types double int String ABMICalculator ABMISpreadsheet Another.

KINDS OF TYPES types Object types Primitive types double int String ABMICalculator ABMISpreadsheet Another. BMISpreadsheet Lower case (by convention) Upper case (by convention) 7

ABSTRACT VALUE VS. SYNTAX Representing the abstract value 2. 2 02. 2 0. 22+E

ABSTRACT VALUE VS. SYNTAX Representing the abstract value 2. 2 02. 2 0. 22+E 1 LBS_IN_KGS 8

SYNTAX FOR INVOKING ABSTRACT OPERATION binary, infix profit - earnings uniary, prefix - earnings

SYNTAX FOR INVOKING ABSTRACT OPERATION binary, infix profit - earnings uniary, prefix - earnings arbitrary, method invocation Math. round(bmi) 9

TYPE RULES REGARDING ASSIGNMENT double height = 1. 77; int weight = 70; double

TYPE RULES REGARDING ASSIGNMENT double height = 1. 77; int weight = 70; double height = 2; Type rules define which of these are legal. int weight = 70. 0; double weight = “seventy”; 10

PRIMITIVE TYPES Each primitive type defines: � Range of abstract values of the type

PRIMITIVE TYPES Each primitive type defines: � Range of abstract values of the type � Constants (literals & named constants) denoting their values � Operations (with invocation syntax) that can be invoked on the values of that type � What types can be assigned to variables of the type 11

INT RANGE & CONSTANTS 32 bits Mathematical Integers {- … + } int {-231

INT RANGE & CONSTANTS 32 bits Mathematical Integers {- … + } int {-231 … +231 - 1} Integer. MIN_VALUE 0 +2 Integer. MAX_VALUE 02 -2 2 There are only 10 types of people in this world: those who read binary and those who don’t. 12

DOUBLE RANGE & CONSTANTS 64 bits Mathematical Real Numbers double Double. MIN_VALUE 0. 22

DOUBLE RANGE & CONSTANTS 64 bits Mathematical Real Numbers double Double. MIN_VALUE 0. 22 2. 2 02. 20 2. 0. 22 E+1 Double. MAX_VALUE. 22 E 1 e. Xy = x*10 y mantissa exponent 2. 2 0. 22 E-1 standard 13

OTHER INTEGER SUBSETS Mathematical Integers {- … + } byte Byte. MIN_VALUE short Short.

OTHER INTEGER SUBSETS Mathematical Integers {- … + } byte Byte. MIN_VALUE short Short. MIN_VALUE long Long. MIN_VALUE {-27. . 27 - 1} Byte. MAX_VALUE {-215. . 215 - 1} Short. MAX_VALUE {-263. . 263 - 1} Long. MAX_VALUE 8 bits 16 bits 64 bits 14

FLOAT SIZE & CONSTANTS Mathematical Real Numbers 32 bits float {-231 … +231 -

FLOAT SIZE & CONSTANTS Mathematical Real Numbers 32 bits float {-231 … +231 - 1} Float. MIN_VALUE . 2 Float. MAX_VALUE 15

MIXED ASSIGNMENT int long l = 70; int long Safe and automatically converted double

MIXED ASSIGNMENT int long l = 70; int long Safe and automatically converted double d = 70; int double d = 70. 0; 16

CAST cast int i = (int) 70. 6; int double Not automatically converted int

CAST cast int i = (int) 70. 6; int double Not automatically converted int i = 70; float double float f = (float) 70. 6; 17

INT BMI SPREADSHEET public class An. Int. BMISpreadsheet implements Int. BMISpreadsheet { int height,

INT BMI SPREADSHEET public class An. Int. BMISpreadsheet implements Int. BMISpreadsheet { int height, weight; public An. Int. BMISpreadsheet() { } public An. Int. BMISpreadsheet( int the. Initial. Height, int the. Initial. Weight) { set. Height(the. Initial. Height); set. Weight(the. Initial. Weight); } public int get. Weight() { return weight; } public void set. Weight(int new. Weight) { weight = new. Weight; } public int get. Height() { return height; } public void set. Height(int new. Height) { height = new. Height; } public int get. BMI() { return weight/(height*height); } } 18

CAST IN BMI SPREADSHEET Object. Editor. edit(new An. Int. BMISpreadsheet ((int) 1. 77, 75));

CAST IN BMI SPREADSHEET Object. Editor. edit(new An. Int. BMISpreadsheet ((int) 1. 77, 75)); 19

ASSIGNMENT RULES Narrower than v TV e TE TE T V v=e double d

ASSIGNMENT RULES Narrower than v TV e TE TE T V v=e double d = 5; Wider than ! (TE TV || TE TV) v = (TV) e TE T V bool b = (bool) 5; v = (TV) e int i = (int) 5. 7; 20

ASSIGNMENT RULES FOR PRIMITIVE TYPES If T 1 narrower than T 2 (Set of

ASSIGNMENT RULES FOR PRIMITIVE TYPES If T 1 narrower than T 2 (Set of instances of T 1 Set of instances of T 2) Expression of type T 1 can be assigned to Variable of type T 2 Expression of type T 2 can be assigned to Variable of type T 1 with cast 21

ACTUAL PARAMETER ASSIGNMENT double weight; public void set. Weight(double new. Weight) { weight =

ACTUAL PARAMETER ASSIGNMENT double weight; public void set. Weight(double new. Weight) { weight = new. Weight; } set. Weight(70); double new. Weight = 70. 0; Implicit assignment 22

ACTUAL PARAMETER ASSIGNMENT int weight; public void set. Weight(int new. Weight) { weight =

ACTUAL PARAMETER ASSIGNMENT int weight; public void set. Weight(int new. Weight) { weight = new. Weight; } set. Weight(70. 6); int new. Weight = 70. 6; Implicit assignment 23

ACTUAL PARAMETER ASSIGNMENT int weight; public void set. Weight(int new. Weight) { weight =

ACTUAL PARAMETER ASSIGNMENT int weight; public void set. Weight(int new. Weight) { weight = new. Weight; } set. Weight((int)70. 6); 24

RETURNING A VALUE double weight; public double get. Int. Weight() { return weight; }

RETURNING A VALUE double weight; public double get. Int. Weight() { return weight; } 25

TRANSLATED INTO ASSIGNMENT double weight; public int get. Int. Weight() { int get. Int.

TRANSLATED INTO ASSIGNMENT double weight; public int get. Int. Weight() { int get. Int. Weight = weight; return get. Int. Weight; } Internal variable 26

TRANSLATED INTO ASSIGNMENT double weight; public int get. Int. Weight() { return (int) weight;

TRANSLATED INTO ASSIGNMENT double weight; public int get. Int. Weight() { return (int) weight; } 27

PRIMITIVE TYPES Constants (Literals & Named Constants) Assignment Rules Operations with Invocation Syntax 28

PRIMITIVE TYPES Constants (Literals & Named Constants) Assignment Rules Operations with Invocation Syntax 28

INT ARITHMETIC OPERATIONS Name Action Operand & Result Type (Signature) + add int, int

INT ARITHMETIC OPERATIONS Name Action Operand & Result Type (Signature) + add int, int - subtract int, int - negate int * multiply int, int / int quotient int, int % int remainder int, int 5/2 2 x == (x/y)*y 5%2 1 x == (x/y)*y + (x%y) 29

DOUBLE ARITHMETIC OPERATIONS Name Action Operand & Result Type (Signature) + add double, double

DOUBLE ARITHMETIC OPERATIONS Name Action Operand & Result Type (Signature) + add double, double - subtract double, double - negate double * multiply double, double / int quotient double, double 5. 0/2. 0 2. 5 30

OVERFLOW Integer. MAX_VALUE + 1 Integer. MAX_VALUE Integer. MIN_VALUE - 1 Integer. MIN_VALUE (double)

OVERFLOW Integer. MAX_VALUE + 1 Integer. MAX_VALUE Integer. MIN_VALUE - 1 Integer. MIN_VALUE (double) (Integer. MIN_VALUE - 1. 0) Double. MAX_VALUE + 1 Double. MAX_VALUE Double. MIN_VALUE - 1 Double. MIN_VALUE (double) Integer. MIN_VALUE - 1. 0 31

DIVIDE BY ZERO 10/0 Exception -10/0 Exception 10. 0/0 Double. POSITIVE_INFINITY -10. 0/0 Double.

DIVIDE BY ZERO 10/0 Exception -10/0 Exception 10. 0/0 Double. POSITIVE_INFINITY -10. 0/0 Double. NEGATIVE_INFINITY 0/0 Exception 0. 0/0. 0 Double. Na. N 32

INT DIVIDE BY ZERO 33

INT DIVIDE BY ZERO 33

DOUBLE OVERFLOW 34

DOUBLE OVERFLOW 34

MIXED OPERATIONS Narrower type converted 5/2. 0 5. 0/2. 0 int i = (int)

MIXED OPERATIONS Narrower type converted 5/2. 0 5. 0/2. 0 int i = (int) (5/2. 0) doube d = 5/(int)2. 0 int i = (int) (5. 0/2. 0) double d = 5/2 int i = (int) (2. 5) double d = 2 int i = 2 double d = 2. 0 35

STRONG VS. WEAK TYPING “hello” - 1 Legal under weak typing “anything goes” Illegal

STRONG VS. WEAK TYPING “hello” - 1 Legal under weak typing “anything goes” Illegal under strong typing “strict type rules” int minus 36

MISCELLANEOUS MATH OPERATIONS Operations (invoked on Math) Signature abs() double, int acos(), asin(), atan()

MISCELLANEOUS MATH OPERATIONS Operations (invoked on Math) Signature abs() double, int acos(), asin(), atan() cos(), sin(), tan() double pow() double, double exp(), log() double round() double long random(), pi() double sqrt() double Math. PI (int) 5. 9 5 Math. pow(5, 3) 53 Math. round(5. 9) 6 int i = (int) Math. Round(5. 9) 6 37

BOOLEAN CONSTANTS boolean true false 38

BOOLEAN CONSTANTS boolean true false 38

RELATIONAL OPERATIONS Name Action Signature of int Implementation Signature of double Implementation == equal?

RELATIONAL OPERATIONS Name Action Signature of int Implementation Signature of double Implementation == equal? int, int boolean double, double boolean != not equal? int, int boolean double, double boolean > greater than? int, int boolean double, double boolean < less than? int, int boolean double, double boolean >= greather than or equal? int, int boolean double, double boolean <= less than or equal? int, int boolean double, double boolean 5 == 5 true 5 != 5 false 5 == 4 false 5 != 4 true 5 >= 4 true 5 <= 4 false 39

BOOLEAN OPERATIONS Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||,

BOOLEAN OPERATIONS Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||, | or boolean, boolean !true false !false true || true || false true && false || true false && true false || false true && true false && false 40

SHORT-CIRCUIT EVALUATION Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||,

SHORT-CIRCUIT EVALUATION Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||, | or boolean, boolean Short-circuit evaluation false && (9654. 34/323. 13 > 32. 34) false true || (9654. 34/323. 13 > 32. 34) false Second operand not evaluated Regular evaluation false & (9654. 34/323. 13 > 32. 34) true| (9654. 34/323. 13 > 32. 34) Second operand evaluated false 41

SHORT-CIRCUIT EVALUATION Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||,

SHORT-CIRCUIT EVALUATION Name(s) Action Signature ! not boolean &&, & and boolean, boolean ||, | or boolean, boolean false && (10/0 == Integer. MAX_VALUE) false & (10/0 == Integer. MAX_VALUE) An error in some programming languages 42

COMPLEX EXPRESSIONS Sub-expression false && (10/0 == Integer. MAX_VALUE) Operator evaluation order? 43

COMPLEX EXPRESSIONS Sub-expression false && (10/0 == Integer. MAX_VALUE) Operator evaluation order? 43

COMPLEX EXPRESSIONS Sub-expression false && ( 10 / 0 ) Operator evaluation order? 44

COMPLEX EXPRESSIONS Sub-expression false && ( 10 / 0 ) Operator evaluation order? 44

BOOLEAN VS. NUMBER EXPRESSIONS boolean over. Worked = hours. Worked > MAX_HOURS True if

BOOLEAN VS. NUMBER EXPRESSIONS boolean over. Worked = hours. Worked > MAX_HOURS True if hours. Worked is greater than MAX_HOURS and false otherwise int earnings = hourly. Wage*hours. Worked + BONUS 45

BOOLEAN PROPERTY 46

BOOLEAN PROPERTY 46

BOOLEAN PROPERTY CODE public boolean is. Over. Weight() { } 47

BOOLEAN PROPERTY CODE public boolean is. Over. Weight() { } 47

BOOLEAN PROPERTY CODE (EDIT) final double HIGH_BMI = 28; public boolean is. Over. Weight()

BOOLEAN PROPERTY CODE (EDIT) final double HIGH_BMI = 28; public boolean is. Over. Weight() { return get. BMI() > HIGH_BMI; } 48

BOOLEAN PROPERTY CODE private final double HIGH_BMI = 25; public boolean is. Over. Weight()

BOOLEAN PROPERTY CODE private final double HIGH_BMI = 25; public boolean is. Over. Weight() { return get. Bmi() > HIGH_BMI; } 49

BOOLEAN PROPERTY CODE // declare in interface private final double HIGH_BMI = 25; public

BOOLEAN PROPERTY CODE // declare in interface private final double HIGH_BMI = 25; public boolean is. Over. Weight() { return get. Bmi() > HIGH_BMI; } 50

PREVENTING INVALID BMI 51

PREVENTING INVALID BMI 51

OPERATOR PRECEDENCE Unary Cast ! - (T) * / & + - < >

OPERATOR PRECEDENCE Unary Cast ! - (T) * / & + - < > == != & | && <= false && 10 / 0 > 0 -5 -4 >= !true && false 5/4*3 true || false == false || true || (int) 5 / 2. 0 52

Unary OPERATOR PRECEDENCE (EDIT) Cast ! - (T) * / & + - <

Unary OPERATOR PRECEDENCE (EDIT) Cast ! - (T) * / & + - < > == != & | && <= false && 10 / 0 > 0 -5 -4 >= !true && false 5/4*3 true || false == false || true || (int) 5 / 2. 0 53

OPERATOR PRECEDENCE Unary Cast ! - (T) * / & + - < >

OPERATOR PRECEDENCE Unary Cast ! - (T) * / & + - < > == != & | && <= false && 10 / 0> 0 -5 -4 >= !true && false 5/4*3 true || false == false || true false && ( (10 / 0) > 0 )) (-5)-4 ( !true ) && false (5/4)*3 true || ( false == false ) || true || (int) 5 / 2. 0 ( (int) 5 ) / 2. 0 54

PRINTING ARBITRARY EXPRESSIONS System. out. println (2) Output: 2 System. out. println (2. 0)

PRINTING ARBITRARY EXPRESSIONS System. out. println (2) Output: 2 System. out. println (2. 0) Output: 2. 0 System. out. println ((int) 2. 0) Output: 2 System. out. println (5 > 0) Output: true 55