1 Lecture 7_2 Chapter7_2 Page 415 Parameters and Memory Allocation Reference Variables of the String Type as Parameters Example 7-9***************************************** //Program: String Objects as Parameters public class Example7_9 { public static void main (String [] args) { String str = "Hello"; System.out.println ("str before calling method: "+str); stringParameter(str); System.out.println ("str after calling method: " +str); } public static void stringParameter (String pStr) { System.out.println ("in the method stringParameter"); System.out.println ("pStr before changing value: "+pStr); pStr="Sunny Day"; System.out.println ("pStr after changing value: " + pStr); } } //******************************************************* Page 419 The class StringBuffer Page 420 Example 7-10******************************************** //Program: StringBuffer Objects as Parameters public class Example7_10 { public static void main (String [] args) { StringBuffer str = new StringBuffer ("Hello"); System.out.println ("str before calling method: "+str); stringParameter(str); System.out.println ("str after calling method: " +str); 2 } public static void stringParameter (StringBuffer pStr) { System.out.println ("in the method stringParameter"); System.out.println ("pStr before changing value: "+pStr); pStr.append("Sunny Day"); System.out.println ("pStr after changing value: " + pStr); } } //***************************************************** Page 421 Example 7-11******************************************** //Program: String Objects as Parameters public class Example7_11 { public static void main (String [] args) { int num1; IntClass num2 = new IntClass (); char ch; StringBuffer str; num1 = 10; num2.setNum (15); ch = 'A'; str = new StringBuffer ("Sunny"); System.out.println ("Inside main: " + " num1= " +num1 + ", num2 = "+num2.getNum() + ", ch = " +ch + ", and str = " + str); funcOne (num1, num2, ch, str); System.out.println ("After funcOne: " + "num1 = " +num1 + ", num2 = "+num2.getNum() + ", ch = "+ ch 3 +", and str = " + str); } public static void funcOne (int a, IntClass b, char v, StringBuffer pStr) { int num; int len; num = b.getNum(); a++; b.addToNum (12); v='B'; len = pStr.length(); // pStr.delete (0,len); pStr.append ("Warm"); System.out.println ("Inside funcOne: \n" + "a= " +a + ", b = "+b.getNum() + ", v = "+ v + ", pStr = "+pStr + ", len = "+len + ", and num = " +num); } } //****************************************************** public class IntClass { private int x; public IntClass ( ) { x = 0; } public IntClass ( int num ) { x = num; } public void setNum ( int num ) { x = num; 4 } public int getNum ( ) { return x; } public void addToNum (int num ) { x = x + num; } public void multiplyToNum ( int num ) { x = x * num; } public int compareTo ( int num ) { return (x - num); } public boolean equals ( int num ) { if (x == num) return true; else return false; } public String toString ( ) { return (String.valueOf( x )); } } //**************************************************** Page 429 Scope of an Identifier Within a Class The scope of an identifier refers to what parts of the program can “see” an identifier, that is, where it is accessible (visible). Local identifier: An identifier declared within a method or block and that is visible only within that method or block 5 Java does not allow the nesting of methods. That is, you cannot include the definition of one method in the body of another method. Within a method or a block, an identifier must be declared before it can be used. A block is a collection of statements enclosed within braces. A method’s definition can contain several blocks. The body of a loop or an if statement also form a block. Within a class, outside every method definition (and every block), an identifier can be declared anywhere. Within a method, an identifier used to name a variable in the outer block of the method cannot be used to name any other variable in an inner block of the method. For example, in the following method definition, the second declaration of the variable x is illegal: public static void illegalIdentifierDeclaration( ) { int x; // block { double x; //illegal declaration, x is already declare …. } } Description of the scope rules of an identifier declared within a class and accessed within a method (block) of the class. In chapter 8, we describe the rules for an object to access the identifiers of its class.) An identifier, say x, declared within a method (block) is accessible: Only within the block from the point at which it is declared until the end of the block. By those blocks that are nested within that block. Suppose x is an identifier declared within a class and outside every method’s definition (block): If x is declared without the reserved word static (such as a named constant or a method name), then it cannot be accessed within a static method. 6 If x is declared with the reserved word static (such as a named constant or a method name), then it can be accessed within a method (block), provided the method (block) does not have any other identifier named x. For example: the statement for (int count=1; count < 10; count++) System.out.println (count); Declares the variable count and initializes it to 1. The scope of the variable count is limited only to the body of the for loop Page 430 Example 7-12 public class ScopeRules { static final double rate = 10.50; static int z; static double t; public static void main (String [] args ) { int num; double x, z; char ch; //… } public static void one (int x, char y) { //.. } public static int w; public static void two (int one, int z) { char ch; int a; // block three { int x = 12; //… 7 }//end block three //… } } Page 431 Table 7-3 Scope (Visibility) of the Identifiers Identifier Visibility in Visibility in Visibility in one two block three rate(before Y Y Y main) z(before main) Y N N t(before main) Y Y Y main Y Y Y local variables N N N of main one(method Y Y Y name) x(one’s formal Y N N parameter) y( one’s Y N N formal parameter) w(before method two) two(method name) one (two’s formal parameter) z(two’s formal parameter) local variables of two x(Block three’s local variable) Visibility in main Y N Y Y Y Y N N Y Y Y Y Y Y Y Y N Y Y N N Y Y N N Y Y N N N Y N Page 432 Method Overloading: An Introduction In Java, several methods can have the same name within a class. This is called method overloading or overloading a method name. 8 Def. Two methods are said to have different formal parameter lists if both methods have: A different number of formal parameters, or If the number of formal parameters is the same, then the data type of the formal parameters, in the order you list, must differ in at least one position. Method headings: public void methodOne (int x) public void methodTwo ( int x, double y) // These methods have different formal parameter lists. Method headings: public void methodOne (int x, double y, char ch) public void methodTwo ( int one, double two, char firstCh) // These methods have the same formal parameter lists. To overload a method name, within a class, any two definitions of the method must have different formal parameter lists. Method overloading: Creating several methods, within a class, with the same name. The signature of a method consists of the method name and its formal parameter list. (Note that the signature of a method does not include the return type of the method.) If a method’s name is overloaded, then all the methods (with the same name) have different signatures if they have different formal parameter lists. These following method headings correctly overload the method AAA: public void AAA ( ) public void AAA (int x, double y) public char AAA (double one) public int AAA (int x, double y, char ch) These method headings to overload the method BBB are incorrect. Method headings: public void BBB (int two, double one) public int BBB ( int x, double y) 9 // These methods have the same formal parameter list and the same name. But the return types of these method headings are different. Compiler will generate a syntax error.