c omp 110 t ypes instructor: prasun dewan 2 p rerequisites interfaces

55
COMP 110 TYPES Instructor: Prasun Dewan

Upload: annice-cain

Post on 13-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Slide 1

Comp 110TypesInstructor: Prasun Dewan#PrerequisitesInterfaces

#Objects vs. Primitive TypesInstances of classes and interfaces are objects All other values are primitivesPrimitive types are used to construct objects~Atoms vs. molecules#Object Types

ABMICalculatorABMISpreadsheet#Primitive TypesintString3 + 4three + four3 4three fourOverloaded operatorint and String are different types#Primitive Typesintdouble3 / 4 = 03.0 / 4.0 = 0.75Overloaded arithmetic operator#Kinds of TypestypesPrimitive typesObject typesdoubleintStringABMICalculatorABMISpreadsheetLower case (by convention)Upper case (by convention)AnotherBMISpreadsheetBMISpreadsheet#Abstract Value vs. Syntax2.202.20.22+E1LBS_IN_KGSRepresenting the abstract value 2.2#Syntax for Invoking Abstract Operationprofit - earnings- earningsMath.round(bmi)binary, infixuniary, prefixarbitrary, method invocation#Type Rules Regarding Assignmentdouble height = 1.77;int weight = 70;double height = 2;int weight = 70.0;double weight = seventy;Type rules define which of these are legal.#Primitive TypesEach primitive type defines:Range of abstract values of the typeConstants (literals & named constants) denoting their valuesOperations (with invocation syntax) that can be invoked on the values of that typeWhat types can be assigned to variables of the type#int Range & ConstantsMathematical Integers {- +}int {-231 +231 - 1}32 bitsInteger.MIN_VALUEInteger.MAX_VALUE002+22-2There are only 10 types of people in this world: those who read binary and those who dont.#double Range & ConstantsMathematical Real NumbersdoubleDouble.MIN_VALUEDouble.MAX_VALUE2.20.22.202.202.0.22E00.22E+1.22E1.22E-1eXy = x*10ymantissaexponentstandard264 bits#Other Integer SubsetsMathematical Integers {- +}8 bitsbyte{-27 .. 27 - 1}Byte.MIN_VALUEByte.MAX_VALUE16 bitsshort{-215 .. 215 - 1}Short.MIN_VALUEShort.MAX_VALUE64 bitslong{-263 .. 263 - 1}Long.MIN_VALUELong.MAX_VALUE#float Size & ConstantsMathematical Real Numbersfloat {-231 +231 - 1}32 bitsFloat.MIN_VALUEFloat.MAX_VALUE.2#Mixed Assignmentlong l = 70;double d = 70.0;double d = 70;intint longSafe and automatically convertedint double#Castint i = 70.6;float f = 70.6;int doubleNot automatically convertedfloat double(int)int i = 70;(float)cast#Int BMI Spreadsheetpublic class AnIntBMISpreadsheet implements IntBMISpreadsheet { int height, weight; public AnIntBMISpreadsheet() { } public AnIntBMISpreadsheet( int theInitialHeight, int theInitialWeight) { setHeight(theInitialHeight); setWeight(theInitialWeight); } public int getWeight() { return weight; } public void setWeight(int newWeight) { weight = newWeight; } public int getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public int getBMI() { return weight/(height*height); }}#Cast in BMI SpreadsheetObjectEditor.edit(new AnIntBMISpreadsheet ((int) 1.77, 75));

#Assignment RulesTVTEveTE TV TE TV Narrower thanWiderthanv = edouble d = 5;v = (TV) eint i = (int) 5.7;! (TE TV || TE TV)v = (TV) ebool b = (bool) 5;#Assignment Rules for Primitive TypesIf 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 T2Expression of type T2 can be assigned to Variable of type T1 with cast#Actual Parameter Assignmentdouble weight;

public void setWeight(double newWeight) {weight = newWeight;}setWeight(70);double newWeight = 70.0;Implicit assignment#Actual Parameter Assignmentint weight;

public void setWeight(int newWeight) {weight = newWeight;}setWeight(70.6);int newWeight = 70.6;Implicit assignment#Actual Parameter Assignmentint weight;

public void setWeight(int newWeight) {weight = newWeight;}setWeight((int)70.6);#Returning a Valuedouble weight;

public double getIntWeight() {return weight;}#Translated into Assignmentdouble weight;

public int getIntWeight() {int getIntWeight = weight;return getIntWeight;}Internal variable#Translated into Assignmentdouble weight;

public int getIntWeight() {return (int) weight;}#Primitive TypesConstants (Literals & Named Constants)Assignment RulesOperations with Invocation Syntax#int Arithmetic OperationsNameActionOperand & Result Type (Signature)+addint, int int-subtractint, int int-negateint int*multiplyint, int int/int quotientint, int int%int remainderint, int int5/2 2 5%2 1 x == (x/y)*yx == (x/y)*y + (x%y)#double Arithmetic OperationsNameActionOperand & Result Type (Signature)+adddouble, double double-subtractdouble, double double-negatedouble double*multiplydouble, double double/int quotientdouble, double double5.0/2.0 2.5 #OverflowInteger.MAX_VALUE + 1 Integer.MAX_VALUEInteger.MIN_VALUE - 1 Integer.MIN_VALUE(double) Integer.MIN_VALUE - 1.0(double) (Integer.MIN_VALUE - 1.0)Double.MAX_VALUE + 1 Double.MAX_VALUEDouble.MIN_VALUE - 1 Double.MIN_VALUE#Divide by Zero10/0 Exception-10/0 Exception10.0/0 Double.POSITIVE_INFINITY-10.0/0 Double.NEGATIVE_INFINITY0/0 Exception0.0/0.0 Double.NaN#int Divide By Zero

#double Overflow

#Mixed Operations5/2.0 5.0/2.0Narrower type convertedint i = (int) (5/2.0)int i = (int) (5.0/2.0)int i = (int) (2.5)int i = 2doube d = 5/(int)2.0double d = 5/2double d = 2double d = 2.0#Strong vs. Weak Typinghello - 1int minusLegal under weak typinganything goesIllegal under strong typingstrict type rules#Miscellaneous Math OperationsOperations (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 doubleMath.PI Math.pow(5, 3) 53 Math.round(5.9) 6 (int) 5.9 5int i = (int) Math.Round(5.9) 6#boolean Constantstruefalseboolean#Relational OperationsNameActionSignature of int ImplementationSignature of double Implementation==equal?int, int booleandouble, double boolean!=not equal?int, int booleandouble, double boolean>greater than?int, int booleandouble, double boolean=greather than or equal?int, int booleandouble, double boolean= 4 true5 32.34) falsetrue || (9654.34/323.13 > 32.34) falseSecond operand not evaluatedShort-circuit evaluationfalse & (9654.34/323.13 > 32.34) falsetrue| (9654.34/323.13 > 32.34) falseSecond operand evaluatedRegular evaluationName(s)ActionSignature!notboolean boolean &&, &andboolean, boolean boolean ||, |orboolean, boolean boolean #Short-Circuit EvaluationName(s)ActionSignature!notboolean boolean &&, &andboolean, boolean boolean ||, |orboolean, boolean boolean false && (10/0 == Integer.MAX_VALUE) falsefalse & (10/0 == Integer.MAX_VALUE)

An error in some programming languages#Complex Expressionsfalse && (10/0 == Integer.MAX_VALUE)Sub-expressionfalse && (10/0 == Integer.MAX_VALUE)Operator evaluation order?#Complex Expressionsfalse && ( 10 / 0 )Sub-expressionfalse && ( 10 / 0 )Operator evaluation order?#boolean vs. Number ExpressionshoursWorked > MAX_HOURSboolean overWorked =True if hoursWorked is greater than MAX_HOURS and false otherwiseint earnings = hourlyWage*hoursWorked + BONUS#boolean Property

#boolean Property Codepublic boolean isOverWeight() {} #boolean Property Code (Edit) final double HIGH_BMI = 28;public boolean isOverWeight() { return getBMI() > HIGH_BMI;} #boolean Property Codeprivate final double HIGH_BMI = 25;

public boolean isOverWeight() {return getBmi() > HIGH_BMI;} #boolean Property Code// declare in interfaceprivate final double HIGH_BMI = 25;

public boolean isOverWeight() {return getBmi() > HIGH_BMI;} #Preventing Invalid BMI

#Operator Precedence! - (T)* / &+ - < > === !=&|&&||UnaryCastfalse && 10 / 0 > 0- 5 - 4!true && false5 / 4 * 3true || false == false || true(int) 5 / 2.0#Operator Precedence (Edit)! - (T)* / &+ - < > === !=&|&&||UnaryCastfalse && 10 / 0 > 0- 5 - 4!true && false5 / 4 * 3true || false == false || true(int) 5 / 2.0#Operator Precedence! - (T)* / &+ - < > === !=&|&&||UnaryCastfalse && 10 / 0> 0 false && ( (10 / 0) > 0 ))- 5 - 4 ( - 5 ) - 4!true && false ( !true ) && false5 / 4 * 3 ( 5 / 4 ) * 3true || false == false || truetrue || ( false == false ) || true(int) 5 / 2.0 ( (int) 5 ) / 2.0#Printing Arbitrary ExpressionsSystem.out.println (2)Output: 2System.out.println (2.0)Output: 2.0System.out.println ((int) 2.0)Output: 2System.out.println (5 > 0)Output: true#