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: Revealing a common mistake with Strings
Revealing a common mistake with Strings – Objects, Immutability, Switch Expressions, and Pattern Matching
46. Revealing a common mistake with Strings Everybody knows that String is an immutable class. In Java Coding Problems, First Edition, Chapter 2, Problem 48, we have even dissected how this class works and what are the consequences of its immutability.Even so, we are still prone to accidentally write code that ignores the fact that […]
Introducing type patterns matching for instanceof – Objects, Immutability, Switch Expressions, and Pattern Matching
53. Introducing type patterns matching for instanceof Can you name the shortcomings of the following classical snippet of code (this is a simple code used to save on a USB device different kinds of artifacts)? public static String save(Object o) throws IOException { if (o instanceof File) { File file = (File) o; return “Saving […]
Tackling type patterns for instanceof and streams – Objects, Immutability, Switch Expressions, and Pattern Matching
57. Tackling type patterns for instanceof and streams Let’s consider a List<Engine> where Engine is an interface implemented by several classes such as HypersonicEngine, HighSpeedEngine, and RegularEngine. Our goal is to filter this List and eliminate all RegularEngine that are electric and cannot pass our autonomy test. So, we can write a code as follows: […]
Dealing with pattern labels dominance in switch 2 – Objects, Immutability, Switch Expressions, and Pattern Matching
The order of pattern labels is imposed by the class hierarchy and is quite strict, but we can do some changes without creating any dominance issues. For instance, since Extra and Large are subclasses of Medium, we can switch their positions. Some things apply to Jumbo and Huge since they are both subclasses of Medium […]
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 […]
Dealing with completeness (type coverage) in pattern labels for switch 3 – Objects, Immutability, Switch Expressions, and Pattern Matching
This code compiles! There is no default label and no total pattern but the switch expression covers all possible values. How so?! This interface is declared as sealed via the sealed modifier: public sealed interface ClassDescextends ConstantDesc, TypeDescriptor.OfField<ClassDesc> Sealed interfaces/classes were introduced in JDK 17 (JEP 409) and we will cover this topic in chapter […]
Defining a day period – Working with Date and Time
63. Defining a day period Let’s imagine that we want to say hello to a friend from another country (different time zone) via a message such as Good morning, Good afternoon, and so on based on his local time. So, having access to AM/PM flags is not enough, because we consider that a day (24 […]
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 […]
Checking leap year – Working with Date and Time
67. Checking leap year This problem become easy as long as we know what is a leap year. In a nutshell, a leap year is any year divisible by 4 (so, year % 4 == 0) that it is not a century (for instance, 100, 200, …, n00). However, if the year represents a century […]