References and Java Memory Management
> Click or hit Control-Enter to run the code above
You’re Halfway There
Congratulations!
Before/After: Content
-
Before: imperative and object-oriented programming
-
After: algorithms, data structures, recursive and functional programming
Before/After: Assessments
-
Before: 3 short MPs, one long MP, one midterm
-
After: 2 long Android MPs, one Android final project, two midterms
Current Status
Overall the class is doing quite well:
-
Without drops: median 82, average 79
-
With drops: median 94, average 89
Midterm Grades
We will post course grade statistics on our grading page today or tomorrow.
-
This information is intended to help you decide whether to continue in the course or not
-
Have feedback you want to share? Use the anonymous feedback form.
Questions?
Anything is fair game.
Homework Review: Creator
Implement a public class called Creator
.
Creator
should provide one public class method called newOrOld
taking a
single boolean
argument.
If the argument is true
, newOrOld
should return a reference to a new
Creator
instance.
If the argument is false
, newOrOld
should return a reference to the last
Creator
created by newOrOld
.
If newOrOld
has never been called before, you should return null
.
Creator
should not expose any public state or allow itself to be extended.
> Click or hit Control-Enter to run Example.main above
Review: Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
System.out.println(me.age);
In Java methods receive a copy of a reference to the passed object.
So they can modify the object the reference refers to.
Review: Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
Review: Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
Review: Pass By (Copied) Reference
class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
}
int birthday(Person toSet) {
toSet.age++;
return toSet.age;
}
Person me = new Person(38);
System.out.println(birthday(me));
System.out.println(me.age);
References Are Powerful
References are a widely-used idea in computer science.
By controlling the process of dereferencing or translating a reference we can:
-
move objects from place to place: just like your phone can move around with the same number
-
block translation in certain cases: just like you can reject email from certain senders
And Memory Management
(Or why you never need to delete
anything in Java…)
Java Memory Management
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
}
Java utilizes references to automatically clean up unused objects to reclaim memory—a process known as garbage collection.
-
If a reference to an object exists, it must still be useful, so keep it
-
If no reference to an object exists, it cannot be used, so remove it
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 0
}
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 1
}
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 2
}
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 3
}
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 3
}
Reference Counting Example
String save;
for (long i = 0; ; i++) {
String s = new String(i);
if (i == 0) {
save = s;
}
// i == 3
}
Questions About Object References?
This concept is critical once we start talking about data structures and algorithms next week.
Review: What Is An Interface?
Interface: a shared boundary across which two or more separate components of a computer system exchange information.
-
Interfaces can be between two pieces of software, between software and hardware, between computers and their users, or between various permutation of these components.
-
Interfaces enable different parts of a system to interact in a structured way.
Java Interfaces
public interface Add {
int add(int first, int second);
}
-
Java interfaces look like empty objects: just method signatures with no implementation.
-
Interfaces can declare both methods and variables.
-
However, interfaces variables are
public static final
by default, meaning that they are only useful for declaring constants.
Implementing Interfaces
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
}
-
Interfaces don’t do anything useful by themselves. Instead, they have to be implemented by specific classes.
-
To declare that a class implements an interface you use the
implements
keyword as shown above. -
To implement an interface you must implement all of the methods that it declares.
> Click or hit Control-Enter to run Example.main above
Interface Casting
public interface Add {
int add(int first, int second);
}
public class Adder implements Add {
public int add(int first, int second) {
return first + second;
}
public int multiply(int first, int second) {
return first * second;
}
}
Add add = new Adder();
System.out.println(add.add(10, 20));
// But this doesn't work because multiply is not part of the add interface
System.out.println(add.multiply(10, 20));
-
Similar to inheritance I can automatically cast an object reference to any interface that it implements.
-
However, if I do that I can no longer access methods that are not part of the interface.
> Click or hit Control-Enter to run Example.main above
Interfaces v. Inheritance
So far this seems very similar to inheritance and overloading.
-
The interface is like the parent class
-
implement
is likeextends
-
Providing your own implementation is like overriding a parent’s method
abstract
Methods
It’s actually even more similar than it seems.
Remember abstract
classes?
abstract
classes can also have abstract
methods:
public abstract class Add {
public abtract int add(int first, int second);
}
So Why Interfaces?
Added Flexibility
Sometimes we want to mix capabilities from different branches of the tree.
Multiple Inheritance
public interface Add {
int add(int first, int second);
}
public interface Subtract {
int subtract(int first, int second);
}
public class Mathy implements Add, Subtract {
public int add(int first, int second) {
return first + second;
}
public int subtract(int first, int second) {
return first - second;
}
}
Unlike inheritance, classes can implement multiple interfaces.
> Click or hit Control-Enter to run Example.main above
Interface as Contract
/**
* Compares this object with the specified object for order.
*
* Returns a negative integer, zero, or a positive integer as this object is
* less than, equal to, or greater than the specified object.
*/
public interface Comparable {
int compareTo(Object other);
}
Interfaces represent a contract between the interface provider and the interface user.
The interface represents all that the two components on either side need to agree on for things to work correctly.
Interface as Contract
public interface Comparable {
int compareTo(Object other);
}
By implementing
Comparable
you commit to being able to compare two instances of your class.
Using this ability I can implement code that:
-
sorts an array containing instances of your class
-
finds the maximum or minimum value of multiple instances of your class
-
arranges instances of your class into a binary tree 1
Interface as Abstraction Barrier
public interface Comparable {
int compareTo(Object other);
}
Good interfaces also represent a barrier between two unrelated parts of a computer program or system.
-
If I implement
Comparable
I don’t need to worry about how my implementation is used, but suddenly my class will have many new desirable features -
If I use
Comparable
I don’t need to worry about how the interface is implemented but I know that I can correctly compare two objects
> Click or hit Control-Enter to run Example.main above
Announcements
-
MP4 is out and due in less than 2 weeks. It’s our first Android MP and we’ll do Android setup in lab this week.
-
I have office hours today from 10AM–12PM. If you need to do an instructor interview please come by at 11AM if possible. I’m tired of talking about myself and am not going to grant any more of these.
-
Please provide feedback on the course using our anonymous feedback form.