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 […]
Author: nickshade
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 […]
Revealing a common mistake with Strings 3 – Objects, Immutability, Switch Expressions, and Pattern Matching
WARNING 4! NPE when accessing index value of null array/collection Consider the following code written by a client of ChainSaw: ChainSaw myChainSaw = ChainSaw.initChainSaw(“QWE-T800”);ChainSaw[] friendsChainSaw = new ChainSaw[]{ ChainSaw.initChainSaw(“Q22-T450”), ChainSaw.initChainSaw(“QRT-T300”), ChainSaw.initChainSaw(“Q-T900”), null, // ops! ChainSaw.initChainSaw(“QMM-T850”), // model is not supported ChainSaw.initChainSaw(“ASR-T900”)};int score = myChainSaw.performance(friendsChainSaw); Creating an array of ChainSaw was quite challenging in this example. […]
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 […]
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 […]
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 […]
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, […]