0% found this document useful (0 votes)
4 views

Java_9_to_17_Interview_Problems

The document outlines key Java features from versions 9 to 17, highlighting modular development with JPMS in Java 9, local variable type inference with 'var' in Java 10, and text blocks for improved string handling in Java 13/15. It also discusses sealed classes in Java 17 for better control over inheritance and the enhancements to switch expressions in Java 14+. These features collectively improve maintainability, readability, and code efficiency while presenting certain challenges in migration and compatibility.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java_9_to_17_Interview_Problems

The document outlines key Java features from versions 9 to 17, highlighting modular development with JPMS in Java 9, local variable type inference with 'var' in Java 10, and text blocks for improved string handling in Java 13/15. It also discusses sealed classes in Java 17 for better control over inheritance and the enhancements to switch expressions in Java 14+. These features collectively improve maintainability, readability, and code efficiency while presenting certain challenges in migration and compatibility.

Uploaded by

pbecic
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java 9 – Java 17 for Strong Senior Developers

What are the key features introduced in Java 9, and how do they impact
modular development?
 Java 9 introduced the Java Platform Module System (JPMS).
 Key features:
 Modules: explicit dependencies, encapsulated packages.
 module-info.java defines exposed and required modules.
 Improves maintainability, encapsulation, and startup performance.
 Impact:
 Large applications can be broken into well-defined modules.
 Avoids classpath hell — better control over visibility.
 Challenges:
 Migration of legacy monoliths may require deep refactoring.
 Third-party library compatibility issues.

What are var and its limitations in Java 10?


 Java 10 introduced local variable type inference with 'var'.
 Allows the compiler to infer the type based on the initializer.
 Example:
 var list = new ArrayList<String>();
 Limitations:
 Only works for local variables, loops, and try-with-resources.
 Cannot be used for method parameters, fields, or return types.
 May reduce code readability if overused or used with complex types.

What are text blocks in Java 13/15 and how do they improve string handling?
 Text blocks simplify multi-line string literals using triple quotes (""" ... """).
 Introduced in Java 13 as preview; finalized in Java 15.
 Benefits:
 Avoids escaping quotes and newlines.
 Improves readability of SQL, JSON, XML snippets.
 Example:
 String sql = """
SELECT *
FROM users
WHERE active = true
""";
What is the difference between sealed classes (Java 17) and abstract
classes/interfaces?
 Sealed classes restrict which classes can extend or implement them.
 Must explicitly permit subclasses using 'permits' clause.
 Motivation:
 Improves control over inheritance and helps maintain invariants.
 Enhances exhaustiveness checking in switch expressions.
 Difference from abstract classes:
 Sealed classes control extension, not behavior enforcement.
 Works with records, interfaces, and final classes.
 Alternatives: non-sealed, final, or standard class hierarchies.

How does the new 'switch' enhancement improve code in Java 14+?
 Java 14 introduced switch expressions (preview in Java 12, finalized in Java 14).
 New features:
 Switch as an expression that returns a value.
 Use of arrow syntax and yield for multi-line blocks.
 Example:
 String result = switch(day) {
case MONDAY -> "Start";
case FRIDAY -> "End";
default -> "Mid";
};
 Benefits:
 Reduces boilerplate and fall-through errors.
 Enables functional-style programming.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy