Java Puzzlers: Traps, Pitfalls, and Corner Cases [NOOK Book]

Overview

"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 ...

See more details below
Java Puzzlers: Traps, Pitfalls, and Corner Cases

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study

Want a NOOK? Explore Now

NOOK Book (eBook)
$19.99
BN.com price
(Save 44%)$35.99 List Price

Overview

"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.

Read More Show Less

Editorial Reviews

From Barnes & Noble
The Barnes & Noble Review
Think you know Java? Really know it? Try the 95 Java puzzles in this book: You just might not know the language as well as you thought. Don’t worry if Java Puzzlers' mini-programs throw you for an occasional loop (or, perhaps, an exception). Each one illustrates some intriguing pitfall, trap, or surprise you really ought to know about. Read them. Think about them. Try them. Then let Joshua Bloch and Neal Gafter unravel them. By the time you’re done, you’ll be writing more robust, resilient, bug-resistant code.

Many of these puzzles are deceptively simple. Why does a method that purports to determine if its sole argument is an odd number fail 25 percent of the time? Why does a program that divides two numbers get the answer 5 when it ought to return 1000? (Lesson: “When working with large numbers, watch out for overflow -- it’s a silent killer.”) Some take you into shrouded areas of Java. For example, one puzzle tests your knowledge of the “question mark colon” conditional operator; you’ll only get it right if you know Java’s arcane rules for determining the result type of a conditional expression. Some are just plain devilishly difficult. For example, in one, the answer can only be found in compiler-generated bytecode.

The authors’ puzzles cover the waterfront: strings, characters, text, loops, iteration, exceptions, try-finally statements, classes, methods, fields, collections, dates, inheritance, overriding, threading, reflection, I/O, nested classes, generics, serialization, binary compatibility, and more. They won’t just stretch your brain: they’ll help you avoid problems you never knew existed. Bill Camarda, from the August 2005 Read Only

Read More Show Less

Product Details

  • ISBN-13: 9780321643513
  • Publisher: Pearson Education
  • Publication date: 7/8/2005
  • Sold by: Barnes & Noble
  • Format: eBook
  • Edition number: 1
  • Pages: 312
  • Sales rank: 829,818
  • File size: 3 MB

Meet the Author

Joshua Bloch is a principal engineer at Google and a Jolt Award-winner. He was previously a distinguished engineer at Sun Microsystems and a senior systems designer at Transarc. Josh led the design and implementation of numerous Java platform features, including JDK 5.0 language enhancements and the award-winning Java Collections Framework. He holds a Ph.D. in computer science from Carnegie Mellon University.

Neal Gafter is a software engineer and Java evangelist at Google. He was previously a senior staff engineer at Sun Microsystems, where he led the development of the Java compiler and implemented the Java language features in releases 1.4 through 5.0. Neal was a member of the C++ Standards Committee and led the development of C and C++ compilers at Sun Microsystems, Microtec Research, and Texas Instruments. He holds a Ph.D. in computer science from the University of Rochester.

Read More Show Less

Read an Excerpt

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

032133678XP06102005

Read More Show Less

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

Read More Show Less

Preface

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

032133678XP06102005

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 3 Customer Reviews
  • Anonymous

    Posted June 16, 2012

    No text was provided for this review.

  • Anonymous

    Posted December 9, 2011

    No text was provided for this review.

  • Anonymous

    Posted May 14, 2011

    No text was provided for this review.

Sort by: Showing all of 3 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)