Prev Next

Java / Java 21 Coding Standards Interview Questions

Explain the internal working of Generational ZGC and its influence on object-lifecycle coding standards?

Generational ZGC (finalized as the default ZGC mode in Java 21) splits the heap into a young generation for newly allocated objects and an old generation for objects that survive multiple collections, based on the well-established observation that most objects die young.

flowchart LR
    A[Object allocated] --> B[Young generation]
    B -->|survives a collection| C[Promoted to old generation]
    B -->|dies young, most objects| D[Reclaimed quickly]
    C -->|collected far less frequently| E[Long-lived object]

Because the young generation is collected far more frequently and cheaply than the old generation, and both collections happen concurrently with the application's own threads with only sub-millisecond pauses, Generational ZGC rewards code that allocates short-lived objects freely rather than fights against garbage collection with manual object pooling.

The coding-standard implication is a shift away from older advice to minimize allocation or reuse mutable objects for performance: freely creating short-lived, immutable objects - including records, streams, and enhanced-switch temporaries - now plays directly to the generational collector's strength, while long-lived caches of mutable state are what actually deserve scrutiny, since it is old-generation objects that are more expensive to collect.

Generational ZGC organizes the heap around the observation that:
Generational ZGC's design shifts coding-standard advice toward:

More Related questions...

What is a coding standard in Java, and why does it matter for Java 21 projects? What are the standard naming conventions for classes and interfaces in Java 21? What are the standard naming conventions for methods and variables in Java? What is the purpose of a static analysis tool like Checkstyle in a Java 21 codebase? What is a record in Java 21, and why do coding standards recommend it for data carriers? What is a text block in Java 21? Define var and explain the coding standard guidelines around its use in Java 21? What are sealed classes in Java 21? Describe the recommended package naming convention in Java style guides? List the common Javadoc conventions required in Java coding standards? What is the purpose of the @Override annotation in Java coding standards? What are the types of comments recommended in Java style guides? How do you format a pattern-matching switch expression per Java 21 coding standards? What is the purpose of the final keyword in Java 21 coding standards? How do you apply consistent indentation and brace style in Java 21 code? What is the purpose of pattern matching for instanceof in Java 21? Describe the standard naming convention for custom exception classes in Java? What are the recommended import ordering rules in Java coding standards? Why is a record preferred over a traditional POJO for immutable data carriers? How does pattern matching for switch change the coding standard for type-checking code? What is the difference between var and explicit typing under Java 21 style guides? Why should you avoid wildcard imports in Java 21 projects? How do you troubleshoot a NullPointerException using Objects.requireNonNull as a coding standard? Why doesn't Checkstyle allow tab characters in most Java 21 style guides? When should you use a sealed interface instead of an enum? What happens when you omit a break statement in a traditional switch under coding standards? How is exception handling standardized in Java 21 with multi-catch and try-with-resources? Why is virtual thread naming convention different from platform thread naming? When should you choose text blocks over string concatenation? How do you optimize import statements and static imports per a Java style guide? What is the difference between checked and unchecked exceptions under Java coding standards? Why should you prefer immutable records over mutable classes for DTOs? Explain the standard structure and member ordering of a Java class file? Which is better and why: an enhanced switch or an if-else chain for type checks? How do you apply the Single Responsibility Principle in Java 21 class design standards? Explain the lifecycle of a virtual thread under Java 21 coding standard recommendations? Explain the execution flow of a switch expression using guarded patterns? Explain the internal working of sealed class exhaustiveness checking by the compiler? How can you optimize code readability using record patterns and deconstruction? How do you troubleshoot deprecated API usage flagged by Java 21 standards tooling? Why is structured concurrency recommended over raw thread management in Java 21? What is the difference between synchronized blocks and virtual-thread-friendly designs? How does the compiler enforce exhaustiveness in a switch over sealed types? Why should you avoid finalizers in favor of try-with-resources under Java 21 standards? Explain the internal working of the Sequenced Collections API introduced in Java 21? How do you design a thread-safe class following Java 21 coding standards? When should you use module-info.java for encapsulation as per coding standards? Which is better and why: exceptions versus sealed result types for error handling? How can you optimize static analysis coverage for Java 21 features like pattern matching? Explain the internal working of Generational ZGC and its influence on object-lifecycle coding standards?
Show more question and Answers...


Comments & Discussions