Return to site

[VV102] The Java 21 Newsletter

Type Erasure, Records, Access privilege, DateTimeFormatter, Primitive Streams

· java21,java,ocp

🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!

Type Erasure

How would you answer this question:

Given:

Does the code compile?

A) True

B) False

#PathToJava21 #java #certification

💡 Solution

True

The code compiles because var infers the type of mapOfCEOs as HashMap.

This happens due to the diamond operator (<>), which defaults to the raw type when no explicit type is provided.

The put method accepts String arguments, which are subtypes of Object, making the calls valid.

Note 1: This is type erasure, not strict type inference, and specifying HashMap explicitly is better for type safety.

Note 2: Type erasure in Java removes all generic type information during compilation, replacing type parameters with their bounds (or Object if none are specified).

This ensures backward compatibility and enforces type safety at compile time, but at runtime, all generics are treated as raw types (e.g., List and List both become List).





Records

Given:

Which record(s) compile? (Select 2)

A) WithInstanceField

B) WithStaticField

C) ExtendingClass

D) ImplementingInterface

#PathToJava21 #java #certification

💡 Solution

Let's explore one record at a time:

Does not compile because the Instance field 'fuz' is not allowed in the record.

Does compile.

It does not compile because no extends clause is allowed for record.

Does compile.

Details on JEP 395: https://openjdk.org/jeps/395





Access privilege

Given:

Choose the right statement.

A) SmartPhone interface does not compile

B) Iphone15 class does not compile

C) Everything compiles

D) An exception is thrown at running Iphone15.ring();

#PathToJava21 #java #certification

💡 Solution

Correct Answer: B) Iphone15 class does not compile

Explanation:

In the provided code:

  • SmartPhone Interface

The SmartPhone interface declares a method boolean ring();

This compiles correctly because there is nothing wrong with the syntax or definition of the interface.

  • Iphone15 Class

The Iphone15 class implements the SmartPhone interface but defines the ring() method with default (package-private) access.

According to Java rules, methods implementing an interface must have the same or greater access visibility than the interface method. Since ring() in the SmartPhone interface is implicitly public, the overriding ring() method in the Iphone15 class must also be declared public.

As written, the Iphone15 class does not compile because the ring() method attempts to reduce the access level, which is not allowed.

  • Other Options

A) The SmartPhone interface compiles correctly, so this is incorrect.

C) Not everything compiles; the Iphone15 class has a compilation error.

D) Since the code doesn’t compile, an exception cannot be thrown at runtime.

  • Key Point:

The error message is:

'ring()' in 'Iphone15' clashes with 'ring()' in 'SmartPhone'; attempting to assign weaker access privileges ('package-private'); was 'public'.





DateTimeFormatter

Given:

Which variable prints 2025-W01-2 (present-day is 12/31/2024).

A) format1

B) format2

C) format3

D) format4

#PathToJava21 #java #certification

💡 Solution

Correct Answer: B) format2

Explanation:

var format1 = new DateTimeFormatter(ISO_WEEK_DATE);

This does not compile because there is no such constructor in the DateTimeFormatter class.

var format2 = DateTimeFormatter.ISO_WEEK_DATE;

This compiles successfully.

ISO_WEEK_DATE is a predefined constant in the DateTimeFormatter class that formats dates according to the ISO-8601 week-based calendar system.

var format3 = new DateFormat(WEEK_OF_YEAR_FIELD);

This does not compile because DateFormat is an abstract class and cannot be directly instantiated.

var format4 = DateFormat.getDateInstance(WEEK_OF_YEAR_FIELD);

This does not work as expected because DateFormat does not provide functionality to format a LocalDate instance in this manner.

So,

When calling now.format(REPLACE_HERE) with the correct formatter,

format2 (which is DateTimeFormatter.ISO_WEEK_DATE) successfully formats the date 2024-12-31 into the ISO week date format: 2025-W01-2.





Primitive Streams

Given:

What is printed?

A) true

B) false

C) 3.3

D) An exception is thrown at runtime

E) Compilation fails

#PathToJava21 #java #certification

💡 Solution

The correct answer is: E) Compilation fails

Explanation:

  • DoubleStream and Primitive Streams:

DoubleStream is part of Java's primitive streams API, specifically designed to handle double primitives directly, avoiding boxing overhead.

Methods like anyMatch in DoubleStream accept predicates designed for double primitives, not boxed Double objects.

This requires a DoublePredicate instead of a generic Predicate.

  • Type Mismatch:

The code attempts to use a Predicate (a generic functional interface) with DoubleStream.anyMatch.

However, DoubleStream.anyMatch expects a DoublePredicate (a specialized functional interface for double).

As a result, the code does not compile due to the type mismatch.

  • Error at Compile Time:

The compiler will generate an error like:

incompatible types: Predicate cannot be converted to DoublePredicate

  • Correcting the Code:

To fix the compilation error, use a DoublePredicate instead of a Predicate:

DoubleStream doubleStream = DoubleStream.of(3.3, 4, 5.25, 6.66);
DoublePredicate doublePredicate = d -> d < 5;

System.out.println(doubleStream.anyMatch(doublePredicate));

This corrected code will compile and print true, as 3.3 is less than 5.