Home > Store

Java Puzzlers: Traps, Pitfalls, and Corner Cases

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

Java Puzzlers: Traps, Pitfalls, and Corner Cases

Book

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

eBook

  • Your Price: $38.39
  • List Price: $47.99
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader PDF The popular standard, used most often with the free Acrobat® Reader® software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.

About

Features

The author of the classic best practices guide Effective Java now teaches programmers the flipside...how to avoid worst practices!

° Gives an honest assessment of the Java programming language

° Highly enjoyable read that gives any programmer the power to be smarter than the programmer next to them

° Puzzles are challenging and fun, but VERY practical and effective for teaching how to avoid problematic program behavior

Description

  • Copyright 2005
  • Dimensions: 7-3/8" x 9-1/4"
  • Pages: 312
  • Edition: 1st
  • Book
  • ISBN-10: 0-321-33678-X
  • ISBN-13: 978-0-321-33678-1

"Every programming language has its quirks. This lively book reveals oddities of the Java programming language through entertaining and thought-provoking programming puzzles."

--Guy Steele, Sun Fellow and coauthor of The Java™ Language Specification

"I laughed, I cried, I threw up (my hands in admiration)."

--Tim Peierls, president, Prior Artisans LLC, and member of the JSR 166 Expert Group

How well do you really know Java? Are you a code sleuth? Have you ever spent days chasing a bug caused by a trap or pitfall in Java or its libraries? Do you like brainteasers? Then this is the book for you!

In the tradition of Effective Java™, Bloch and Gafter dive deep into the subtleties of the Java programming language and its core libraries. Illustrated with visually stunning optical illusions, Java™ Puzzlers features 95 diabolical puzzles that educate and entertain. Anyone with a working knowledge of Java will understand the puzzles, but even the most seasoned veteran will find them challenging.

Most of the puzzles take the form of a short program whose behavior isn't what it seems. Can you figure out what it does? Puzzles are grouped loosely according to the features they use, and detailed solutions follow each puzzle. The solutions go well beyond a simple explanation of the program's behavior--they show you how to avoid the underlying traps and pitfalls for good. A handy catalog of traps and pitfalls at the back of the book provides a concise taxonomy for future reference.

Solve these puzzles and you'll never again fall prey to the counterintuitive or obscure behaviors that can fool even the most experienced programmers.



Extras

Companion Site

Visit the Companion Web Site related to this title.

Sample Content

Downloadable Sample Chapter

Download the Sample Chapter related to this title.

Table of Contents

Preface xi

Chapter 1: Introduction 1

Chapter 2: Expressive Puzzlers 5

Puzzle 1: Oddity 5

Puzzle 2: Time for a Change 7

Puzzle 3: Long Division 9

Puzzle 4: It's Elementary 11

Puzzle 5: The Joy of Hex 13

Puzzle 6: Multicast 15

Puzzle 7: Swap Meat 17

Puzzle 8: Dos Equis 19

Puzzle 9: Tweedledum 21

Puzzle 10: Tweedledee 23

Chapter 3: Puzzlers with Character 25

Puzzle 11: The Last Laugh 25

Puzzle 12: ABC 27

Puzzle 13: Animal Farm 29

Puzzle 14: Escape Rout 31

Puzzle 15: Hello Whirled 33

Puzzle 16: Line Printer 35

Puzzle 17: Huh? 37

Puzzle 18: String Cheese 39

Puzzle 19: Classy Fire 41

Puzzle 20: What's My Class? 43

Puzzle 21: What's My Class, Take 2 45

Puzzle 22: Dupe of URL 47

Puzzle 23: No Pain, No Gain 49

Chapter 4: Loopy Puzzlers 53

Puzzle 24: A Big Delight in Every Byte 53

Puzzle 25: Inclement Increment 55

Puzzle 26: In the Loop 57

Puzzle 27: Shifty i's 59

Puzzle 28: Looper 61

Puzzle 29: Bride of Looper 63

Puzzle 30: Son of Looper 65

Puzzle 31: Ghost of Looper 67

Puzzle 32: Curse of Looper 69

Puzzle 33: Looper Meets the Wolfman 71

Puzzle 34: Down for the Count 73

Puzzle 35: Minute by Minute 75

Chapter 5: Exceptional Puzzlers 77

Puzzle 36: Indecision 77

Puzzle 37: Exceptionally Arcane 79

Puzzle 38: The Unwelcome Guest 81

Puzzle 39: Hello, Goodbye 83

Puzzle 40: The Reluctant Constructor 85

Puzzle 41: Field and Stream 87

Puzzle 42: Thrown for a Loop 89

Puzzle 43: Exceptionally Unsafe 93

Puzzle 44: Cutting Class 97

Puzzle 45: Exhausting Workout 101

Chapter 6: Classy Puzzlers 105

Puzzle 46: The Case of the Confusing Constructor 105

Puzzle 47: Well, Dog My Cats! 107

Puzzle 48: All I Get Is Static 109

Puzzle 49: Larger Than Life 111

Puzzle 50: Not Your Type 113

Puzzle 51: What's the Point? 115

Puzzle 52: Sum Fun 119

Puzzle 53: Do Your Thing 123

Puzzle 54: Null and Void 125

Puzzle 55: Creationism 127

Chapter 7: Library Puzzlers 131

Puzzle 56: Big Problem 131

Puzzle 57: What's in a Name? 133

Puzzle 58: Making a Hash of It 137

Puzzle 59: What's the Difference? 139

Puzzle 60: One-Liners 141

Puzzle 61: The Dating Game 143

Puzzle 62: The Name Game 145

Puzzle 63: More of the Same 147

Puzzle 64: The Mod Squad 149

Puzzle 65: A Strange Saga of a Suspicious Sort 152

Chapter 8: Classier Puzzlers 157

Puzzle 66: A Private Matter 157

Puzzle 67: All Strung Out 161

Puzzle 68: Shades of Gray 163

Puzzle 69: Fade to Black 165

Puzzle 70: Package Deal 167

Puzzle 71: Import Duty 169

Puzzle 72: Final Jeopardy 171

Puzzle 73: Your Privates Are Showing 173

Puzzle 74: Identity Crisis 175

Puzzle 75: Heads or Tails? 177

Chapter 9: More Library Puzzlers 183

Puzzle 76: Ping Pong 183

Puzzle 77: The Lock Mess Monster 185

Puzzle 78: Reflection Infection 189

Puzzle 79: It's a Dog's Life 193

Puzzle 80: Further Reflection 195

Puzzle 81: Charred Beyond Recognition 197

Puzzle 82: Beer Blast 199

Puzzle 83: Dyslexic Monotheism 201

Puzzle 84: Rudely Interrupted 203

Puzzle 85: Lazy Initialization 205

Chapter 10: Advanced Puzzlers 209

Puzzle 86: Poison-Paren Litter 209

Puzzle 87: Strained Relations 211

Puzzle 88: Raw Deal 213

Puzzle 89: Generic Drugs 217

Puzzle 90: It's Absurd, It's a Pain, It's Superclass! 221

Puzzle 91: Serial Killer 224

Puzzle 92: Twisted Pair 229

Puzzle 93: Class Warfare 231

Puzzle 94: Lost in the Shuffle 233

Puzzle 95: Just Desserts 237

Appendix A Catalog of Traps and Pitfalls 239

Appendix B Notes on the Illusions 259

References 265

Index 271

Preface

Untitled Document Like many books, this one had a long gestation period. We've collected Java puzzles for as long as we've worked with the platform: since mid-1996, in case you're curious. In early 2001, we came up with the idea of doing a talk consisting entirely of Java puzzles. We pitched the idea to Larry Jacobs, then at Oracle, and he bought it hook, line, and sinker.

We gave the first "Java Puzzlers" talk at the Oracle Open World conference in San Francisco in November 2001. To add a bit of pizazz, we introduced ourselves as "Click and Hack, the Type-it Brothers" and stole a bunch of jokes from Tom and Ray Magliozzi of Car Talk fame. The presentation was voted best-in-show, and probably would have been even if we hadn't voted for ourselves. We knew we were on to something.

Dressed in spiffy blue mechanic's overalls emblazoned with the "cup and steam" Java logo, we recycled the Oracle talk at JavaOne 2002 to rave reviews--at least from our friends. In the years that followed, we came up with three more "Java Puzzlers" talks and presented them at countless conferences, corporations, and colleges in cities around the globe, from Oslo to Tokyo. The talks were almost universally well liked, and we got very little fruit thrown at us. In the March 2003 issue of Linux Magazine, we published an article consisting entirely of Java puzzles and received almost no hate mail. This book contains nearly all the puzzles from our talks and articles and many, many more.

Although this book draws attention to the traps and pitfalls of the Java platform, we do not mean to denigrate it in any way. It is because we love the Java platform that we've devoted nearly a decade of our professional lives to it. Every platform with enough power to do real work has some problems, and Java has far fewer than most. The better you understand the problems, the less likely you are to get hurt by them, and that's where this book comes in.

Most of the puzzles in the book focus on short programs that appear to do one thing but actually do something else. That's why we've chosen to decorate the book with optical illusions--drawings that appear to be one thing but are actually another. Also, you can stare at them while you're trying to figure out what in the world the programs do.

Above all, we wanted this book to be fun. We sincerely hope that you enjoy solving the puzzles as much as we enjoyed writing them and that you learn as much from them as we did.

And by all means, send us your puzzlers! If you have a puzzle that you think belongs in a future edition of this book, write it on the back of a $20 bill and send it to us, or e-mail it to puzzlers@javapuzzlers.com. If we use your puzzle, we'll give you credit.

Last but not least, don't code like my brother.

Josh Bloch
Neal Gafter
San Jose, California
May 2005

Index

Download the Index file related to this title.

Updates

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