CS151 lecture notes, Fall 1999 Week 5, Wednesday Reading: Chapter 5 Quiz Friday: I will specify a method and you will write it It will involve logical operators. Review of method invocation --------------------------- Very important: 1) arguments get evaluated by the caller 2) the resulting VALUES get passed to the callee 3) the callee assigns those values to the parameters An interesting example of recursion ----------------------------------- What does this program do? public static void zippo (int x) { if (x == 0) { return; } else { binary (x/10); } System.out.print (x%10); } public static void main (String[] args) { zippo (345); System.out.println (""); } } The call stack for this program looks like: x = 345 x = 34 x = 3 x = 0 The x=0 instance returns immediately without printing anything. The x=3 instance prints 3%10 = 3 The x=34 instance prints 34%10 = 4 The x=345 instance prints 345%10 = 5 Since it uses print, 345 appears on a single line. main prints a newline to terminate the line. Is this a useful method? I mean, we can already print things using println. Well, sure, but this is not too different from the way println works (I assume). The problem is that integers are stored in the computer as binary numbers (base 2). In order to print them we have to do a bunch of arithmetic to extract all the digits. How would you modify zippo to print numbers in binary? Methods that return things -------------------------- public static void means "void method -- returns nothing" public static int means the method returns an int public static double means the method returns a double public static String means the method returns a String methods can only return one value, with one type You use the return statement to specify the return value. The return statement can contain any expression, but it must have the promised type. public static int square (int x) { return x * x; } You can have more than one return statement in alternate blocks: public static int absValue (int x) { if (x < 0) { return -x; } else { return x; } } As soon as the return statement is executed, the method is over. Flow of execution returns to whoever invoked the method, and any subsequent statements are not executed. If you define a method that returns a value, you can use the method invocation as part of an expression. int x = square (3) + square (4); Java knows the type of a method invocation by looking at the method definition. The compiler knows that the following is wrong: String y = absValue (int x); Because it knows that the return value from absValue is an int. Overloading ----------- You can have more than one method with the same name, as long as they take different arguments. When you invoke an overloaded method, the compiler knows which version you want to invoke by looking at the types of the arguments. As always, the compiler can tell the type of an expression. Overloaded methods can have different return types. Example: Math.abs, Math.min, Math.max are all defined for both int and double. In each case, the return value is the same type as the arguments. Other example: print and println are overloaded. There is a version that accepts a single int, another version that accepts a single double. Same for String and boolean. Overloading is a useful feature, but it can make code hard to read because it is not always clear what method is getting invoked.