Effective Java II - Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Material Type: Notes; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 2000;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-y0v
koofers-user-y0v 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 132:
Object-Oriented Programming II
Effective Java II
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Effective Java II - Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Effective Java II

Department of Computer Science

University of Maryland, College Park

Effective Java

Collection of tips for

programming in Java

By Joshua Bloch (Sun)

Quick look at 3 topics

(out of 57)

  1. Duplicate Object Creation
  2. Defensive Copying
  3. Immutable Classes

Slides borrowed from

Bloch & adapted

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); } }

  1. Defensive Copying

Java programming language is safe

Immune to buffer overruns, wild pointers, etc… Unlike C, C++

Makes it possible to write robust classes

Correctness doesn’t depend on other modules Even in safe language, requires effort

Defensive Programming

Assume clients will try to destroy invariants

May actually be true More likely honest mistakes

Ensure class invariants survive any inputs

The Problem Date is Mutable

// ATTACK INTERNALS OF PERSON

Date today = new Date();

Person p = new Person(today);

today.setYear(78); // MODIFIES P’S BIRTHDAY!

Constructors can allow invariant to be modified

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

Use constructor, not clone, to make copies

Necessary because Date class is nonfinal

Attacker could implement malicious subclass

Records reference to each instance in list Provides attacker with access to instance list

…and pass subclass to Person( ) constructor

More Defensive Copying

// ACCESSOR ATTACK ON INTERNALS OF PERSON

Date today = new Date();

Person p = new Person(today);

Date bday = p.bday( );

bday.setYear(78); // MODIFIES P’S BIRTHDAY!

Constructors are only half the battle

Accessors can allow invariant to be modified

Defensive Copying Summary

Don’t incorporate mutable parameters into

object – make defensive copies

Constructors Static factories Pseudo-constructors Mutators

Return defensive copies of mutable fields

Accessors

Real lesson – use immutable components

Eliminates the need for defensive copying

  1. Immutable Classes

Class whose instances cannot be modified

Examples

String Integer BigInteger

How, why, and when to use them

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)); } }