Good Java idioms
Implementing equals()
class Person {
String name;
int birthYear;
public boolean equals(Object obj) {
if (!(obj instanceof Person))
return false;
else {
Person other = (Person)obj;
return name.equals(other.name)
&& birthYear == other.birthYear;
}
}
}
- The parameter must be of type
Object, not the type of the enclosing class. foo.equals(null)must returnfalse, not throw aNullPointerException. (Note thatnull instanceof Anythingis alwaysfalse, so the code above works.)- Compare primitive fields (e.g.
int) using==, compare object fields usingequals(), and compare array fields usingArrays.equals(). - See:
java.lang.Object.equals()
Implementing compareTo()
class Person implements Comparable<Person> {
String firstName;
String lastName;
// Compare by firstName, then break ties by lastName
public int compareTo(Person other) {
if (firstName.compareTo(other.firstName) != 0)
return firstName.compareTo(other.firstName);
else
lastName.compareTo(other.lastName);
}
}
- Always implement the generic version
Comparable<T>rather than the raw typeComparablebecause it saves code and hassle. - Only the sign of the returned result matters (negative/zero/positive), not the magnitude.
- See:
java.lang.Comparable
Implementing clone()
class Values implements Cloneable {
int foo;
int[] bars;
public Values clone() {
try {
Values result = (Values)super.clone();
result.bars = result.bars.clone();
return result;
} catch (CloneNotSupportedException e) { // Impossible
throw new AssertionError(e);
}
}
}
- Use
super.clone()to make theObjectclass be responsible for creating the new object. - Manually make a deep copy of all the non-primitive fields (objects and arrays).
- When the class implements
Cloneable,clone()will never throwCloneNotSupportedException. So catch the exception and ignore it, or wrap it in an unchecked exception. - It’s also possible and legal to implement
clone()manually without usingObject.clone(). - See:
java.lang.Object.clone()
Using Iterator.remove()
void filter(List<String> list) {
for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) {
String item = iter.next();
if (...)
iter.remove();
}
}
remove()acts on the most recent item returned bynext().remove()can only be used once per item.- See:
java.util.Iterator.remove()
Using try-finally
Example with I/O stream:
void writeStuff() throws IOException {
OutputStream out = new FileOutputStream(...);
try {
out.write(...);
} finally {
out.close();
}
}
Example with lock:
void doWithLock(Lock lock) {
lock.acquire();
try {
...
} finally {
lock.release();
}
}
- If the statement before the
tryfails and throws an exception then thefinallyblock won’t execute, but there is nothing to release anyway. - If a statement inside the
tryblock throws an exception then execution will jump thefinallyblock, finish the block, then jump out of the method (unless there is another enclosingfinallyblock).
Using StringBuilder/StringBuffer
// join(["a", "b", "c"]) -> "a and b and c"
String join(List<String> strs) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String s : strs) {
if (first) first = false;
else sb.append(" and ");
sb.append(s);
}
return sb.toString();
}
- Don’t use repeated string concatenation like this because it takes O(n2) time:
s += item; - In
StringBuilderorStringBuffer, useappend()to add text andtoString()to get the entire accumulated text. - See:
java.lang.StringBuilder
Staring a thread
Example implementing Runnable:
void startAThread0() {
new Thread(new MyRunnable()).start();
}
class MyRunnable implements Runnable {
public void run() {
...
}
}
Example extending Thread:
void startAThread1() {
new MyThread().start();
}
class MyThread extends Thread {
public void run() {
...
}
}
Example anonymously extending Thread:
void startAThread2() {
new Thread() {
public void run() {
...
}
}.start();
}
- Do not call
run()directly. Always callThread.start(), which starts a new thread and makes that new thread callrun(). - See:
java.lang.Thread
Reversing a String
String reverse(String s) {
new StringBuilder(s).reverse().toString();
}
- Maybe this belongs in the Java standard library. I mean, Python sure has it.
Defensive checking: values
int factorial(int n) {
if (n < 0)
throw new IllegalArgumentException("Undefined");
else if (n >= 13)
throw new ArithmeticException("Result overflow");
else if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
- Never assume that numeric inputs are going to be positive, sufficiently small, etc. Check for these conditions explicitly.
Defensive checking: objects
int findIndex(List<String> list, String target) {
if (list == null || target == null)
throw new NullPointerException();
...
}
- Never assume that object inputs are not
null. Check for this condition explicitly.
Defensive checking: array indexes
void frob(byte[] b, int index) {
if (b == null)
throw new NullPointerException();
if (index < 0 || index >= b.length)
throw new IndexOutOfBoundsException();
...
}
- Never assume that a given array index is within bounds. Check explicitly.
Defensive checking: array ranges
void frob(byte[] b, int off, int len) {
if (b == null)
throw new NullPointerException();
if (off < 0 || off > b.length
|| len < 0 || b.length - off < len)
throw new IndexOutOfBoundsException();
...
}
- Never assume that a given array range (i.e. "starting at
off, going forlenelements") is within bounds. Check explicitly.
Generating a random integer in a range
Random rand = new Random();
// Between 1 and 6, inclusive
int diceRoll() {
return rand.nextInt(6) + 1;
}
- Always use the Java API method to generate random numbers in an integer range.
- Never try to improvise something like
Math.abs(rand.nextInt()) % nbecause it is biased. - See:
java.util.Random.nextInt(int n)
Packing 4 bytes into an int
int packBigEndian(byte[] b) {
return b[0] << 24
| (b[1] & 0xFF) << 16
| (b[2] & 0xFF) << 8
| b[3] & 0xFF;
}
int packLittleEndian(byte[] b) {
return b[0] & 0xFF
| (b[1] & 0xFF) << 8
| (b[2] & 0xFF) << 16
| b[3] << 24;
}
Unpacking an int into 4 bytes
byte[] unpackBigEndian(int x) {
return new byte[] {
(byte)(x >>> 24),
(byte)(x >>> 16),
(byte)(x >>> 8),
(byte)(x >>> 0)
};
}
byte[] unpackLittleEndian(int x) {
return new byte[] {
(byte)(x >>> 0),
(byte)(x >>> 8),
(byte)(x >>> 16),
(byte)(x >>> 24)
};
}
- Always use the unsigned right shift operator (
>>>) for bit packing, never the arithmetic right shift operator (>>).
Reading from a stream
InputStream in = (...);
while (true) {
int b = in.read();
if (b == -1)
break;
(... process b ...)
}
in.close();
read()either returns the next byte value (range 0 to 255, inclusive) from the stream or returns −1 if the stream has ended.- See:
java.io.InputStream.read()