Return to site

[VV117] The Java 21 Newsletter

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

QUESTION 1️⃣

How would you answer this question:

Given:

What will be printed?

A) Nothing

B) Hello from France!

C) An exception is thrown at runtime.

D) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

The correct answer is: B) Hello from France!

Explanation:

In the code, we have this line inside the sayHi method:

This uses pattern matching for instanceof,

a feature added in Java 16 (preview) and made standard in Java 17.

The syntax:

object instanceof String String

means: if object is an instance of String,

then cast it to a String and assign it to a variable named String.

While naming a variable String is legal (though not recommended),

it can still be compiled and executed successfully. So in this context:

- object is "Hello from France!", which is a String

- "Hello from France!" has more than 3 characters

- Therefore, the if condition is true

- System.out.println(object); is executed

And the output is:

Hello from France!

✅ No runtime exceptions ✅ No compile error ✅ Output is printed

(Using Object-Oriented Concepts in Java)

LESSON 1️⃣

🧑‍💻💡 Java 21 mini lesson – Mastering switch Expressions with yield (featuring French poets 🇫🇷✍️)

Java’s modern switch expression syntax with case L -> makes your code cleaner, safer, and more expressive

— especially when you need to return values from each branch.

Given:

✅ Why this matters:

🟣 switch expressions return values with yield

🟣 case L -> avoids fall-through bugs (unlike case L:)

🟣 Block syntax lets you handle multiple instructions

🧠 Next time you use switch, try this lambda-like syntax — your future self (and your teammates) will thank you!

QUESTION 2️⃣

How would you answer this question:

Given:

How many times "Hello from France!" will be printed?

A) Zero

B) Two

C) Three

D) An exception is thrown at runtime.

E) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

Let's walk through the code together:

Key Points:

🟣 There are two nested for loops.

🟣 The outer loop is labeled stopOnThree.

🟣 Inside the inner loop, there’s a condition: if (i + j > 3) → then break stopOnThree; — this exits both loops, not just the inner one.

🟣 The print statement is in the outer loop, after the inner loop.

Let's simulate the iterations:

Outer loop (i from 0 to 2):

🟣 i = 0

Inner loop (j from 0 to 2)

i + j = 0 + 0 = 0 → OK

i + j = 0 + 1 = 1 → OK

i + j = 0 + 2 = 2 → OK

End of inner loop → print → "Hello from France!" ✅

🟣 i = 1

i + j = 1 + 0 = 1 → OK

i + j = 1 + 1 = 2 → OK

i + j = 1 + 2 = 3 → OK

End of inner loop → print → "Hello from France!" ✅

🟣 i = 2

i + j = 2 + 0 = 2 → OK

i + j = 2 + 1 = 3 → OK

i + j = 2 + 2 = 4 → ❗Condition true → break stopOnThree; → exits outer loop completely

So the print happens only twice, for i = 0 and i = 1.

✅ Correct Answer: B) Two

The break Statement: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

(Controlling Program Flow)

QUESTION 3️⃣

How would you answer this question:

You have to create the Java module AirbusA380Plant named com.airbus.a380.plant .

The com.airbus.plant module depends on three modules:

com.airbus.toulouse.assembly,

com.airbus.broughton.wings, and

com.airbus.hamburg.fuselage

How would you define the module-info.java related file?

A)

module com.airbus.a380.plant{

import com.airbus.toulouse.assembly;

import com.airbus.broughton.wings;

import com.airbus.hamburg.fuselage;

}

B)

module com.airbus.a380.plant{

uses com.airbus.toulouse.assembly;

uses com.airbus.broughton.wings;

uses com.airbus.hamburg.fuselage;

}

C)

module com.airbus.a380.plant{

opens com.airbus.toulouse.assembly;

opens com.airbus.broughton.wings;

opens com.airbus.hamburg.fuselage;

}

D)

module com.airbus.a380.plant{

requires com.airbus.toulouse.assembly;

requires com.airbus.broughton.wings;

requires com.airbus.hamburg.fuselage;

}

E)

module com.airbus.a380.plant{

exports com.airbus.toulouse.assembly;

exports com.airbus.broughton.wings;

exports com.airbus.hamburg.fuselage;

}

#PathToJava21 #java #certification

💡 Solution

The correct answer is:

D)

module com.airbus.a380.plant {

requires com.airbus.toulouse.assembly;

requires com.airbus.broughton.wings;

requires com.airbus.hamburg.fuselage;

}

✅ Why this is correct:

The requires directive is used in a module-info.java file to declare dependencies on other modules. Since com.airbus.a380.plant depends on the three mentioned modules, it must require them.

❌ Why the others are incorrect:

A)

import ...;

→ Invalid syntax in module-info.java. import is used in regular Java files, not in module descriptors.

B)

uses ...;

→ uses is for service loading, when the module is a consumer of a service defined via ServiceLoader. This is not what's described in your scenario.

C)

opens ...;

→ opens is used for reflection access, typically in frameworks like Spring. It's not for basic module dependency declaration.

E)

exports ...;

→ exports is used to expose a package from the current module to others, not to declare dependencies.

Understanding Java 9 Modules: https://www.oracle.com/corporate/features/understanding-java-9-modules.html

(Packaging and Deploying Java Code)

LESSON 2️⃣

🧑‍💻💡 Java 21 mini lesson Switch and enum

🍽️ Java devs, did you know this about switch expressions?

Switch expressions in Java can be elegant and concise — if you respect the rules.

Take this example using a PlatFrancais enum 🇫🇷:

✔️ No default needed

✔️ No yield headaches

✔️ Readable and expressive

✅ Why does it work?

Because we’re switching on an enum and we’ve covered all known constants.

The compiler sees this as exhaustive and silently adds an implicit default.

💡 Tip: Stick to the lambda (->) style when possible — it’s cleaner and less error-prone.

🍷 Bonus: Using French dishes as your domain model might make your code more delicious to read 😄

#Java #CodingTips #SwitchExpression #Java17 #Enum #CleanCode #FrenchFood #DeveloperLife

QUESTION 4️⃣

How would you answer this question:

Given:

What will be printed?

A) [ratatouille, bouillabaisse, coq_au_vin]

B) [ratatouille, bouillabaisse, carbonade_flamande]

C) [ratatouille, bouillabaisse, coq_au_vin, carbonade_flamande]

D) An exception is thrown at runtime.

E) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

The correct answer is:

D) An exception is thrown at runtime.

Explanation:

You're working with a Java ArrayList and performing the following operations:

var frenchPlats = new ArrayList();

- This creates an ArrayList with raw type, which isn't ideal, but compiles.

frenchPlats.add("ratatouille");

frenchPlats.add("bouillabaisse");

frenchPlats.add("coq_au_vin");

- After these add calls, the list has 3 elements:

[ratatouille, bouillabaisse, coq_au_vin]

frenchPlats.set(3, "carbonade_flamande");

- This line tries to replace the element at index 3, but indexing is zero-based, and the current valid indices are 0, 1, and 2.

- Since there is no element at index 3, this throws an exception:

java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Thus, a runtime exception is thrown, and nothing is printed.

✅ Correct Answer: D

(Using Object-Oriented Concepts in Java)

LESSON 3️⃣

🎬 Java Record Classes Explained with French Cinema Icons! 🇫🇷 | Java Tutorial

Welcome to this fun and practical Java tutorial where we dive into the world of Java record classes — using examples inspired by famous French actors like Marion Cotillard, Omar Sy, and Jean Dujardin! 🇫🇷✨

In this video, you'll learn: ✅ What Java record classes are

✅ Why they’re perfect for modeling simple data objects

✅ How they reduce boilerplate code compared to traditional classes

✅ What Java generates automatically for you (constructor, equals, toString, etc.)

✅ Real-world examples using iconic French cinema figures 🎭

Perfect for beginners and intermediate Java developers looking to write cleaner, more expressive code!

📚 Based on JEP 395 – Records

🎥 Code examples with a cinematic twist!

👉 Don’t forget to like, subscribe, and comment if you enjoyed this tutorial and want more themed Java lessons!

#Java #RecordClass #JavaTutorial #FrenchActors #LearnJava #CodingMadeFun #JEP395