Overview
csc207-01-grader@grinnell.edu
When are we getting our quizzes back?
Monday, I hope.
When will we get HW1 back?
Monday, I hope.
Can you discuss more about what happens in the examples in problem 4?
Two kinds of objects equality in Java,
==
andx.equals(y)
.==
means “same memory location”..equals
method can be implemented by individual classes, works how those classes have implemented it. (Default.equals
is==
.).equals
is generally slower but broader.
Java standard says (I think) that all the identical string constants (string literals) that appear in a program share the same memory location.
Java standard does not say what happens with other strings. (Identical strings read at run time can share memory locations or may not.)
We’ve discovered that our implementation of Java seems to give a new location to each new string it encounters at runtime.
New strings:
scanner.next()
,x + "hjello"
, …
Side note: For primitive types, like
int
, == behaves the way it behaved in C.
equals
methods?Recall the Cell
class, which has an int field called x
.
public class Cell {
int x;
public Cell(int x) {
this.x = x;
} // Cell(int)
We might say “We’d like two cells to be equal if their x values are equal.”
Suppose we have not implemented that policy. What do you expect for the following?
public static void main(String[] args) {
PrintWriter pen = new PrintWriter(System.out, true);
Cell c1 = new Cell(1);
Cell c01 = new Cell(1);
Cell c2 = new Cell(2);
pen.println(c1.equals(c1)); // true, would like true
pen.println(c1.equals(c01)); // false, would like true
pen.println(c1.equals(c2)); // false, would like false
pen.println(c1.equals("1")); // false, would like false
pen.println("1".equals(c1)); // false, would like false
} // main
Suppose we implement that policy as follows.
public boolean equals(Cell other) {
return (this.x == other.x);
} // equals(?)
c1.equals(c01)
.==
work? Because we’re comparing two int
values; two
primitive types. (numeric types, booleans, characters)c1.equals("1")
? After all "1"
is not a
Cell. Because Java permits overloading (multiple methods with the
same name, but different parameter types). Since we called equals
without a Cell
, it uses the default equals
.New equals
.
public boolean equals(Object other) {
return this.toString().equals(other.toString());
} // equals
toString()
method. If you don’t
implement it, you get something like CLASS<location>
. If you do
implement it, it does whatever the method says.Separate lesson: Java permits overloading You can have multiple methods with the same name and different parameter types (or number of parameters), and it figures out which one you mean.
int
values vs. Integer
objectsint i = 1;
+---------+
i | 1 |
+---------+
Integer j = new Integer(2); // deprecated
+---------+ +Integer---+
j | *-----------> | val: 2 |
+---------+ +----------+
Integer k = 3; // Automatic conversion
+---------+ +Integer---+
k | *-----------> | val: 3 |
+---------+ +----------+
sqrt
. We can do “generic” math.sqrt
example, you need not do a loop for the other types.
E.g.,
BigInteger bi = new BigInteger(“12345”);
double sqrt_bi = MathUtils.sqrt(bi);
pen.println(“The square root of “ + bi + “ ~= “ + sqrt_bi);
pen.println(sqrt_bi + “^2 = “ + (sqrt_bi * sqrt_bi));