Cover image for Hardcore Java

Book description

Java has quickly become one of the most important languages in programming, particularly for professional and enterprise-level projects. From its infancy as a language primarily used for web applets to its maturity through servlets, Enterprise JavaBeans, and database access, Java has become a complex and robust tool for today's developer. Hardcore Java takes this language and breaks it apart, piece by piece, revealing the important secrets and tricks that will take you from a junior-level programmer to a seasoned and expert developer. You'll fly through the fundamentals and quickly find yourself learning about advanced memory management techniques, optimization and bytecode-level enhancements, and the techniques required to build lightning-fast GUIs. Throughout the book, you'll also master the art of writing and maintaining bulletproof and error-proof code, all while grasping the intricacies of the Java language. Hardcore Java covers:

  • Use of the final keyword to optimize and protect your Java classes.

  • Complete and thorough coverage of all types of nested classes, including how to optimize anonymous and inner classes.

  • Detailed discussion of immutable objects, including unique tips on when to use them (and when not to).

  • Elimination of bugs through exception-handling management.

  • In-depth studies of constants, including their impact on the Java memory model.

  • The most thorough discussion of reflection in print, moving far beyond other books' "Hello World" coverage.

  • Construction and use of dynamic proxies, in both Java Standard and Enterprise editions.

  • Expansive coverage of weak references, including usage patterns and their role in garbage collection and memory management.

Hardcore Java is an invaluable addition to every programmer's library, and even the most advanced developers will find themselves moving beyond their own conceptions into truly advanced applications of the language. Thousands of lines of code, heavily commented and easily runnable, illustrate each concept in the book.

Table of Contents

  1. Hardcore Java
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Audience
        1. Prerequisites and Assumptions
      2. Typographical Conventions
      3. Code Samples
        1. Locating an Example in the Downloadable Code
        2. Categories of Examples
          1. Syntax checkers
          2. Demonstrators
          3. Executables
      4. Tools
        1. UML Diagramming
        2. IDE
        3. Out-of-IDE Building
        4. Logging
        5. Jakarta Commons
      5. Using Code Examples
      6. Comments and Questions
      7. Acknowledgments
    4. 1. Java in Review
      1. 1.1. Core Concepts
        1. 1.1.1. Constant Problems with Pointers
        2. 1.1.2. Everything Is a Class and Object Is God
        3. 1.1.3. RTTI
      2. 1.2. Syntax Issues
        1. 1.2.1. Abbreviated if
        2. 1.2.2. Ternary Expressions
        3. 1.2.3. Leveraging for Loops
          1. 1.2.3.1. for loop fundamentals
          2. 1.2.3.2. Collection iteration with for
        4. 1.2.4. Abrupt Flow Control
          1. 1.2.4.1. break
          2. 1.2.4.2. continue
          3. 1.2.4.3. Labels
        5. 1.2.5. assert
          1. 1.2.5.1. Assertions versus exceptions
          2. 1.2.5.2. To assert or not to assert
          3. 1.2.5.3. Assertions and deployment
        6. 1.2.6. Chaining Constructors
        7. 1.2.7. Initialization
      3. 1.3. Access Issues
        1. 1.3.1. Preferred Restrictions
        2. 1.3.2. Friends Allow Unrestricted Access
      4. 1.4. Common Mistakes
        1. 1.4.1. System Streams
        2. 1.4.2. System.exit( )
        3. 1.4.3. Default Execution
    5. 2. The Final Story
      1. 2.1. Final Constants
        1. 2.1.1. Public Primitives and Substitution
        2. 2.1.2. Excessive Constants
      2. 2.2. Final Variables
        1. 2.2.1. Method-Scoped final Variables
        2. 2.2.2. Deferred Initialization
          1. 2.2.2.1. Chained deferred initialization
      3. 2.3. Final Parameters
      4. 2.4. Final Collections
      5. 2.5. Instance-Scoped Variables
      6. 2.6. Final Classes
      7. 2.7. Final Methods
      8. 2.8. Conditional Compilation
        1. 2.8.1. Conditional Compilation Variable Location
      9. 2.9. Using final as a Coding Standard
    6. 3. Immutable Types
      1. 3.1. Fundamentals
        1. 3.1.1. Creating Immutable Types
        2. 3.1.2. Cracked Immutables
        3. 3.1.3. False Immutable Types
      2. 3.2. Immutable Problems
        1. 3.2.1. The String Trap
        2. 3.2.2. Buffering Bad Performance
      3. 3.3. Immutable or Not
    7. 4. Collections
      1. 4.1. Collection Concepts
        1. 4.1.1. An Interface-Based Approach
        2. 4.1.2. Collection Types
          1. 4.1.2.1. Maps
          2. 4.1.2.2. Collections
      2. 4.2. Implementations
        1. 4.2.1. Determining Equality and Order
          1. 4.2.1.1. Equality versus identity
          2. 4.2.1.2. Comparing objects
        2. 4.2.2. Big O Notation
        3. 4.2.3. Lists
          1. 4.2.3.1. java.util.Vector
          2. 4.2.3.2. java.util.ArrayList
          3. 4.2.3.3. java.util.LinkedList
        4. 4.2.4. Maps and SortedMaps
          1. 4.2.4.1. java.util.HashTable
          2. 4.2.4.2. java.util.HashMap
          3. 4.2.4.3. java.util.LinkedHashMap
          4. 4.2.4.4. java.util.IdentityHashMap
          5. 4.2.4.5. java.util.WeakHashMap
          6. 4.2.4.6. java.util.TreeMap
        5. 4.2.5. Sets and SortedSets
          1. 4.2.5.1. java.util.HashSet
          2. 4.2.5.2. java.util.LinkedSet
          3. 4.2.5.3. java.util.TreeSet
      3. 4.3. Choosing a Collection Type
      4. 4.4. Iterating Collections
        1. 4.4.1. Three Iterators
          1. 4.4.1.1. java.util.Enumeration
          2. 4.4.1.2. java.util.Iterator
          3. 4.4.1.3. java.util.ListIterator
        2. 4.4.2. Fail-Fast Iterators
      5. 4.5. Collection Gotchas
        1. 4.5.1. Storage by Reference
        2. 4.5.2. Failure to Override hashCode( )
        3. 4.5.3. Lack of Type Safety
        4. 4.5.4. Collecting Problems
    8. 5. Exceptional Code
      1. 5.1. Two Types of Exceptions
        1. 5.1.1. The Exception Subclasses
          1. 5.1.1.1. Superfluous exceptions
        2. 5.1.2. The RuntimeException Subclasses
          1. 5.1.2.1. Masking exceptions
          2. 5.1.2.2. Ignoring exceptions
        3. 5.1.3. Exception or RuntimeException
      2. 5.2. When to Use Exceptions
        1. 5.2.1. Forgetting Exceptions
        2. 5.2.2. Too Many Exceptions
      3. 5.3. Finally for Closure
      4. 5.4. Exceptional Traps
    9. 6. Nested Classes
      1. 6.1. Inner Classes
        1. 6.1.1. Hierarchies of Inner Classes
      2. 6.2. Limited-Scope Inner Classes
        1. 6.2.1. Anonymous Classes
          1. 6.2.1.1. Anonymous class syntax
        2. 6.2.2. Problems with Limited-Scope Inner Classes
      3. 6.3. Static Nested Classes
      4. 6.4. Double Nested Classes
      5. 6.5. Nested Classes in Interfaces?
      6. 6.6. Nested Interfaces
        1. 6.6.1. Nested Interfaces in Classes
        2. 6.6.2. Nested Interfaces in Interfaces
      7. 6.7. Nested Class Rules
    10. 7. All About Constants
      1. 7.1. Substitution Constants
        1. 7.1.1. Internationalizing with Substitution Constants
          1. 7.1.1.1. Common internationalization techniques
          2. 7.1.1.2. Improving internationalization performance
      2. 7.2. Bit Fields
        1. 7.2.1. Altering Bit Fields
        2. 7.2.2. Comparing Bit Fields
        3. 7.2.3. Problems with Bit Fields
          1. 7.2.3.1. Mutual exclusivity
          2. 7.2.3.2. Limited expandability
          3. 7.2.3.3. Improper encapsulation
      3. 7.3. Option Constants
        1. 7.3.1. Defects of Option Constants
      4. 7.4. Constant Objects
        1. 7.4.1. Performance of Constant Objects
        2. 7.4.2. Indexing Constant Objects
        3. 7.4.3. Serialized Constant Objects
        4. 7.4.4. Accounting for Multiple Constant Object Types
      5. 7.5. Constant Encapsulation
    11. 8. Data Modeling
      1. 8.1. The Requirements Document
        1. 8.1.1. An Iterative Process
        2. 8.1.2. Getting Approval of Requirements
        3. 8.1.3. Closing the Gap
      2. 8.2. Natural Language Modeling
        1. 8.2.1. Noun Sets
        2. 8.2.2. An Application of Natural Language Modeling
          1. 8.2.2.1. Creating the noun set
          2. 8.2.2.2. Moving to a data model
          3. 8.2.2.3. Relationships and attributes
          4. 8.2.2.4. The first draft
      3. 8.3. Aspects of Well-Designed Data Models
        1. 8.3.1. Ghost Classes
        2. 8.3.2. Proper Relationships
          1. 8.3.2.1. Is a
          2. 8.3.2.2. Has a
          3. 8.3.2.3. Uses a
        3. 8.3.3. A Primitive Question
          1. 8.3.3.1. Partial use of primitives
          2. 8.3.3.2. Using only wrapper types
        4. 8.3.4. Mutable Objects
          1. 8.3.4.1. Identity and equality for all objects
      4. 8.4. Reusable Data Constraints
        1. 8.4.1. The ConstraintException
        2. 8.4.2. The Constraint Hierarchy
          1. 8.4.2.1. Primitive constraints
          2. 8.4.2.2. Object constraints
          3. 8.4.2.3. A numerical object constraint
          4. 8.4.2.4. Constraints for collections
        3. 8.4.3. Using Constraints
        4. 8.4.4. Creating New Constraint Types
      5. 8.5. Persistence
        1. 8.5.1. RDBMS Versus OODBMS
        2. 8.5.2. Java Data Objects to the Rescue
    12. 9. Practical Reflection
      1. 9.1. The Basics
      2. 9.2. Reflection and Greater Reflection
        1. 9.2.1. Package java.lang
          1. 9.2.1.1. Class java.lang.Class
          2. 9.2.1.2. Class java.lang.Object
          3. 9.2.1.3. Class java.lang.Package
        2. 9.2.2. Package java.lang.reflect
          1. 9.2.2.1. Class java.lang.reflect.Field
          2. 9.2.2.2. Class java.lang.reflect.Method
          3. 9.2.2.3. Class java.lang.reflect.Modifier
          4. 9.2.2.4. Class java.lang.reflect.Proxy
          5. 9.2.2.5. Class java.lang.reflect.AccessibleObject
        3. 9.2.3. Package java.beans
          1. 9.2.3.1. Class java.beans.Introspector
          2. 9.2.3.2. Class java.beans.PropertyDescriptor
          3. 9.2.3.3. Class java.beans.IndexedPropertyDescriptor
          4. 9.2.3.4. Classes MethodDescriptor and ParameterDescriptor
          5. 9.2.3.5. Interface java.beans.BeanInfo
      3. 9.3. Applying Reflection to MutableObject
        1. 9.3.1. Reflecting on toString( )
        2. 9.3.2. Fetching Constraints
      4. 9.4. Performance of Reflection
      5. 9.5. Reflection + JUnit = Stable Code
    13. 10. Proxies
      1. 10.1. What Is a Proxy?
        1. 10.1.1. Network Proxies and CORBA
        2. 10.1.2. Factories
        3. 10.1.3. Client and Server Objects
      2. 10.2. Two Kinds of Proxies
        1. 10.2.1. Static Proxies
        2. 10.2.2. Proxy by Interface
        3. 10.2.3. Dynamic Proxies
          1. 10.2.3.1. Invocation handlers
          2. 10.2.3.2. Generated proxy classes
          3. 10.2.3.3. Using dynamic proxies
      3. 10.3. Proxy Gotchas
    14. 11. References in Four Flavors
      1. 11.1. The Problem
      2. 11.2. Java Reference Concepts
        1. 11.2.1. Strong References
        2. 11.2.2. Weak References
        3. 11.2.3. Soft References
        4. 11.2.4. Phantom References
        5. 11.2.5. References and Referents
        6. 11.2.6. Reference Queues
        7. 11.2.7. The Garbage-Collection Process
      3. 11.3. The Java Reference Classes
        1. 11.3.1. Reference Classes
          1. 11.3.1.1. java.lang.ref.Reference
          2. 11.3.1.2. java.lang.ref.WeakReference
          3. 11.3.1.3. java.lang.ref.SoftReference
          4. 11.3.1.4. java.lang.ref.PhantomReference
        2. 11.3.2. Reference Queues
      4. 11.4. Practical Applications
        1. 11.4.1. A Weak Collection
        2. 11.4.2. Testing WeakHashSet
      5. 11.5. A Weak Listener
      6. 11.6. When to Use References
    15. 12. Tiger: JDK 1.5
      1. 12.1. New Language Features
        1. 12.1.1. For Each
        2. 12.1.2. Autoboxing with Variables
          1. 12.1.2.1. Implicit casting
          2. 12.1.2.2. Ambiguous autoboxing
        3. 12.1.3. Enums
          1. 12.1.3.1. Adding functionality to enums
          2. 12.1.3.2. Interfaces and hierarchies
        4. 12.1.4. Static Imports
        5. 12.1.5. Variable Arguments
      2. 12.2. Generics
        1. 12.2.1. The Syntax of Generics
          1. 12.2.1.1. Nested generics
          2. 12.2.1.2. Multi-generics
          3. 12.2.1.3. Bounds
          4. 12.2.1.4. Wildcards
          5. 12.2.1.5. Lower bounds
          6. 12.2.1.6. Raw types
        2. 12.2.2. Erasure
          1. 12.2.2.1. Cracking type safety
        3. 12.2.3. Casting Generics
        4. 12.2.4. Generic Methods
        5. 12.2.5. Inference
          1. 12.2.5.1. Simple generic inference
          2. 12.2.5.2. Masking methods with inference
          3. 12.2.5.3. Inference based on return type
          4. 12.2.5.4. Generic parameter-based inference
          5. 12.2.5.5. Context-sensitive inference
          6. 12.2.5.6. Avoiding inference through explicit declaration
        6. 12.2.6. Generic Gotchas
          1. 12.2.6.1. Going overboard
          2. 12.2.6.2. Compile-time class conformance
      3. 12.3. Other Improvements in Tiger
    16. About the Author
    17. Colophon
    18. SPECIAL OFFER: Upgrade this ebook with O’Reilly