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: Computing pregnancy due date
Implementing an immutable stack – Objects, Immutability, Switch Expressions, and Pattern Matching
45. Implementing an immutable stack A common coding challenge in interviews sounds like this: implement an immutable stack in Java. In case you need a quick overview of immutability concepts and the principles of writing an immutable class then I strongly recommend you Java Coding Problems, First Edition, Problems 47-52.Being an abstract data type, a […]
Taking on the hard way to discover equals() – Objects, Immutability, Switch Expressions, and Pattern Matching
50. Taking on the hard way to discover equals() Check out the following code: Integer x1 = 14; Integer y1 = 14;Integer x2 = 129; Integer y2 = 129;List<Integer> listOfInt1 = new ArrayList<>( Arrays.asList(x1, y1, x2, y2));listOfInt1.removeIf(t -> t == x1 || t == x2);List<Integer> listOfInt2 = new ArrayList<>( Arrays.asList(x1, y1, x2, y2));listOfInt2.removeIf(t -> t.equals(x1) || […]
Introducing pattern matching – Objects, Immutability, Switch Expressions, and Pattern Matching
52. Introducing pattern matching JDK 16 has introduced one of the major and complex features of Java referred to as pattern matching. The future is wide open for this topic.In a nutshell, pattern matching defines a synthetic expression for checking/testing whether a given variable has certain properties. If those properties are met then automatically extract […]
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 […]
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, […]
Dealing with completeness (type coverage) in pattern labels for switch – Objects, Immutability, Switch Expressions, and Pattern Matching
61. Dealing with completeness (type coverage) in pattern labels for switch In a nutshell, switch expressions and switch statements that use null and/or pattern labels should be exhaustive. In other words, we must cover with explicit switch case labels all the possible values. Let’s consider the following example: class Vehicle {}class Car extends Vehicle {}class […]
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 […]
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: ” […]
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 […]