Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Similar presentations


Presentation on theme: "Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75."— Presentation transcript:

1 Bicycle Car Types pedal() pushAccelerator()

2 Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75

3 BMISpreadsheet ABMICalculator Typing Objects

4 Object Types Primitive types Classes Kinds of Types Types ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Stringdoubleint 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 Abstract Value Vs Representation 2.2 02.2 0.22E+1 LBS_IN_KG

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

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

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

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

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

12 Mathematical Integers Other Integer Subsets {- .. +  } byte {-2 7.. (2 7 – 1)} Byte.MAX_VALUEByte.MIN_VALUE short {-2 15.. (2 15 – 1)} Short.MAX_VALUEShort.MIN_VALUE long {-2 63.. (2 63 – 1)} Short.MAX_VALUEShort.MIN_VALUE 8 bits 16 bits 64 bits

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

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

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

16 Assignment Rules ve T e  T v v = e; T e  T v v = (T v) e; TvTv TeTe !(T e  T v || T e  T v) v = (T v) e; double d = 5; int i = (int) 5.7; bool b = (bool) 5; Narrower than Wider than

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; public void setWeight (double newWeight) { weight = newWeight; } setWeight (70);

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

20 Returning a Value double weight; public int getIntWeight () { return weight; }

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

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

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

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

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 - 1  Integer.MIN_VALUE Double.MAX_VALUE + 1  Double.MAX_VALUE Double.MIN_VALUE - 1  Double.MIN_VALUE (double) Integer.MIN_VALUE - 1.0  double)( Integer.MIN_VALUE - 1.0

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

28 Int Overflow

29 Double Overflow

30 Mixed Operations 5/2.0 narrower type converted 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.0double d = 5/2double d = 2 double d = 2.0

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

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

33 boolean Constants boolean falsetrue

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

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

36 Boolean Property

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

38 Boolean Property Code public final double HIGH_BMI == 25; public boolean getOverWeight() { return getBMI() > HIGH_BMI; }

39 Boolean Property Code public final double HIGH_BMI == 25; public boolean isOverWeight() { return getBMI() > HIGH_BMI; }

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

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

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

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

44 Operator Precedence  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 unarycast

45 Operator Precedence (edit)  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 unarycast

46 Operator Precedence  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 unarycast

47 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


Download ppt "Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75."

Similar presentations


Ads by Google