Slashdot Mirror


Java Puzzlers

Kylar writes "When you have spare time and decide to do something with a book (That's like an analog webpage, for the neuronauts among us), how often do you turn to a computer related book? How often has it happened in the last year? Right. The problem being that computer books are often either: a) boring, b) difficult to read, c) poorly written, or possibly: d) made of cheese." Read on for the rest of Kylars' review. Traps, Pitfalls, and Corner Cases - Java Puzzlers author Joshua Bloch, Neal Gafter pages 282 publisher Addison-Wesley rating 9 out of 10 reviewer Tom Byrne ISBN 0-321-33678-X summary 95 Corner cases and traps that any serious java developer should be aware of (or entertained by.)

Java Puzzlers is none of the above(*), being well written, amusing, whimsical, and above all, informative. Bloch and Gafter have brought us a book that entertains us with corner-cases, one-in-a-million chances and other happenings that explore the ins, outs, and guts of the Java Programming Language.

Anyone who has been a serious java programmer in the last several years should know the name Josh Bloch, and more importantly, should have read his book Effective Java. Josh, acting as java's platform architect has been directing and guiding Java into it's current incarnation as a mature, robust (Cue the laughter from the peanut gallery) programming language.

This book primarily references the Java 1.5 programming language, and some of the puzzles are 1.5-specific, although a significant portion of the problems are applicable to previous versions. Also, this book is aimed towards people who are competent-to-expert java programmers, and although there is a lot of good information, people who are new to Java will probably be a bit lost. As it stands, I have 7 years of Java experience, and I was only able to figure out about 15% of the puzzles without resorting to code, or more frequently the answer. The reason that I didn't stop to try out most of these problems is that the book is eminently readable, and difficult to put down (unusual for a computer book, and doubly so for one that delves deeply into a language specification document.)

This book dives into a lot of esoteric bits of the Java Language Specification, also known as "The Big Paper That Sometimes Tells Us Why Java Acts Like That," and there are lots of bits in there that don't even make sense, let alone seem intuitive.

Divided into 10 parts, each part presents a series of different code problems that usually present a small method or class that looks innocuous, but in reality exposes a piece of behavior that is strange, spectacular, or, more often, completely confusing. The book exposes flaws in the language, including one of my personal pet peeves, their inability to have a consistent Date object, and this is noted in Puzzle 62 by my favorite line in the book: "The lesson for API designers is: If you can't get it right the first time, at least get it right the second..."

One topic that I found was a continually recurring theme had to do with handling primitives and what happens when they are cast into different types. Java provides a lot of ways to deal with primitives, and for the most part, they play nicely with each other. There are several occurrences that really surprised me. A perfect example is the following innocent statements:

byte b = -1;
char c = (char)b;

so c=-1, right? Wrong. Places like this are things that you could potentially knock your head against the wall trying to figure out why something doesn't do what it appears to do.
(In this case, byte is signed, char isn't, and the widening cast fills in bits, leaving c=65535.)

A good job is also done describing best-practices for API and library designers, as well as us, the more mundane programmers.

The only downside (from my background and point of view - that of an applications architect, and not generally as a language or API designer) - is that some of the amazing optical illustrations can cause dizziness and nausea - although I can't guarantee that won't happen by the loops and twists that your mind will be tied into because of the puzzles.

Lastly, Bloch & Gafter include an appendix that serves as a summary to all the pitfalls and traps that are introduced in the book, and almost could be an appendix to Bloch's 'Effective Java'.

The bottom line is that in reading this book I learned a fair amount about several edge cases and issues that I had actually encountered - and it increased my understanding as to HOW java does things - although I'm fairly certain that I'll never understand the WHY. And most of all - I enjoyed this book, from start to finish, and that's rare, and worth the time.

You can purchase Java Puzzlers from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

6 of 239 comments (clear)

  1. garbage collector by Janek+Kozicki · · Score: -1, Offtopic

    official thread for flames about garbage collector in java.

    for example how many people have discovered that java is fast as long as the benchamrk lasts less than, say, 10000 cycles, and slows down to crawl, when there are millions of cycles? VM decides to launch gc when CPU is under havy load (to reduce the load), not beforehand, when users would notice.

    --
    #
    #\ @ ? Colonize Mars
    #
  2. Java Puzzle by Anonymous Coward · · Score: -1, Offtopic

    Anyone figure out why the VM needs to eat up so much RAM?

  3. Books and Papers by NitsujTPU · · Score: 0, Offtopic

    When you have spare time and decide to do something with a book (That's like an analog webpage, for the neuronauts among us), how often do you turn to a computer related book? How often has it happened in the last year? Right. The problem being that computer books are often either: a) boring, b) difficult to read, c) poorly written, or possibly: d) made of cheese

    Being a computer science grad student... I imagine myself to be among the nerdy. Interestingly, I own an assortment of books, spend most of my time that isn't in a lab in a library, and read about 6-7 hard-copy academic papers a week, in addition to an assortment of books. When I have free time, I read books on AI.

  4. Re:Buy it here! by Anonymous Coward · · Score: -1, Offtopic

    Why is this guy trolling his referral link "informative"???

  5. bi7ch by Anonymous Coward · · Score: -1, Offtopic

    Tryi8g to dissect

  6. Re:Buy it here! by Hamilton+Publius · · Score: -1, Offtopic

    "Iran's stance has always been clear on this ugly phenomenon [i.e., Israel]. We have repeatedly said that this cancerous tumor of a state should be removed from the region."

    No, those are not the words of Iran's president, Mahmoud Ahmadinejad, speaking last week. Rather, that was Ali Khamenei, the Islamic Republic of Iran's supreme leader, in December 2000.

    In other words, Ahmadinejad's call for the destruction of Israel was nothing new but conforms to a well-established pattern of regime rhetoric and ambition. "Death to Israel!" has been a rallying cry for the past quarter-century. Mr. Ahmadinejad quoted Ayatollah Khomeini, its founder, in his call on October 26 for genocidal war against Jews: "The regime occupying Jerusalem must be eliminated from the pages of history," Khomeini said decades ago. Mr. Ahmadinejad lauded this hideous goal as "very wise."

    In December 2001, Ali Akbar Hashemi Rafsanjani, a former Iranian president and still powerful political figure, laid the groundwork for an exchange of nuclear weapons with Israel: "If a day comes when the world of Islam is duly equipped with the arms Israel has in possession, the strategy of colonialism would face a stalemate because application of an atomic bomb would not leave anything in Israel but the same thing would just produce minor damages in the Muslim world."

    In like spirit, a Shahab-3 ballistic missile (capable of reaching Israel) paraded in Tehran last month bore the slogan "Israel Should Be Wiped Off the Map."

    The threats by Messrs. Khamenei and Rafsanjani prompted yawns but Mr. Ahmadinejad's statement roused an uproar.

    The U.N. secretary-general, Kofi Annan, expressed "dismay," the U.N. Security Council unanimously condemned it, and the European Union condemned it "in the strongest terms." Prime Minister Martin of Canada deemed it "beyond the pale," Prime Minister Blair of Britain expressed "revulsion," and the French foreign minister, Philippe Douste-Blazy, announced that "for France, the right for Israel to exist should not be contested." Le Monde called the speech a "cause for serious alarm," Die Welt dubbed it "verbal terrorism," and a London Sun headline proclaimed Ahmadinejad the "most evil man in the world."

    The governments of Turkey, Russia, and China, among others, expressly condemned the statement. Maryam Rajavi of the National Council of Resistance of Iran, a leading opposition group, demanded that the European Union rid the region of the "hydra of terrorism and fundamentalism" in Tehran. Even the Palestinian Authority's Saeb Erekat spoke against Mr. Ahmadinejad: "Palestinians recognize the right of the state of Israel to exist, and I reject his comments." The Cairene daily Al-Ahram dismissed his statement as "fanatical" and spelling disaster for Arabs.

    Iranians were surprised and suspicious. Why, some asked, did the mere reiteration of long-standing policy prompt an avalanche of outraged foreign reactions?

    In a constructive spirit, I offer them four reasons. First, Mr. Ahmadinejad's virulent character gives the threats against Israel added credibility. Second, he in subsequent days defiantly repeated and elaborated on his threats. Third, he added an aggressive coda to the usual formulation, warning Muslims who recognize Israel that they "will burn in the fire of the Islamic umma [nation]."

    This directly targets the Palestinians and several Arab states, but especially neighboring Pakistan. Just a month before Mr. Ahmadinejad spoke, the Pakistani president, Pervez Musharraf, stated that "Israel rightly desires security." He envisioned the opening of embassies in Israel by Muslim countries like Pakistan as a "signal for peace." Mr. Ahmadinejad perhaps indicated an intent to confront Pakistan over relations with Israel.

    Finally, Israelis estimate that the Iranians could, within six months, have the means to build an atomic bomb. Mr. Ahmadinejad implicitly confirmed this rapid timetable when he warned that after just "a short period ... the process of the