Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 6
Note Set 6 Overview Methods – In Depth Return values and the call stack Argument promotion/casting Random Number Generation Scope
Understanding Calls - Stack Data Structure Understanding stacks is useful for understanding return Stacks are LIFO Last In – First Out Last value put in (pushed) is first value removed (pop) When method A calls method B, the system (jvm) must know where to return to in A when B is finished Activation Record – information on local variables in a method
Argument Promotion and Casting Arguments can have different data types from parameters under certain conditions Header for square root public static double sqrt(double a) Expects a double, but can be sent an int. Int would be cast to double System.out.println(sqrt(4)); Expects a double, but can be sent an int. Int would be cast to double System.out.println(sqrt(4));
Valid promotions May get compile time errors if you try to implicitly “demote” a type double to int Use explicit casting in this case square( (int) doubleValuedVariable );
Random Number Generation used in simulation, games, cryptography Use Random Class that is in java.util can generate random boolean, byte, float, double, int, long Produce numbers using mathematical function with VERY large period Random randGenerator = new Random() int randomValue = randGenerator.nextInt(); double doubleRandom = randomGenerator.nextDouble();
Random Ints Produces values from -2,147,483,648 to 2,147,483,647 Limit between 0 and n int x = randGenerator(24) Will produce pseudo sequence of numbers on the range [0, 23]. How would you produce numbers between m and n, m > n and m >= 0?
Random Generator Because it is a function, will produce the same sequence of numbers unless you tell it where to start every time Random randGenerator = new Random() Random randGenerator = new Random( 25 ); Random randGenerator = new Random(???) Need something that changes every time the program is run. Ideas???
Scope of Variables declaration introduces a entity with a name that can be referred to in code. Scope – the portion of the program that can refer to the declared entity by name Scope Rules scope of parameter declaration is the body of the method in which the declaration appears scope of local variable is from the point of declaration to the end of the block scope of variable that appears in the initialization section of a for loop header if for the body of the for statement and other parts of for header scope of a method or field of a class is the entire body of the class
Shadowing Local variable in an instance method with same name as an instance variable shadows the instance variable. public class Test { private int x; public void someMethod() { int x = 5; System.out.println(x); } Will access Local variable x instead of instance variable x Will access Local variable x instead of instance variable x
Method Overloading Overloaded Methods – Methods in the same class that have the same name but different sets of parameters determined by the number, types and order of parameters Differences in return type are irrelevant when determining if 2 methods are overloaded public class X { public void myMethod (int x, int y) { } public int myMethod (float z, float y){ } public class TestX { public static void main (String [] args) { X myVar = new X(); myVar.myMethod(1, 2); myVar.myMethod (3.0, 5.0); }
Example
Overloading Compiler distinguishes methods by their signature combination of the method’s name and the number, types and order of its parameters. Remember – Return type of method DOES NOT MATTER Overloaded methods do not need to have the same number of parameters public class Tester { public int myMethod(int) { } public void myMethod(int) { } Still ambiguous to the compiler
Parameter Passing Concepts
Primitive Variables public class Test1 { public static void foo(int x) { x++; } public static void main (String[]args) { int z = 10; foo(z); } } z x when method is called, value of z is copied into x. We can conclude that…
Passing an Object Reference public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] p = java.awt.Point[x=2,y=3] now p = java.awt.Point[x=10,y=20] OUTPUT:
Passing an Array public class Test3 { public static void fun(int [] x) { x[0] = 99999; } public static void main(String [] args) { int[] values = new int[3]; values[0] = 1; values[1] = 2; values[2] = 3; System.out.printf("value[0] = %d\n", values[0]); fun(values); System.out.printf("value[0] = %d\n", values[0]); } value[0] = 1 value[0] = value[0] = 1 value[0] = OUTPUT:
What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } Main: p 2 3 bar: x The object reference stored in p gets copied into x. System Stack
What’s going on here? public class Test2 { public static void bar(Point x) { x.setLocation(10, 20); } public static void main(String [] args) { Point p = new Point(2, 3); System.out.printf("p = %s\n", p.toString()); bar(p); System.out.printf("now p = %s\n", p.toString()); } } Main: p bar: x The object reference stored in p gets copied into x. System Stack
Strings mess it up public class Test4 { public static void main (String [] args) { String myString = "Mark"; System.out.printf("MyString 1.0 = %s\n", myString); function(myString); System.out.printf("MyString 2.0 = %s\n", myString); } public static void function(String s) { s = "Hello”; System.out.printf ("s = %s\n", s); } } MyString 1.0 = Mark s = Hello MyString 2.0 = Mark MyString 1.0 = Mark s = Hello MyString 2.0 = Mark OUTPUT: What’s going on here?