Professional Java is less about knowing every API and more about shipping types that are hard to misuse: clear names, tight visibility, honest failures, tests at the boundary, logs an operator can use at 3 a.m.
This chapter is the craft layer. Language topics (equals, exceptions, collections) still apply — here we assemble them into habits.
JUnit 5 (Jupiter) is the professional default. A test class is an ordinary class; methods annotated @Test are the specs.
class MoneyTest {
@Test
void rejectsNegativeCents() {
assertThrows(IllegalArgumentException.class, () -> new Money(-1, "INR"));
}
@ParameterizedTest
@CsvSource({"100,INR,100", "0,USD,0"})
void storesCents(long cents, String ccy, long expected) {
assertEquals(expected, new Money(cents, ccy).cents());
}
@BeforeEach
void setUp() { /* fresh state — do not reuse mutable fixtures across tests */ }
}
assertEquals(expected, actual) — expected first. assertThrows for exceptions. assertThat (AssertJ) if the project already uses it.@BeforeEach / @AfterEach for lifecycle. Prefer local variables over shared mutable fields.Clock (java.time) into types that need “now.”@Disabled without a ticket is rot. Flaky tests are defects — quarantine or fix.When not to test: getters that do nothing; generated record accessors; asserting the framework works. When to test: parsing, arithmetic, state machines, concurrency protocols, exception translation, anything you would be ashamed to break in prod.
Use a facade (SLF4J is the industry default) over a backend (Logback, Log4j2, java.util.logging). Do not System.out.println in library or service code. Do not e.printStackTrace().
private static final Logger log = LoggerFactory.getLogger(AccountService.class);
log.debug("loaded {} entries", n); // parameterized — no concat if level off
log.info("account opened {}", accountId);
log.warn("retry {} of {}", attempt, max, e); // pass the exception as the last arg
toString when the level is off. log.debug("x " + big) always builds the string.java.util.logging is in the JDK; fine for tiny tools. Applications should still pick one facade and stick to it.Null is legal in Java. Professional code makes it boring.
Objects.requireNonNull at the boundary.Optional, not null.boolean/int fields over wrappers unless null is a real third state you named.@Nullable / @NonNull annotations help tools (NullAway, IDEA). Use the house standard; do not mix three annotation libraries.Optional on fields to “be safe.” See Streams, Optional, and Lambdas.public User rename(String id, String newName) {
Objects.requireNonNull(id, "id");
Objects.requireNonNull(newName, "newName");
if (newName.isBlank()) throw new IllegalArgumentException("newName blank");
...
}
List.of), is/has for booleans.java.time (Instant, LocalDate, Duration). Never Date / Calendar in new code. Inject Clock.Bloch’s book is still the professional common language. Internalize these; do not quote chapter numbers in reviews unless asked.
Boolean.valueOf, List.of).List not ArrayList) except when a constructor or factory documents the implementation.On Java 17–21 these are standard (or standard enough):
int x = switch (day) { case MON, TUE -> 1; default -> 0; }; — must be exhaustive; no fall-through with arrows.if (o instanceof String s).switch (shape) { case Circle c -> …; case Rect r -> …; }. On 17 this was preview — do not ship preview features unless the team agrees.int label = switch (status) {
case NEW, PENDING -> 0;
case ACTIVE -> 1;
case CLOSED -> 2;
};
Date, Instant, and epoch millis without naming the zone.~ Good luck with your revision! ~