Overview
Readings Reference ch. 4, 10 · week 4
A single object isn’t very useful. Real programs are made of many objects that hold references to each other and pass information back and forth. This chapter is about the questions that come up as soon as objects start interacting:
- What does it mean for two things to be “equal”? Same value, or the same object? (
==vsequals) - When you hand an object to a method, can the method change it? (parameter passing)
- How do objects find each other? (linking, through reference fields)
- How do you go from a written problem to a set of classes? (design)
It starts with Java’s built-in String class, which is the first object most programs use and a good illustration of all of these.
Nearly every surprise in this chapter is the reference idea from Ch 3 again: an object variable holds an address, not the object. When something behaves strangely, I draw the boxes and arrows.
Notes
The String class
String is a class, so a String variable is a reference to a String object, not the text itself. Strings get one special privilege, though: you can create them from a literal (text in quotes) without writing new.
String a = "ABC 41"; // literal: stored in the String pool
String b = "ABC 41"; // same literal → reuses the SAME object
String c = new String("ABC 41"); // new → always a brand-new objectWhy the pool exists. Programs use the same bits of text over and over. Instead of making a new object for every "ABC 41" in the code, Java keeps one shared copy in a String pool and points every identical literal at it. That saves memory, and it’s only safe because of the next point.
Strings are immutable. Once created, a String’s characters can never change. Methods like concat, replace and toUpperCase don’t modify the original. They build and return a new String:
name.toUpperCase(); // does nothing useful: result thrown away
name = name.toUpperCase(); // keeps itImmutability is what makes sharing safe. If a and b point at the same pooled "ABC 41" and a could change its letters, b’s text would change too, out of nowhere. Because no String can ever change, any number of variables can share one with no risk. The price is that “changing” a string really means making a new one and moving your variable to it, which is why you must keep the result.
Common methods (indices start at 0, as with arrays):
| Method | Returns | Example on "ABC 41" |
|---|---|---|
length() | Number of characters | 6 |
charAt(i) | Character at index i | charAt(2) → 'C' |
concat(s) | This string + s | concat("564") → "ABC 41564" |
replace(old, new) | Every old char replaced | replace('4', '0') → "ABC 01" |
substring(i) | From i to the end | substring(1) → "BC 41" |
substring(i, j) | From i up to but not including j | substring(1, 2) → "B" |
toUpperCase() / toLowerCase() | Case-changed copy | "abc" → "ABC" |
equals(s) | true if same characters | — |
equalsIgnoreCase(s) | Same, ignoring case | — |
compareTo(s) | < 0, 0, or > 0 (alphabetical order) | — |
substring(i, j) excludes j on purpose. That way the length of the result is just j - i, and substring(0, k) plus substring(k) glue back together into the original with no overlap.
length()has parentheses for Strings because it's a method (the String works it out). For arrays it'slengthwith no parentheses because it's a field, a stored value. They do the same job but come from different kinds of thing.
Asking for an index past the end, like substring(1, 8) on a 6-character string, throws a StringIndexOutOfBoundsException.
Example 4.1: String methods
Given String licensePlate = new String("ABC 41");, what is each value?
char a = licensePlate.charAt(2);
String extendedPlate = licensePlate.concat("564");
int b = licensePlate.length();
String newPlate = extendedPlate.replace('4', '0');
String sub1 = licensePlate.substring(1);
String sub2 = licensePlate.substring(1, 2);
String sub3 = licensePlate.substring(1, 8);Show answerHide answer
Indices: A=0, B=1, C=2, space=3, 4=4, 1=5.
a='C'extendedPlate="ABC 41564"(licensePlateitself is unchanged)b=6(the space counts)newPlate="ABC 01560": every'4'in"ABC 41564"is replacedsub1="BC 41"sub2="B"(index 1 only; length )sub3throwsStringIndexOutOfBoundsException: index 8 is past the end of a 6-character string
Comparing values
What “equal” means depends on what you’re comparing:
| Type | How to compare | Why |
|---|---|---|
byte, short, int, long | == | Whole numbers are stored exactly |
float, double | Math.abs(a - b) < TOLERANCE | Decimals are only stored approximately, so 0.1 + 0.2 == 0.3 is false |
char | ==, <, > | Compares Unicode numbers: '0' < 'L' is true |
String / objects | .equals() | == compares addresses, not contents |
Why decimals need a tolerance. As Ch 2 explained, 0.1 has no exact binary form, so it’s stored as the closest value that fits. Add two of those approximations and the tiny errors add up: 0.1 + 0.2 comes out as 0.30000000000000004. It’s not equal to the stored 0.3, even though mathematically it should be. So instead of asking “are these exactly equal?”, ask “are they close enough that the difference is just rounding error?”:
final double TOLERANCE = 0.00000001;
if (Math.abs(a - b) < TOLERANCE) { /* treat as equal */ }Example 4.2: Comparing decimals with a tolerance
What do these print?
double x = 0.1 + 0.2;
System.out.println(x == 0.3);
final double TOLERANCE = 0.00000001;
System.out.println(Math.abs(x - 0.3) < TOLERANCE);Show answerHide answer
The first line prints false. 0.1 and 0.2 are stored as the nearest binary approximations, and their sum is 0.30000000000000004, which isn’t the stored 0.3. The second prints true, because the difference is about , far below TOLERANCE. Choose a tolerance that is bigger than the rounding error but smaller than any difference that matters in your problem.
== vs equals()
For references, == compares what’s actually in the two variables, which is two addresses. So it asks “are these the same object?”, not “do these hold the same data?“.
String name1 = new String("Lewis");
String name2 = new String("Lewis");
name1 == name2 // false: two separate objects
name1.equals(name2) // true: same charactersThink of two printed copies of the same book. They have identical contents (equals is true), but they’re two different physical books (== is false). equals is a method the class provides to compare contents, and String’s version compares the characters one by one.
Two identical literals (
"Lewis" == "Lewis") often come outtrue, but only because the String pool made them the same object. Strings built while the program runs (from input, or by joining) won't be pooled. Code that relies on==for Strings works in testing and then fails on real input. Always use.equals()for Strings.
Example 4.3: == vs equals on Strings
Given these lines, what does each comparison give?
String s1 = "Lewis";
String s2 = "Lewis";
String s3 = new String("Lewis");Show answerHide answer
| Expression | Result | Why |
|---|---|---|
s1 == s2 | true | Both literals point at the same pooled object |
s1 == s3 | false | new always builds a separate object |
s1.equals(s3) | true | Same characters |
s3.equals(s2) | true | equals ignores which object it is |
Only the equals answers say whether the text matches. The == answers depend on how the Strings were created, which is why equals is the one to use.
Comparing your own objects
For a class you wrote, Java has no idea what “same contents” should mean. Should two cars be equal if their plates match? If their speeds match too? So by default equals() falls back to the only thing it can check, the address, and acts just like ==:
Car miniVan = new Car("ABC", 50, 100);
Car sedan = new Car("ABC", 50, 100);
sedan == miniVan // false: different objects
sedan.equals(miniVan) // STILL false: default equals is ==To compare contents, compare the fields that matter (later you’ll write your own equals method that does this):
sedan.licensePlate.equals(miniVan.licensePlate)
&& sedan.speed == miniVan.speed
&& sedan.maxSpeed == miniVan.maxSpeed // trueYou decide what makes two objects equal, based on what the class represents. For cars, matching licence plates might be enough, since a plate identifies one real car, even if the speeds differ at the moment.
Parameter passing
When you call a method, Java copies whatever is in each argument variable into the matching parameter. Always. That one rule (called pass by value) explains everything; what changes is what is in the variable:
| Argument type | What’s in the variable, and so what gets copied | Can the method change the caller’s data? |
|---|---|---|
Primitive (int, double, …) | The value itself | No: the method gets its own copy of the number |
| Object / array | The reference (address) | Yes: both copies of the address lead to the same object |
void changeMe(double d) { d = 345.0; } // changes only the local copy
double d = 2.0;
changeMe(d);
System.out.println(d); // 2.0The method’s d is a separate box that started with a copy of 2.0. Changing it does nothing to the caller’s box.
void changeParameters(Car c) { c.speed = 30; } // follows the reference
Car miniVan = new Car("ABC", 50, 100);
changeParameters(miniVan);
System.out.println(miniVan.speed); // 30.0Here the copied value is an address. c and miniVan are two boxes holding the same address, like two copies of a house key. Whoever uses their key and rearranges the furniture changes the one house both keys open.
Reassigning the parameter itself (
c = new Car(...)) inside the method doesn't affect the caller. That just puts a different address into the method's own box, like swapping your copy of the key for one to a different house. The caller's key, and their house, are untouched. The caller only sees changes made through the reference (c.speed = ...).
Figure 4.1: A primitive argument is copied by value. A reference argument copies the address, so both variables reach the same object.
Example 4.4: Pass by value vs reference
What does this print?
public static void main(String[] args) {
int n = 5;
Car car = new Car("XYZ", 50, 100);
update(n, car);
System.out.println(n + " " + car.speed);
}
static void update(int n, Car c) {
n = 99;
c.speed = 80;
c = new Car("NEW", 0, 0);
c.speed = 10;
}Show answerHide answer
n = 99only changes the method’s own copy, so the caller’snstays5.c.speed = 80follows the reference to the caller’s car, so its speed is now80.c = new Car(...)points the localcat a new object. The caller’scarisn’t touched.c.speed = 10changes the new object, which becomes garbage once the method returns.
Figure 4.3: Memory just before update returns. Only the first car was changed through the caller’s reference, and the NEW car is lost afterwards.
Prints 5 80.0.
Linking objects
A field can hold a reference to another object, and that links the two. This is how objects find each other: an object doesn’t search for the others it needs, it keeps their addresses. A reference can also be null, meaning “linked to nothing”.
class Employee {
String name;
Employee supervisor; // a reference to another Employee
Employee(String name, Employee supervisor) {
this.name = name;
this.supervisor = supervisor;
}
}
Employee ceo = new Employee("Stacey", null); // no supervisor
Employee emp = new Employee("Gavin", ceo); // linked to Stacey
if (emp.supervisor == null)
System.out.println(emp.name + " is the CEO!");
else
System.out.println("The supervisor of " + emp.name + " is " + emp.supervisor.name);
// The supervisor of Gavin is Staceyemp.supervisor.name follows two links: from emp to Stacey’s object, then to her name. Each dot means “go to the object this refers to”. Note that supervisor has type Employee, the same class it’s in. A class can refer to its own kind, which is how chains and hierarchies (an org chart, a family tree) get built. The null check matters: the chain has to end somewhere, and following a null link crashes the program.
Figure 4.2: Two Employee objects linked through the supervisor field. Stacey’s own supervisor is null.
Example 4.5: Following a chain of links
Using Employee as above, a third employee is created with Employee intern = new Employee("Ravi", emp);. What do these give?
intern.supervisor.name
intern.supervisor.supervisor.name
ceo.supervisor.nameShow answerHide answer
intern.supervisorisemp(Gavin), so the first is"Gavin".intern.supervisor.supervisoris Gavin’s supervisor,ceo(Stacey), so the second is"Stacey".ceo.supervisorisnull, and.nameonnullthrows aNullPointerException.
Guard a walk up the chain with a check such as while (e.supervisor != null) { e = e.supervisor; }, which stops at the top.
Example 4.6: Tracing an array of linked objects
class Coord {
int x, y;
public Coord(int x, int y) { this.x = x; this.y = y; }
}
Coord[] original, alias;
original = new Coord[6];
int i = 0;
while (i < original.length) {
original[i] = new Coord(i, original.length - i);
i++;
}
i = 0;
while (i < original.length) {
original[i] = original[i + 1];
i += 2;
}
alias = original;
alias[3].y = alias[0].x;
System.out.println(original[0].x + "," + original[0].y);
System.out.println(alias[3].x + "," + alias[3].y);Show answerHide answer
-
First loop creates six objects, :
Index 0 1 2 3 4 5 Object (0,6) (1,5) (2,4) (3,3) (4,2) (5,1) -
Second loop runs for , copying each odd slot’s reference into the even slot before it:
Index 0 1 2 3 4 5 Object (1,5) (1,5) (3,3) (3,3) (5,1) (5,1) Slots 0/1, 2/3 and 4/5 now share objects, and the original (0,6), (2,4), (4,2) objects are garbage.
-
alias = original→ same array, two names -
alias[3].y = alias[0].x→ the (3,3) object’sybecomes1, so it’s now (3,1).original[2]sees the change too.
Figure 4.4: Final state. Slots 0/1, 2/3 and 4/5 share objects, the shared (3,3) object became (3,1), and the three original objects are unreachable garbage.
1,5
3,1
Designing from a description
Turning a paragraph of requirements into classes is the design phase from Ch 1. A reliable starting point is to read the description for its grammar:
- Nouns become classes (Book, Member, Library). They’re the things the problem is about.
- Their properties become fields (title, author, ISBN). That’s what each thing needs to remember.
- “Has many” becomes an array or list field (a Member has borrowed books). That’s a link to several objects at once.
- Verbs become methods (borrow, add, register). That’s what the things do, and each method goes in the class whose data it mainly changes.
- Write a tester class that plays out the scenario in the description, to check the design can actually do what’s asked.
This works because OOP is built to mirror the problem (Ch 1). If the description is about members borrowing books from a library, the code should have members, books and a library, doing borrowing.
Example 4.7: Design an Airport class from its output
Write the Airport class so this main gives the output below.
Airport quizairport = new Airport(5); // 5 gates
for (int i = 0; i < 5; i++)
quizairport.addPassengers(i, (20 * i + 3)); // (gate index, passengers)
quizairport.printPassengers();Number of passengers at Gate 1 is 3
Number of passengers at Gate 2 is 23
...
Number of passengers at Gate 5 is 83
Show answerHide answer
- Fields: one passenger count per gate, so an
intarray sized by the constructor addPassengers(gate, n): storenat indexgateprintPassengers(): loop over the array. Gates are indexed from 0 but printed from 1, so printi + 1.
public class Airport {
private int[] passengers;
public Airport(int numGates) {
passengers = new int[numGates];
}
public void addPassengers(int gate, int count) {
passengers[gate] = passengers[gate] + count;
}
public void printPassengers() {
for (int i = 0; i < passengers.length; i++)
System.out.println("Number of passengers at Gate " + (i + 1)
+ " is " + passengers[i]);
}
}Example 4.8: Library system design
A Book has a title, author and ISBN. A Member has a name and can borrow multiple books. A Library manages its books and members. John borrows Anne of Green Gables.
Show answerHide answer
- Classes from the nouns:
Book,Member,Library - Fields: Book has
title,author,isbn. Member hasnameplus an array of borrowedBooks. Library has arrays ofBooks andMembers. - Methods from the verbs:
Member.borrow(Book),Library.addBook(Book),Library.addMember(Member)
public class Book {
private String title, author, isbn;
public Book(String title, String author, String isbn) {
this.title = title; this.author = author; this.isbn = isbn;
}
public String getTitle() { return title; }
}
public class Member {
private String name;
private Book[] borrowed = new Book[10];
private int count = 0;
public Member(String name) { this.name = name; }
public void borrow(Book b) {
borrowed[count] = b; // stores a REFERENCE: no copy of the book is made
count++;
}
public String getName() { return name; }
}
public class Library {
private Book[] books = new Book[100];
private Member[] members = new Member[100];
private int numBooks = 0, numMembers = 0;
public void addBook(Book b) { books[numBooks++] = b; }
public void addMember(Member m) { members[numMembers++] = m; }
}
public class LibraryTest {
public static void main(String[] args) {
Library lib = new Library();
Book anne = new Book("Anne of Green Gables", "L. M. Montgomery", "978-0");
Member john = new Member("John");
lib.addBook(anne);
lib.addMember(john);
john.borrow(anne);
}
}After
john.borrow(anne), the library's array and John's array both refer to the sameBookobject. That's what linking means. There's only ever one copy of the book.
Key syntax
Comparisons
s1.equals(s2) // String contents
Math.abs(a - b) < TOLERANCE // floating-point
obj1 == obj2 // same object?Substring range
- : first index included
- : first index excluded
Common mistakes
- Comparing Strings with
==instead of.equals() - Calling
s.toUpperCase();and expectingsto change (Strings are immutable) - Forgetting that
substring(i, j)includesibut excludesj - Comparing
doubles with== - Expecting a method to change a primitive argument, or expecting
param = new X()to change the caller’s variable - Following a link (
emp.supervisor.name) when the link isnull, which gives aNullPointerException - Writing
"... " + emp.name " is ..."with a missing+between pieces, which won’t compile