43. Xlinting default constructors We know that a Java class with no explicit constructor gets automatically an “invisible” default constructor for setting default values of the instance variables. The following House class falls in this scenario: public class House { private String location; private float price; …} If this is exactly what we wanted then […]
Category: Xlinting default constructors
Using yield in switch expressions – Objects, Immutability, Switch Expressions, and Pattern Matching
48. Using yield in switch expressions In Java Coding Problems, First Edition, Chapter 2, Problems 55-57 we covered in detail the new switch expressions introduced in JDK 12. It will not be fair to repeat here the content of those problems, so we continue from where we left off with the switch evolution in JDK […]
Handling the scope of a binding variable in type patterns for instanceof – Objects, Immutability, Switch Expressions, and Pattern Matching
54. Handling the scope of a binding variable in type patterns for instanceof From Problem 52, we know the headlines of scoping the binding variables in pattern matching. Moreover, we know from the previous problem that in type pattern for instanceof we have a single binding variable. It is time to see some practical examples, […]
Adding guarded pattern labels in switch – Objects, Immutability, Switch Expressions, and Pattern Matching
59. Adding guarded pattern labels in switch Do you remember that type patterns for instanceof can be refined with extra boolean checks applied to the binding variables to obtain fine-grained use cases? Well, we can do the same for the switch expressions that use pattern labels. The result is named guarded pattern labels. Let’s consider […]
Dealing with pattern labels dominance in switch – Objects, Immutability, Switch Expressions, and Pattern Matching
60. Dealing with pattern labels dominance in switch The compiler matches the selector expression against the available pattern labels by testing the selector expression against each label starting from top to bottom (or, from the first to the last) in the exact order that we wrote them in the switch block. This means that the […]
Dealing with completeness (type coverage) in pattern labels for switch 2 – Objects, Immutability, Switch Expressions, and Pattern Matching
Now, the selector expression can be any type which means that the total pattern Vehicle v is not total anymore. While Vehicle v becomes an optional ordinary pattern, the new total pattern is case Object obj. This means that we can cover all possible values by adding the default label or the case Object obj […]
Understanding the unconditional patterns and nulls in switch expressions – Objects, Immutability, Switch Expressions, and Pattern Matching
62. Understanding the unconditional patterns and nulls in switch expressions Let’s imagine that we use JDK 17 and we have the following code: private static String drive(Vehicle v) { return switch (v) { case Truck truck -> “truck: ” + truck; case Van van -> “van: ” + van; case Vehicle vehicle -> “vehicle: ” […]
Converting between Date and YearMonth – Working with Date and Time
64. Converting between Date and YearMonth Converting a java.util.Date to JDK 8 java.time.YearMonth can be done based on YearMonth.from(TemporalAccessor temporal). A TemporalAccessor is an interface (more precisely, a framework-level interface) that exposes read-only access to any temporal object including date, time, and offset (a combination of these is also allowed). So, if we convert the […]
Getting the first and last day of a quarter – Working with Date and Time
69. Getting the first and last day of a quarter Let’s assume that we represent the first and last day of a quarter via this simple class: public final class Quarter { private final Date firstDay; private final Date lastDay; …} Next, we have a java.util.Date and we want the first and the last day […]
Computing pregnancy due date – Working with Date and Time
71. Computing pregnancy due date I’m not an expert in the field but let’s start with these two constants: public static final int PREGNANCY_WEEKS = 40;public static final int PREGNANCY_DAYS = PREGNANCY_WEEKS * 7; Let’s consider the first day as a LocalDate and we want to write a calculator that prints the pregnancy due date, […]