Avoid Common Java Pitfalls: Error Codes, Stream toMap, Leap Year Bugs & More
Discover the newly released Java Development Handbook (Taishan edition) featuring 34 new guidelines, solutions to Stream Collectors toMap pitfalls, unified error‑code design, leap‑year date bugs, SQL aliasing tricks, and code‑formatting tips, plus free download links and a chance to win prizes.
The Java Development Handbook (Taishan edition) has been released, adding 34 new guidelines and updating 90 descriptions, with a complete solution for unified error‑code design.
Error Code Unification
Unified error codes are five‑character strings split into a source identifier (A/B/C) and a four‑digit number, offering fast tracing, easy memorization, and standardized communication. Source A: user‑related errors; B: system or business‑logic errors; C: third‑party service errors. The hierarchy includes macro‑level, secondary, and tertiary codes.
Stream Collectors toMap Pitfall
Using stream.Collectors.toMap() can throw a NullPointerException when a null value is mapped, and duplicate keys also cause failures. The author shares personal experience of encountering NPE and recommends careful handling of nulls and duplicate keys.
Leap‑Year Date Bug
Hard‑coding 365 days for a year leads to cache expiration and database overload on leap years. Leveraging java.time.LocalDate avoids this issue by providing the correct number of days for each year.
SQL Table Alias
When multiple tables contain columns with the same name, aliasing tables (or columns) prevents ambiguous references and reduces bugs in multi‑table joins.
Code Formatting
For methods longer than ten lines, insert a blank line after a closing brace that ends a return or throw block to improve readability and maintainability.
Ternary Operator Pitfalls
The ternary operator can trigger auto‑unboxing; if an operand is null, a NullPointerException occurs. The following example demonstrates this problem:
Integer a = 1;
Integer b = 2;
Integer c = null;
Boolean flag = false;
Integer result = (flag ? a * b : c);The handbook consolidates these topics and invites developers to download the free PDF and join the giveaway.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
