Review of References
> Click or hit Control-Enter to run Example.main above
ACM Mental Health Awareness Night
Tuesday (tomorrow) 10/15 from 8–9PM in Siebel 2405.
Mental Health in Computer Science
Don’t be a computer.
-
Computers are never wrong.
-
Computers incessantly point out human error.
Mental Health in Computer Science
Be not a computer.
-
If a computer can do it, it’s not uniquely human.
-
Try to emphasize your human qualities when interacting with others—especially with computer scientists.
-
Also join the Computer Support Group that meets daily in Siebel 0403…
Speaking of Being Wrong…
The machine project is intended to be confusing and frustrating at times… but not this confusing and frustrating.
The Machine Project is Too Hard
That’s entirely my fault, and I apologize.
-
When we try new things we sometimes have to make adjustments, and probably should have made this one sooner.
-
It’s also been putting a huge amount of stress on the course staff, who have been handling things extremely well.
-
And you’re also all doing quite well, despite the increased difficulty level.
Adjusting the MP Difficulty Level
Here’s what we’re going to do.
-
We’re adjusting the length and difficulty level of the remaining parts of the MP
-
We’ve also provided everyone with 1 MP checkpoint drop. (The grades page is up-to-date as of this morning.)
-
The midterm grades that went out over the weekend do not include this change.
Questions or Concerns?
I have office hours today from 1–3PM if you want to come by and chat.
Review: References
Reference: a value that enables a program to indirectly access a particular datum, such as a variable’s value or a record, in the computer’s memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.
Review: Java Reference Variables
class Person { }
/*
* me is declared to hold a reference to an object of type Person,
* but currently refers to nothing.
*/
Person me;
/*
* Initializing an instance to null is another way of indicating
* that it currently refers to nothing.
*/
Person you = null;
me = new Person(); // Now me refers to a new Person object
you = me; // Now me and you refer to the same Person object
System.out.println(you == me);
you = new Person(); // Now you refers to a new Person object
System.out.println(you == me);
We can refer to a Java variable that refers to an object as a reference variable.
Review: References Are Not Objects
References are not the thing the refer to.
Some real-world references include:
-
A phone number: which refers to a phone
-
A street address: which refers to a physical location
-
A social security number: which refers to a person
Review: Copying References
Copying a reference does not copy the object it refers to.
-
Copying a phone number doesn’t copy the phone. Anyone with the number can call the same person.
-
Copying a street address doesn’t copy the location. Anyone with the address can navigate to the same spot.
-
Copying a social security number doesn’t copy the person 1. Anyone with social security number may be able to impersonate that person.
Copying References
class Person {
public int age;
}
Person me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 10;
Copying References
class Person {
public int age;
}
Person me = new Person();
Person you = me;
me.age = 10;
System.out.println(you.age);
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(40);
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.
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(40);
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(40);
System.out.println(birthday(me));
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(40);
System.out.println(birthday(me));
System.out.println(me.age);
> Click or hit Control-Enter to run Example.main above
How To Copy Objects
public class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
Person(Person other) {
this.age = other.age;
}
}
If we want to copy an object, we have a few options:
-
Object
provides aclone
method -
You can implement a copy constructor as shown above
Shallow v. Deep Copies
public class Person {
public Pet pet;
Person(Person other) {
this.pet = other.pet;
}
}
What is a potential problem with the copy constructor shown above?
-
It only copies the reference to the
Pet
object. So both the existing and the new object will share the samePet
object. -
This is called a shallow copy. A deep copy copies all of the objects so the old and new object share nothing.
Review: Reference v. Object Equality
public class Person {
public int age;
Person(int setAge) {
this.age = setAge;
}
boolean equals(Person other) {
return this.age == other.age;
}
}
Person me = new Person(40);
Person other = new Person(40);
System.out.println(me == other);
System.out.println(me.equals(other));
-
If two references are equal then they refer to the same object, and
.equals
is almost always true. -
If two references are not equal, the class may still define
.equals
to be true depending on the value of the instance variables.
References and Method Overriding
Note that Java uses the reference type, not the object type when matching method signatures.
-
If the reference type doesn’t match, Java will upcast until it finds a match or the call fails
> Click or hit Control-Enter to run Example.main above
Questions About Object References?
This concept is critical once we start talking about data structures and algorithms next week.
Interfaces
Interfaces are an incredibly important idea when building computer programs and systems.
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.
Examples of Computer Interfaces
-
Software-software: between the test cases that we write and the code that you complete for each MP or homework problem.
-
Software-hardware: between my laptop and the Chromecast that is displaying today’s lecture slides.
-
Computer-user: computer displays, keyboards, pointing devices, and other peripherals.
Software Interfaces
We’re going to focus on software interfaces, and specifically on interfaces in Java.
-
However, interfaces are not a Java- or language-specific idea.
-
Some languages—like Java, Go, and others—include a specific notion of interfaces as part of the language.
-
For other languages this is done by convention.
-
But all software development involves interfaces, regardless of what language you are using.
Note: Every Java Object Has An Interface
Even Java classes that don’t implements
a Java interface provide an interface.
The interface to a Java class
is the the set of methods that it
provides.
Interface Documentation
Interfaces are also a place where we need excellent documentation.
-
This facilitates communication between users of an interface and providers of an interface.
-
This is exactly what Javadoc is for.
Announcements
-
No Coders on this week’s quiz. Chapters 5 and 6 for next week.
-
I have office hours today from 1–3PM in Siebel 0403.
-
We have a anonymous feedback form to the course website. Use it to give us feedback!