Download presentation
Presentation is loading. Please wait.
1
Lecture 10: More on Methods and Scope
CS201
2
Value and Reference So far, parameters (with one exception) have been passed by value The calling method makes a copy of the value and sends it to the called method The called method sets up a variable, assigns the parameter value to it, and, presumably, uses it. If the parameter was a variable, its value in the calling method does not change This is how primitive data types, like int, double, and char are passed to and from methods
3
Value and Reference With a primitive data type, if you want to change the value of the variable in a calling method but it is not in scope in the called method, you can return the correct data type from the called method and assign the return value to the variable: x = getNewX(); Note the difference between this and the situation with myInt in MethodScopeDemo from the last lecture
4
Value and Reference Sounds pretty easy so far, right? Not any more!
The demos in the last lecture used primitive types like int and double It’s more complicated when you pass an object, such as a String This material is complicated but very important. There will certainly be problems on the quizzes and final exam that require you to understand it thoroughly.
5
Reference In Java, a reference to an object in memory is the memory address where the object can be found. A variable of a primitive data type holds the actual value int myInt = 1; A variable whose type is a class, like Scanner or String, holds a reference to the object.
6
Value and Reference Consider this code: StringBuilder sb = “Hi, Mom”;
All parameters are passed by value as described above BUT objects are not passed. Instead, we pass-by-value variables that contain references to objects Consider this code: StringBuilder sb = “Hi, Mom”; sb is a reference to the StringBuilder, not a copy of the StringBuilder itself. We need to “dereference” the reference to get the actual StringBuilder.
7
Value and Reference Other languages (eg Visual Basic) use the concept “pass by reference” Some languages, like C, allow the programmer to choose whether to pass by value or by reference In Java, objects can’t be passed In Java we conventionally say we are passing the reference variable by value. Its value is the reference, so it still pass by value. In Java terminology, this is called passing a reference by value.
8
Reference It will be easier to remember this if you think about the reasons for it. Primitive data types consume predictable and manageable amounts of memory 4 bytes for an int, 8 bytes for a double It is easy to make copies of these to send back and forth Objects, instances of classes, are, in principle, unbounded in size and may be very large. A String may contain no characters at all, but it might also contain the text of the Bible. The expense of copying an object to send to a method is unpredictable and may be very large.
9
Value and Reference If the method that receives a reference-type variable as a parameter changes the object, we will see the changes from anywhere where the object is in scope No need to return the new value Remember that this is how we pass objects, instances of classes like String or Scanner.
10
Value and Reference public class NoPassByReferenceDemo{ public static void main(String[] args) { StringBuilder tagLine = new StringBuilder("Ah, Satan sees Natasha"); System.out.println(tagLine); otherMethod(tagLine); } public static void otherMethod(StringBuilder myStringBuilder) { myStringBuilder.reverse();
11
Value and Reference Here is what happened in the code above:
We passed the reference variable to otherMethod() Then we used it to find the object and changed the object, but not the reference variable After that, the value of the reference is still the same – it shows where to find the object. But, the object has changed We see the new state of the object when we access it from wherever it is in scope This point is difficult but very important! If you don’t understand it after this lecture, study the last few slides carefully. If you still don’t understand it, come to my office hours.
12
Pass By Value Passing a primitive data type by value is like making a copy of the value and handing it to the called method
13
Pass A Reference By Value
Passing a reference to an object is like telling the called method where to find the object
14
Copying Variables of Primitive Data Types and Object Types
15
Method Overloading Scary-sounding term for a very simple concept
You can’t have two methods in the same class with the same name and same method signature But you can have two methods with the same name and different signatures
16
Method Overloading Scary term for a very simple concept
You can’t have two methods in the same class with the same name and same method signature But you can have two methods with the same name and different signatures
17
import javax.swing.JOptionPane; public class Overload{ public static void main(String[] args){ int x = 0; x = getInt(); JOptionPane.showMessageDialog(null, "You chose " + x); int min = 2; int max = 10; x = getInt(min, max); } public static int getInt(){ int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter an integer")); return x; public static int getInt(int low, int high){ int val = low -1; do{ val = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter an integer between " + low + " and " + high)); } while(val < low || val > high); return val;
18
Lurking Troll Principle
Never write a method with more code than you can see on the screen all at once. What you can’t see will hurt you.
19
Stacks A stack is a way of ordering data so that the last value you add to the stack is the first one you use from it. Like a stack of plates in a cafeteria Last In, First Out (LIFO)
20
Stacks Stacks have many uses in programming
The one that concerns us in this class is that this is how the JVM keeps track of method calls
21
Call Stacks
22
i is declared and initialized
animation Trace Call Stack i is declared and initialized
23
j is declared and initialized
animation Trace Call Stack j is declared and initialized
24
animation Trace Call Stack Declare k
25
animation Trace Call Stack Invoke max(i, j)
26
pass the values of i and j to num1 and num2
animation Trace Call Stack pass the values of i and j to num1 and num2
27
pass the values of i and j to num1 and num2
animation Trace Call Stack pass the values of i and j to num1 and num2
28
animation Trace Call Stack (num1 > num2) is true
29
animation Trace Call Stack Assign num1 to result
30
Return result and assign it to k
animation Trace Call Stack Return result and assign it to k
31
The Stevie Wonder Principle
When you believe in things that you don't understand, then you suffer. You should be able to explain any method you write in one sentence. If you can't, break it down into two methods. Repeat this process until all your methods are easy to understand.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.