






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 2000;
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Effective Java
By Joshua Bloch (Sun)
Object Duplication Example
public class Person { private final Date birthDate; public Person(Date birthDate){ this.birthDate = birthDate; } // UNNECESSARY OBJECT CREATION public boolean bornBefore2000(){ Calendar gmtCal = Calendar.getInstance( TimeZone.getTimeZone("GMT")); gmtCal.set(2000,Calendar.JANUARY,1,0,0,0); Date MILLENIUM = gmtCal.getTime(); return birthDate.before(MILLENIUM); } }
Object Duplication Example (cont.)
public class Person { … // STATIC INITIALIZATION CREATES OBJECT ONCE private static final Date MILLENIUM; static { Calendar gmtCal = Calendar.getInstance( TimeZone.getTimeZone("GMT")); gmtCal.set(2000,Calendar.JANUARY,1,0,0,0); Date MILLENIUM = gmtCal.getTime(); } public boolean bornBefore2000(){ // FASTER! return birthDate.before(MILLENIUM); } }
Immune to buffer overruns, wild pointers, etc… Unlike C, C++
Correctness doesn’t depend on other modules Even in safe language, requires effort
Defensive Programming
May actually be true More likely – honest mistakes
The Problem – Date is Mutable
Date today = new Date();
Person p = new Person(today);
today.setYear(78); // MODIFIES P’S BIRTHDAY!
The Solution – Defensive Copying
public class Person { private final Date birthDate; // REPAIRED CONSTRUCTOR // DEFENSIVELY COPIES PARAMETERS public Person(Date birthDate){ this.birthDate = new Date(birthDate.getTime()); } // RETURNS birthDate public Date bday() { return birthDate; } }
Another Important Detail
Necessary because Date class is nonfinal
Records reference to each instance in list Provides attacker with access to instance list
More Defensive Copying
Date today = new Date();
Person p = new Person(today);
Date bday = p.bday( );
bday.setYear(78); // MODIFIES P’S BIRTHDAY!
Defensive Copying Summary
Constructors Static factories Pseudo-constructors Mutators
Accessors
Eliminates the need for defensive copying
String Integer BigInteger
Immutable Fval Class Example
public final class Fval { private final float f; public Fval(float f) { this.f = f; } // ACCESSORS WITHOUT CORRESPONDING MUTATORS public float value( ) { return f; }
// ALL OPERATIONS RETURN NEW Fval public Fval add(Fval x) { return new Fval(f + x.f); } // SUBTRACT, MULTIPLY, ETC. SIMILAR TO ADD
Immutable Float Example (cont.)
public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Fval)) return false; Fval c = (Fval) o; return (Float.floatToIntBits(f) == Float.floatToIntBits(c.f)); } }