Home > Store

Effective Java  Programming Language Guide

Register your product to gain access to bonus material or receive a coupon.

Effective Java Programming Language Guide

Book

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 2001
  • Edition: 1st
  • Book
  • ISBN-10: 0-201-31005-8
  • ISBN-13: 978-0-201-31005-4

A new edition of this title is available, ISBN-10: 0321356683 ISBN-13: 9780321356680

Sample Content

Online Sample Chapter

Java Methods

Downloadable Sample Chapter

Click below for Sample Chapter related to this title:
blochch6.pdf

blochch7.pdf

ch7generalp.pdf

steeleforewordbloch.pdf

Table of Contents



Foreword.


Preface.


Acknowledgements.


1. Introduction.


2. Creating and Destroying Objects.

Consider Providing Static Factory Methods Instead of Constructors.

Enforce the Singleton Property with a Private Constructor.

Enforce Noninstantiability with a Private Constructor.

Avoid Creating Duplicate Objects.

Eliminate Obsolete Object References.

Avoid Finalizers.



3. Methods Common to All Objects.

Obey the General Contract when Overriding Equals.

Always Override HashCode When You Override Equals.

Always Override to String.

Override Clone Judiciously.

Consider Implementing Comparable.



4. 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 Non-Static.



5. Substitutes for C Constructs.

Replace Structures with Classes.

Replace Unions with Class Hierarchies.

Replace Enums with Classes.

Replace Function Pointers with Classes and Interfaces.



6. Methods.

Check Parameters for Validity.

Make Defensive Copies when Needed.

Design Method Signatures Carefully.

Use Overloading Judiciously.

Return Zero-Length Arrays, Not Nulls.

Write Doc Comments for All Exposed API Elements.



7. General Programming.

Minimize the Scope of Local Variables.

Know and Use the Libraries.

Avoid Float and Double if Exact Answers are Required.

Avoid Strings where Other Types are More Appropriate.

Beware the Performance of String Concatenation.

Refer to Objects by their Interfaces.

Prefer Interfaces to Reflection.

Use Native Methods Judiciously.

Optimize Judiciously.

Adhere to Generally Accepted Naming Conventions.



8. Exceptions.

Use Exceptions Only for Exceptional Conditions.

Use Checked Exceptions for Recoverable Conditions, Runtime Exceptions for Programming Errors.

Avoid Unnecessary Use of Checked Exceptions.

Favor the Use of Standard Exceptions.

Throw Exceptions Appropriate to the Abstraction.

Document All Exceptions Thrown by Each Method.

Include Failure-Capture Information in Detail Messages.

Strive for Failure Atomicity.

Don't Ignore Exceptions.



9. Threads.

Synchronize Access to Shared Mutable Data.

Avoid Excessive Synchronization.

Never Invoke Wait Outside a Loop.

Don't Depend on the Thread Scheduler.

Document Thread-Safety.

Avoid Thread Groups.



10. Serialization.

Implement Serializable Judiciously.

Consider Using a Custom Serialized Form.

Write ReadObject Methods Defensively.

Provide a ReadResolve Method when Necessary.



References.


Index.

Preface

In 1996 I pulled up stakes and headed west to work for JavaSoft, as it was then known, because it was clear that was where the action was. In the intervening five years I've served as Java Platform Libraries Architect. I've designed, implemented and maintained many of the libraries, and served as a consultant for many others. Presiding over these libraries as the Java platform matured was a once-in-a-lifetime opportunity. It is no exaggeration to say that I had the privilege to work with some of the great software engineers of our generation. In the process, I learned a lot about the Java programming language--what works, what doesn't, and how to use the language and its libraries to best effect.

This book is my attempt to share my experience with you, so that you can imitate my successes while avoiding my failures. I borrowed the format from Scott Meyers's Effective C++ Meyers98, which consists of fifty items, each conveying one specific rule for improving your programs and designs. I found the format to be singularly effective and I hope you do too.

In many cases, I took the liberty of illustrating the items with real-world examples from the Java platform libraries. When describing something that could have been done better, I tried to pick on code that I wrote myself, but occasionally I pick on something written by a colleague. I sincerely apologize if, despite my best efforts, I've offended anyone. Negative examples are cited not to cast blame but in the spirit of cooperation, so that all of us can benefit from the experience of those who've gone before.

While this book is not targeted solely at developers of reusable components, it is inevitably colored by my experience writing such components over the past two decades. I naturally think in terms of exported APIs (Application Programming Interfaces) and I encourage you to do likewise. Even if you aren't developing reusable components, thinking in these terms tends to improve the quality of the software you write. Furthermore, it's not uncommon to write a reusable component without knowing it: you write something useful, share it with your buddy across the hall, and before long you have half a dozen users. At this point, you no longer have the flexibility to change the API at will, and are thankful for all the effort that you put into designing the API when you first wrote the software.

My focus on API design may seem a bit unnatural to devotees of the new lightweight software development methodologies, such as Extreme Programming Explained Beck99. These methodologies emphasize writing the simplest program that could possibly work. If you're using one of these methodologies you'll find that a focus on API design serves you well in the refactoring process. The fundamental goals of refactoring are the improvement of system structure and the avoidance of code duplication. These goals are impossible to achieve in the absence of well-designed APIs for the components of the system.

No language is perfect, but some of them are excellent. I have found the Java programming language and its libraries to be immensely conducive to quality and productivity, and a joy to work with. I hope this book captures my enthusiasm and helps make your use of the language more effective and enjoyable.

Josh Bloch
Cupertino, California
April, 2001


0201310058P04232001

Index

A

abstract classes
adding an aspect to, 31
as replacement for discriminated union, 101
designing for inheritance, 82
evolution of, vs. interfaces, 88
examples
adding behaviors to typesafe enum, 108
replacement for discriminated union, 101
skeletal implementation, 87
static member class, 94
for adding behaviors to typesafe enum, 108
for service provider framework, 8
for skeletal implementations, 82, 85
noninstantiability and, 12
vs. interfaces, 84-88
access control, 60
access levels, 3
method overriding and, 61
of classes and interfaces, 60
of constants, 61
of members, 60
of static member classes, 91
readResolve and, 232
accessor methods, 98
defensive copies and, 63, 124
examples
defensive copies, 124
immutability, 64
for failure-capture information, 173, 184
for information returned by toString, 44
for lower-level exception, 179
immutability and, 63
naming conventions for, 167
vs. public fields, 98-99, 103
alien methods, 196
deadlock and, 196-199
example, 197
safety failures and, 199
anonymous classes, 91, 93-94
as concrete strategy classes, 116
as function objects, 127
examples
finalizer guardian, 23
in adapters, 86

Updates

Errata

Click below for Errata related to this title:
Errata

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020