10/6/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 16:Method OverloadingLast time:1. APIs, comments and documentationToday:1. Project #3 due Sunday2. Exam #1 coming 10/113. Method overloadingCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #3 Due Monday It is due Monday, 10/9 at 11 pm Try to turn it in earlier (today if possible) The project is open Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Exam #1 Is 10/11 in Discussion SectionTake the test in your own section Test will cover material since beginning of semester Test will be closed book, closed neighbor Start studying now!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Why Is It Called “The Stack”? Stack is part of main memory used to hold variables and their values When a variable is referenced, its value is found by looking in stack When methods are invoked, temporary additions (“stack frames”) are made to stack before body of method is executed. So why is it called a “stack”?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Stacks in Computer Science A stack is a data structure (“device” for holding values) Three operations on a stack push: add a new value into the stack pop: remove the most recently added value still in stack top: return the most recently added value in stack Think: stack of plates in a restaurant push = put new plate on top pop = remove top plate top = look at top plateCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Example S.push (3); S.push (4); S.top == ??4 S.pop (); S.push (5); S.top == ??5S345CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6What Do “Stacks” Have To Do with “The Stack”Main memory: “stack” is short for “call stack” “call stack” = information for processing method calls Contents of call stack: stack frames! At beginning of method call, new stack frame pushed onto call stack After body of method has finished executing, stack is “popped”CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7i 2Example public class C {int f (int i) {return (i+1);}int g (int i) {return (1+f(i+1));} What is printed by:System.out.println(C.g(1)); 4HeapStack……i 1stack framestack frameCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Local Variables and the Stack Local variables are treated like method arguments When declared, they are added to current stack frame When stack frame is popped, those local variables disappear Whenever a new block is introduced, so is a new stack frame{int x = 3;…} This introduces a new stack frame associating x with 3 When the block is finished, the stack frame goes awayCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Initialization of Instance Variables Instance variables are assigned values even if none explicitly provided Primitives are set to 0 / false References are set to null (i.e. point to nothing) Consider:public class C {public int x;}…C cObj = new C ();System.out.println (cObj.x); 0 is printed Don’t rely on this in your programs! Compliers famously get this wrong Better practice: Set default values when variable is declared in class;ublic tnt x = 0; In constructors, supply alternative values as desiredCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Initialization of Local Variables Local variables are not initialized. Java checks if variables are uninitialized If use of uninitialized variable is possible, the Java compiler (and Eclipse also) complain Example:public void f() {int x;int z = x + 3; // won't compile} What about static variables? They are treated like instance variables class C {public static int X;} Expression C.X evaluates to 0 Good practice: always initialize variables explicitly Avoids compiler errors (local variables) You don’t have to remember different treatments of local, instance variables You don’t run the risk of a misbehaving compilerCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Method Overloading Overloading: declaration of two methods in the same class with same name Java allows overloading The different methods must have different argument lists e.gpublic class C {int f ();int f (int f); // OK} Why use overloading? To allow similar operations to have the same name We have seen overloading before with constructorsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Example Recall Date class print method// The method for converting dates to stringspublic String toString () {return (month + separator + day + separator + year);}// Method for printing dates to System.outpublic void print () {System.out.print (toString ());} May want another print method that allows specification of surrounding textpublic void print (String pre, String post){System.out.print (pre);print ();System.out.print (post);} Java allows this What is output ofDate d = new Date ();d.print (“The date is “, “.”);CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13When Is Overloading Allowed? Terminology: Prototype: I/O types, behavior (public, static, etc.) for methodpublic static void f(int x, float y) Signature: Input types for methodf(int x, float y) You can only overload a methods if the signatures are differentvoid f (int)int f (float) // fine Just having different return types is not enough:void f (int)int f (int) // not fine Why? Because compiler can’t always tell which f to call just based on return typef(3); // Which f should be
View Full Document