Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4...
-
date post
22-Dec-2015 -
Category
Documents
-
view
216 -
download
0
Transcript of Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4...
BicycleCar
Types
pedal()pushAccelerator()
Stringdoubleint
Programming Language Types
3 3.0 4.04 “three” “four”
= 7= “threefour”
+ +/
= 0
/
= 0.75
BM
ISpr
eads
heet
AB
MIC
alcu
lato
rTyping Objects
Object Types
Primitive types Classes
Kinds of Types
Types
ABMISpreadsheet
AnotherBMISpreadsheet
Interfaces
BMISpreadsheet
Stringdouble int
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.
Abstract Value Vs Representation
2.2 02.2
0.22E+1
LBS_IN_KG
Invoking Abstract Operation
profit - earnings
-earnings
Math.round(bmi)
binary,infix
unary, prefix
Arbitray, method invocation
Types and Assignment
double height = 1.77;
int weight = 70;
int weight = 70.0;
double height = 2;
int weight = “seventy”;
Primitive Types
• Constants (Literals & Named Constants)
• Assignment Rules
• Operations with Invocation Syntax
Mathematical Integers
int Range & Constants
{- .. +}
int {-231 .. (231 – 1)}
0-2147483648
2147483647 2
02
Integer.MAX_VALUE
Integer.MIN_VALUE
-2
+2
32 bits
Mathematical Real Numbers
double Range & Constants
double
2.2
0.22E+1
0.22
Double.MAX_VALUE
Double.MIN_VALUE .2
64 bits
02.20 2. 2
2.2E0 22E-1
Standardmantissa
.22E1 .22E-1
exponent
xEy = x * 10y
Mathematical Integers
Other Integer Subsets
{- .. +}
byte {-27 .. (27 – 1)}
Byte.MAX_VALUEByte.MIN_VALUE
short {-215 .. (215 – 1)}
Short.MAX_VALUEShort.MIN_VALUE
long {-263 .. (263 – 1)}
Short.MAX_VALUEShort.MIN_VALUE
8 bits
16 bits
64 bits
Mathematical Real Numbers
float Size & Constants
float 32 bits
Float.MAX_VALUE
Float.MIN_VALUE .2
Mixed Assignment
long l =70;
int
Safe & Automatically Converted
int long
double d = 70;
int double
70.0;
long l
int i = 70.6;
Cast
int double
Not Automatically Converted
(int)
cast
70;
float i = 70.6;(float)
float double
Assignment Rules
v e
Te Tv
v = e;
Te Tv
v = (Tv) e;
TvTe
!(Te Tv || Te Tv)
v = (Tv) e;
double d = 5;
int i = (int) 5.7; bool b = (bool) 5;
Narrower than
Wider than
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;
public void setWeight (double newWeight) {
weight = newWeight;
}
setWeight (70);
Actual Parameter Assignment
int intWeight;
public void setIntWeight (int newWeight) {
intWeight = newWeight;
}
setWeight (70.6);setWeight ((int)70.6);
Returning a Value
double weight;
public int getIntWeight () {
return weight;
}
Translated Into Assignment
double weight;
public int getIntWeight () {
return weight;
}
double weight;
public int getIntWeight () {
int getIntWeight = weight;
}
Internal Variable
Returning a Value
double weight;
public int getIntWeight () {
return(int) weight;
}
Primitive Types
• Constants (Literals & Named Constants)
• Assignment Rules
• Operations with Invocation Syntax
Int Arithmetic Operations
5/2 5 % 2
x == (x/y)*y x == (x / y) * y + (x % y)
5/2 2 5 % 2 1
Name Action Operands & ResultType(Signature)
+ add int, int int- subtract int, int int- negate int int* multiply int, int int/ int quotient int, int int% int remainder int, int int
double Arithmetic Operations
5.0/2.0 5.0/2.0 2.5
Name Action Operands & Result Type(Signature)
+ add double, double double- subtract (unary) double- subtract double, double double* multiply double, double double/ divide double, double double
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
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
Int Overflow
Double Overflow
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.0 double d = 5/2 double d = 2
double d = 2.0
Strong Vs Weak Typing
“hello” - 1
int minus
Legal under Weak Typing
Illegal under Strong Typing
anything goes
strict type rules
Misc Math Operations
Operations (invoked on Math) Signatureabs() double double, int intacos(), asin(), atan(), cos(), sin(), tan() double doublepow () double, double doubleexp(), log() double doubleround() double longrandom(), pi() doublesqrt() double double
Math.pi() Math.power(5,3) 53
int i = Math.round(5.9)
Math.round(5.9) (long) 6
int i = 6int i = (int) Math.round(5.9)
(int) Math.round(Integer.MAX_VALUE + 1.0) Integer.MAX_VALUE
(int) 5.9 5
boolean Constants
boolean
falsetrue
Relational Operations
5 == 5 true
5 == 4 false
5 >= 4 true
5 <= 4 false
5 != 5 false
5 != 4 true
Name Action Signature of intimplementation
Signature of doubleimplementation
== 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>= greater than or
equal?int, int boolean double, double boolean
<= less than or equal? int, int boolean double, double boolean
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
Boolean Property
Boolean Property Code (edit)
public boolean isOverWeight() {
return OVER_WEIGHT_BMI < getBMI();
}
Boolean Property Code
public final double HIGH_BMI == 25;
public boolean getOverWeight() {return getBMI() >
HIGH_BMI;}
Boolean Property Code
public final double HIGH_BMI == 25;
public boolean isOverWeight() {return getBMI() >
HIGH_BMI;}
Boolean Operations
!true false
!false true
true && true true
true && false false
Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean
false && true false
false && false false
true || true true
true || false true
false || true true
false || false false
Regular evaluation
Short-circuit evaluation
Short-Circuit Evaluation
false && (9654.34/323.13 > 32.34) false
true || (9654.34/323.13 > 32.34) true
Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean
Second operand not evaluated
false & (9654.34/323.13 > 32.34 false
true | (9654.34/323.13 > 32.34) true
Second operand evaluated
Short-Circuit Evaluation
false && (10/0) false
Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean
false & (10/0)
error in some languages
false && (10 / 0)
Complex Expressions
Sub-expression
false && 10 / 0
Operator evaluation order?
Operator Precedence
! - (T)* / %+ -> < <= >=== !=&|&&||
false && 10 / 0false && 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
unary cast
Operator Precedence (edit)
! - (T)* / %+ -> < <= >=== !=&|&&||
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
unary cast
Operator Precedence
! - (T)* / %+ -> < <= >=== !=&|&&||
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
unary cast
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