470 likes | 707 Views
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.
E N D
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 double int String ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Kinds of Types Types Type = Set of operations
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.
2.2 Abstract Value Vs Representation 02.2 0.22E+1 LBS_IN_KG
binary,infix unary, prefix Arbitray, method invocation Invoking Abstract Operation profit - earnings -earnings Math.round(bmi)
Types and Assignment double height = 1.77; int weight = 70; double height = 2; int weight = 70.0; int weight = “seventy”;
Primitive Types • Constants (Literals & Named Constants) • Assignment Rules • Operations with Invocation Syntax
32 bits Mathematical Integers int int Range & Constants {- .. +} {-231 .. (231 – 1)} Integer.MIN_VALUE -2147483648 0 02 +2 Integer.MAX_VALUE 2147483647 2 -2
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
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 {- .. +}
Mathematical Real Numbers 32 bits float Size & Constants float Float.MIN_VALUE .2 Float.MAX_VALUE
int Safe & Automatically Converted Mixed Assignment int long long l =70; long l int double double d = 70; 70.0;
Not Automatically Converted cast Cast int double int i = 70.6; (int) 70; float double float i = 70.6; (float)
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;
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.
Actual Parameter Assignment double weight; publicvoid setWeight (double newWeight) { weight = newWeight; } setWeight (70);
Actual Parameter Assignment int intWeight; publicvoid setIntWeight (int newWeight) { intWeight = newWeight; } setWeight (70.6); setWeight ((int)70.6);
double weight; publicint getIntWeight () { return weight; } Returning a Value
double weight; publicint getIntWeight () { return weight; } double weight; publicint getIntWeight () { int getIntWeight = weight; } Internal Variable Translated Into Assignment
double weight; publicint getIntWeight () { return(int) weight; } Returning a Value
Primitive Types • Constants (Literals & Named Constants) • Assignment Rules • Operations with Invocation Syntax
Int Arithmetic Operations 5/2 5/2 2 5 % 2 5 % 2 1 x == (x/y)*y x == (x / y) * y + (x % y)
double Arithmetic Operations 5.0/2.0 5.0/2.0 2.5
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
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
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
int minus Strong Vs Weak Typing Legal under Weak Typing “hello” - 1 anything goes Illegal under Strong Typing strict type rules
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)
boolean Constants boolean true false
Relational Operations 5 == 5 true false 5 != 5 5 == 4 false 5 != 4 true 5 >= 4 true false 5 <= 4
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
Boolean Property Code (edit) public boolean isOverWeight() { return OVER_WEIGHT_BMI < getBMI(); }
Boolean Property Code publicfinaldouble HIGH_BMI == 25; • publicboolean getOverWeight() { • return getBMI() > HIGH_BMI; • }
Boolean Property Code publicfinaldouble HIGH_BMI == 25; • publicboolean isOverWeight() { • return getBMI() > HIGH_BMI; • }
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
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
error in some languages Short-Circuit Evaluation false && (10/0) false false & (10/0)
Sub-expression false && 10 / 0 Operator evaluation order? Complex Expressions false && (10 / 0)
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
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)
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
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)