Presentation is loading. Please wait.

Presentation is loading. Please wait.

AAA. Today’s lecture Review of Chapter 6 Go over examples.

Similar presentations


Presentation on theme: "AAA. Today’s lecture Review of Chapter 6 Go over examples."— Presentation transcript:

1 AAA

2 Today’s lecture Review of Chapter 6 Go over examples

3 Chapter 6 review What is a static method? (A static variable?) How does Java handle assignment (during method calls)? What is scope? Local? Parameter? Instance? Class? What is visibility? Public? Private?

4 Copying parameters Example What value gets printed? public class Test { public static void main(String[] args) { int x=3; changeVal(x,5); System.out.println(x); } public static void changeVal(int p, int v) { p = v; } ("3" is printed.)

5 But what does this mean for objects? What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p.setName(n); } Thomas ( Java copies the reference)

6 How about now? What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p = new Person(n,30); } Mason(new object created/modified/discarded) Does it matter that we have a local variable called p in changeName ? What if it were called q ?

7 How about now? (v2) What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); p = changeName(p,"Thomas"); System.out.println(p.getName()); } public static Person changeName(Person q, String n) { q = new Person(n,30); return q; } "Thomas" (new object created/modified/returned, THEN p is updated to point to it.) Does it matter that we have a local variable called p in changeName ? What if it were called q ?

8 Reference Assignment continued Why are the references copied, instead of the entire object? Objects can be big Wastes space Takes a long time to copy What can be a downside to copying just the reference? Now you have two things pointing to the same location in memory What happens if you forget this mistakenly?

9 Creating lots of objects We know how to create and allocate memory to programs with new in Java Could our program ever run out of memory? ◦ Not if it is super small ◦ What if you run something for weeks that is constantly creating objects it only uses once? Want a way to retrieve (recover) memory that is no longer being used ◦ Turns out this is tricky! How do we know an object is no longer needed?

10 Coming up: String Methods Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection

11 Method Overloading (Quick View) We can have multiple methods with the same name in one class! They are distinct methods with no actual relations other than the name – this is just a mnemonic convenience! To coexist, their method signatures must be uniquely identifiable by the parameter types and method name alone. parameter names are ignored – arguments are nameless. return type is ignored – not explicit via method call, so can't help select correct method. Constructors are methods too – we can write multiple constructors for one class, as long as they have different signatures. This is a valuable opportunity (something Python disallows).

12 Method Overloading - Examples Example: these methods may all be in one class: 1.public int foo (int a, int b){…} 2.public int foo (char a, int b){…} 3.public int foo (int b, char a){…} 4.public int bar (int a, int b){…} 5.public String foo (int a, int b, int c){…} The following could not be added to the class: public String foo (int a, int b) {…}return type irrelevant public int foo (int other, int names){…}param names irrelevant private int foo (int a, int b){…}modifier irrelevant

13 © 2004 Pearson Addison- Wesley. All rights reserved 4-13 Visibility Modifiers publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation

14 © 2004 Pearson Addison- Wesley. All rights reserved 4-14 Let’s go over the examples

15 © 2004 Pearson Addison- Wesley. All rights reserved 4-15 Questions?


Download ppt "AAA. Today’s lecture Review of Chapter 6 Go over examples."

Similar presentations


Ads by Google