Xlinting default constructors – Objects, Immutability, Switch Expressions, and Pattern Matching

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 […]

Revealing a common mistake with Strings 2 – Objects, Immutability, Switch Expressions, and Pattern Matching

WARNING 1! NPE when calling an instance method via a null object Consider the following code written by a client of ChainSaw: ChainSaw cs = ChainSaw.initChainSaw(“QW-T650”);         cs.start(); // ‘cs’ is null The client passes a chainsaw model that is not supported by this class, so the initChainSaw() method returns null. This is really bad because […]

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 […]

Rewriting equals() via type patterns for instanceof – Objects, Immutability, Switch Expressions, and Pattern Matching

55. Rewriting equals() via type patterns for instanceof It is not mandatory to rely on instanceof for implementing the equals() method, but it is a convenient approach to write something as follows: public class MyPoint {  private final int x;  private final int y;  private final int z;  public MyPoint(int x, int y, int z) […]

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 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 – 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 […]

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 […]

© 2024 nickshade Please read our Terms and Privacy Policy. We also use Cookies.