

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; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


1
2
Effective Java Programming Language Guide
Joshua Bloch
Learn to use Java language and its libraries more effectively
3
Simple and elegant Need to avoid some sharp corners!
Java code fragments Expose some tricky aspects of Java
Patterns and idioms to emulate Pitfalls to avoid
4
public class Name { private String myName; public Name(String n) { myName = n; } public boolean equals(Object o) { if (!(o instanceof Name)) return false; Name n = (Name)o; return myName.equals(n.myName); } public static void main(String[ ] args) { Set s = new HashSet(); s.add(new Name("Donald")); System.out.println( s.contains(new Name("Donald"))); } }
Name class violates Java hashCode( ) contract.
If you override equals( ), must also override hashCode( )!
5
public class Trivial {
public static void main(String args[ ]) { System.out.print("H" + "a"); System.out.print('H' + 'a'); }
}
Prints Ha
'H' + 'a' evaluated as int , then converted to String!
Use string concatenation (+) with care. At least one operand must be a String 6
public class Confusing { public Confusing(Object o) { System.out.println("Object"); } public Confusing(double[] dArray) { System.out.println("double array"); } public static void main(String args[]) { new Confusing(null); } }
When multiple overloadings apply, the most specific wins
Avoid overloading. If you overload, avoid ambiguity
7
Time For A Change
If you pay $2.00 for a gasket that costs $1.10, how much change do you get?
public class Change { public static void main(String args[ ]) { System.out.println(2.00 - 1.10); } }
Prints 0.8999999999999999. Decimal values can’t be represented exactly by float or double
Avoid float or double where exact answers are required. Use BigDecimal, int, or long instead 8
A Private Matter
class Base { public String name = "Base"; } class Derived extends Base { private String name = "Derived"; } public class PrivateMatter { public static void main(String[] args) { System.out.println(new Derived( ).name); } }
Compiler error in class PrivateMatter: Can't access name
Private field can hide public. Avoid hiding & public fields
9
Effective Java Topics
10
Creating and Destroying Objects
Consider providing static factory methods instead of constructors Enforce singleton property with a private constructor Enforce noninstantiability with a private constructor Avoid creating duplicate objects Eliminate obsolete object references Avoid finalizers
11
Methods Common to All Objects
Obey the general contract when overriding equals Always override hashCode when you override equals Always override toString Override clone judiciously Consider implementing Comparable
12
Classes and Interfaces
Minimize the accessibility of classes and members Favor immutability Favor composition over inheritance Design and document for inheritance or else prohibit it Prefer interfaces to abstract classes Use interfaces only to define types Favor static member classes over nonstatic