1 / 47

Types

Bicycle. Car. pedal(). pushAccelerator(). Types. int. double. String. Programming Language Types. 3. /. +. 4. 3.0. /. 4.0. “three”. +. “four”. = 7. = 0. = 0.75. = “threefour”. ABMICalculator. BMISpreadsheet. Typing Objects. Object Types. Classes. Primitive types.

ezra
Download Presentation

Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bicycle Car pedal() pushAccelerator() Types

  2. int double String Programming Language Types 3 / + 4 3.0 / 4.0 “three” + “four” = 7 = 0 = 0.75 = “threefour”

  3. ABMICalculator BMISpreadsheet Typing Objects

  4. Object Types Classes Primitive types double int String ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Kinds of Types Types Type = Set of operations

  5. Object Vs Primitive Types • Instances of object types are objects. • Instances of primitive types are primitive values. • Primitive types used to construct object types. • ~ Atoms Vs Molecules • Different set of rules. • In Smalltalk no difference. • C++ even worse.

  6. 2.2 Abstract Value Vs Representation 02.2 0.22E+1 LBS_IN_KG

  7. binary,infix unary, prefix Arbitray, method invocation Invoking Abstract Operation profit - earnings -earnings Math.round(bmi)

  8. Types and Assignment double height = 1.77; int weight = 70; double height = 2; int weight = 70.0; int weight = “seventy”;

  9. Primitive Types • Constants (Literals & Named Constants) • Assignment Rules • Operations with Invocation Syntax

  10. 32 bits Mathematical Integers int int Range & Constants {- .. +} {-231 .. (231 – 1)} Integer.MIN_VALUE -2147483648 0 02 +2 Integer.MAX_VALUE 2147483647 2 -2

  11. 64 bits Mathematical Real Numbers .22E1 .22E-1 mantissa exponent Standard double Range & Constants xEy = x * 10y double Double.MIN_VALUE 2.2 0.22 02.20 .2 2. 2 Double.MAX_VALUE 0.22E+1 2.2E0 22E-1

  12. 64 bits 8 bits 16 bits {-263 .. (263 – 1)} {-215 .. (215 – 1)} {-27 .. (27 – 1)} byte long short Byte.MIN_VALUE Short.MIN_VALUE Short.MIN_VALUE Byte.MAX_VALUE Short.MAX_VALUE Short.MAX_VALUE Other Integer Subsets Mathematical Integers {- .. +}

  13. Mathematical Real Numbers 32 bits float Size & Constants float Float.MIN_VALUE .2 Float.MAX_VALUE

  14. int Safe & Automatically Converted Mixed Assignment int  long long l =70; long l int  double double d = 70; 70.0;

  15. Not Automatically Converted cast Cast int  double int i = 70.6; (int) 70; float  double float i = 70.6; (float)

  16. Narrower than Te  Tv v = e; Wider than !(Te  Tv || Te  Tv) Te  Tv v = (Tv) e; v = (Tv) e; Assignment Rules Te Tv v e double d = 5; bool b = (bool) 5; int i = (int) 5.7;

  17. Assignment Rules for Primitive Types • If T1 narrower than T2 (Set of instances of T1  Set of instances of T2) • Expression of type T1 can be assigned to Variable of type T2 • Expression of type T2 can be assigned to Variable of type T1 with cast.

  18. Actual Parameter Assignment double weight; publicvoid setWeight (double newWeight) { weight = newWeight; } setWeight (70);

  19. Actual Parameter Assignment int intWeight; publicvoid setIntWeight (int newWeight) { intWeight = newWeight; } setWeight (70.6); setWeight ((int)70.6);

  20. double weight; publicint getIntWeight () { return weight; } Returning a Value

  21. double weight; publicint getIntWeight () { return weight; } double weight; publicint getIntWeight () { int getIntWeight = weight; } Internal Variable Translated Into Assignment

  22. double weight; publicint getIntWeight () { return(int) weight; } Returning a Value

  23. Primitive Types • Constants (Literals & Named Constants) • Assignment Rules • Operations with Invocation Syntax

  24. Int Arithmetic Operations 5/2 5/2  2 5 % 2 5 % 2  1 x == (x/y)*y x == (x / y) * y + (x % y)

  25. double Arithmetic Operations 5.0/2.0 5.0/2.0  2.5

  26. Overflow Integer.MAX_VALUE + 1  Integer.MAX_VALUE  Integer.MIN_VALUE Integer.MIN_VALUE - 1  double)( Integer.MIN_VALUE - 1.0 (double) Integer.MIN_VALUE - 1.0 Double.MAX_VALUE + 1  Double.MAX_VALUE  Double.MIN_VALUE Double.MIN_VALUE - 1

  27. Overflow 10/0  Integer.MAX_VALUE -10/0  Integer.MIN_VALUE 10.0/0.0  Double.POSITIVE_INFINITY  Double.NEGATIVE_INFINITY -10.0/0.0  0 0/0  Double.NaN 0.0/0.0

  28. Int Overflow

  29. Double Overflow

  30. narrower type converted Mixed Operations 5/2.0 5.0/2.0 int i = (int) (5/2.0) int i =(int) (5.0/2.0) int i = (int) 2.5 int i = 2 double d = 5/(int) 2.0 double d = 5/2 double d = 2 double d = 2.0

  31. int minus Strong Vs Weak Typing Legal under Weak Typing “hello” - 1 anything goes Illegal under Strong Typing strict type rules

  32. Math.pi()   Math.power(5,3) 53  Math.round(5.9)  (long) 6  5  int i = 6 Integer.MAX_VALUE  Misc Math Operations (int) 5.9 int i = (int) Math.round(5.9) int i = Math.round(5.9) (int) Math.round(Integer.MAX_VALUE + 1.0)

  33. boolean Constants boolean true false

  34. Relational Operations 5 == 5  true  false 5 != 5 5 == 4  false 5 != 4  true 5 >= 4  true  false 5 <= 4

  35. true if hoursWorked is greater than MAX_HOURS and false otherwise int earnings = hourlyWage*hoursWorked + BONUS Boolean Vs Number Expressions boolean overWorked = hoursWorked > MAX_HOURS

  36. Boolean Property

  37. Boolean Property Code (edit) public boolean isOverWeight() { return OVER_WEIGHT_BMI < getBMI(); }

  38. Boolean Property Code publicfinaldouble HIGH_BMI == 25; • publicboolean getOverWeight() { • return getBMI() > HIGH_BMI; • }

  39. Boolean Property Code publicfinaldouble HIGH_BMI == 25; • publicboolean isOverWeight() { • return getBMI() > HIGH_BMI; • }

  40. Boolean Operations !true  false !false  true true && true  true true || true  true true && false  false true || false  true false && true  false false || true  true false && false  false false || false  false

  41. Short-circuit evaluation Second operand not evaluated Regular evaluation Second operand evaluated Short-Circuit Evaluation false && (9654.34/323.13 > 32.34)  false true || (9654.34/323.13 > 32.34)  true false & (9654.34/323.13 > 32.34  false true | (9654.34/323.13 > 32.34)  true

  42. error in some languages Short-Circuit Evaluation false && (10/0)  false false & (10/0)

  43. Sub-expression false && 10 / 0 Operator evaluation order? Complex Expressions false && (10 / 0)

  44. unary cast  false && 10 / 0 false && 10 / 0 - 5 - 4  -5 - 4 !true && false  !true && false 5 / 4 * 3  5/4 * 3 true || false == false || true  true || false == false || true (int) 5 / 2.0  (int) 5 / 2.0 Operator Precedence

  45. unary cast  false && (10 / 0) false && 10 / 0 - 5 - 4  (-5) - 4 !true && false ( !true) && false 5 / 4 * 3  (5/4) * 3 true || false == false || true  true || (false == false) || true (int) 5 / 2.0  ((int) 5) / 2.0 Operator Precedence (edit)

  46. unary cast  false && (10 / 0) false && 10 / 0 - 5 - 4  (-5) - 4 !true && false  (!true) && false 5 / 4 * 3  (5/4) * 3 true || false == false || true  true || (false == false) || true (int) 5 / 2.0  ((int) 5) / 2.0 Operator Precedence

  47. Printing Arbitrary Expressions System.out.println (2) Output: 2 Output: 2.0 System.out.println (2.0) Output: 2 System.out.println ((int) 2.0) Output: true System.out.println (5 > 0)

More Related