DOC PREVIEW
UMD CMSC 131 - Lecture 17: Implicit Promotion

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 17 Implicit Promotion Last time 1 Method overloading Today 1 Project 3 due today at 11 pm 2 Exam 1 on Wednesday 3 Implicit type promotion 4 this 10 9 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Exam 1 Wednesday in Discussion Section Take the test in your own discussion section Test will cover material since beginning of semester Test will be closed book closed neighbor Start studying now CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Recall Type Casting Type casting conversion of values from one type to another Type casts can be implicit int x 3 float y x value of x converted to float They may be explicit float x 3 0F int y int x Java only performs implicit upcasts int x 3 float y x OK byte z x compiler error CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Implicit Promotion Another term for implicit type casting Used in context of method inputs outputs Consider void f float q The call f 7 is valid 7 implicitly cast promoted to type of input float Stack frame used to evaluate body of f associates promoted value to q Implicit promotion also used in conjunction with return values double foo return 7 7 promoted to double CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Promotion and Math Library Promotion used frequently in conjunction with Math library Consider prototype for square root function public static double sqrt double May be called as follows sqrt 7 7 promoted to double CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Ambiguities in Promotion and Overloading Consider the following prototypes public void f int x double y public void f double a int b f is overloaded Both versions of f have two inputs but types are different so Java is OK The following calls are OK f 3 10 0 f 7 7 5 first version of f called second version of f called What about f 3 7 First version could be called by promoting second argument to double Second version could be called by promoting first argument to double Java flags this as an error Error Unresolved compilation problem The method f int double is ambiguous for the type CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 this this keyword for referring to current object Example Recall definition of setYear method in Date class public void setYear int newYear year newYear This is equivalent to public void setYear int newYear this year newYear CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Why this One use method parameter names that match instance variable names Re implementation of setYear public void setYear int year this year year Without this parameter would need different name why More interesting consistency checking in methods Suppose we have following consistency checker in Date class see Date java for this class meeting public static boolean isValid Date d Takes Date object as input Checks that year is 0 month is 1 12 day value correct for month Then can implement setYear as follows public void setYear int newYear int oldYear year year newYear if Date isValid this System out println Invalid year argument to setYear year oldYear Restore old year value This ensures only valid dates are set CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland newYear 7 Why this cont Even more interesting consistency checking in constructors Goal ensure only valid dates created Recall old Date constructor Date int newDay int newMonth int newYear day newDay month newMonth year newYear No consistency check on values A better approach Make assignments to instance variables Check consistency If inconsistent return default object CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Re implementing Constructor New implementation Date int newDay int newMonth int newYear day newDay month newMonth year newYear if isValid this restoreDefaults Notes restoreDefaults resets instance variables to default values this method also needs to be implemented You can see implementation in Date java for this class meeting posted on web site Need for this in implementation due to static declaration of isValid CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Review Organizaton of a Java Program One file for each class Each file has Possibly a package membership specification Possibly some import statements One public class with the same name as the file name e g 3rd project package photoLab import cmsc131PictureLibrary public class PicViewer Java compiler included with Eclipse generates bytecode files what are these E g Date java Eclipse handles this Foo java compiles to Foo class which contains bytecodes class files are run by Java Virtual Machine To execute an executable program there must be a method named main with prototype public static void main String args CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Review Classes and Objects Objects have two components state data stored in instance variables operations methods Class Blueprint for an object Defines variables aka fields and implementations of methods Fields methods together sometimes called members An object is an instance of a class You use new to instantiate an object The object goes on the heap Two kinds of variables Primitive hold a value Reference either point to an object or set to null CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Review Class Members Visibility for class members Three categories of variables public anyone can access private accessible in SAME class Non static each object has its own copy Static one copy per class Local temporary variables defined in methods Methods Return type void or something else primitive or object reference Parameters placeholders for inputs provided by caller of method Two categories Non static Static May refer to instance variables in body Can only be invoked via an object created from the class Can only refer to static variables in body No need to create object in class in order to invoke Overloading same name but different parameter list CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 More Review See notes from Lecture 15 Look at review questions on web site CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13


View Full Document

UMD CMSC 131 - Lecture 17: Implicit Promotion

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 17: Implicit Promotion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 17: Implicit Promotion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 17: Implicit Promotion and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?