Using “record”

Java 14’s introduction of the record type has been a game-changer since it’s reducing boilerplate code to enhancing data immutability, records are a valuable addition to Java’s toolkit.

The example below demonstrates how record might replce the Lombok library.

class JEP_359_Records {
    public static void main(String...args) {
        System.out.println("Hello World");

        var emp1 = new Employee("John", "Washington");
        var emp2 = new Employee("John", "Washington");

        //emp1.firstName = "Alex"; // ERROR: the properties are implicitly final.
        // emp1.firstName("Alex"); // ERROR: the properties are implicitly final.
    
        System.out.println(emp1); // toString(), output: Employee[firstName=John, lastName=Washington]
        System.out.println(emp1.hashCode()); // hashCode()
        System.out.println(emp2.hashCode()); // hashCode()
        System.out.println(emp1.equals(emp2)); // equals(), output: true

        var emp3 = new Employee(null, null);
        System.out.println(emp3); // custom constructor sets Unknown

        // we can create a custom static method building the class
        var emp4 = Employee.unnamed("Washington");
        System.out.println(emp4);
    }

    public record Employee(String firstName, String lastName) {
        // firstName, lastName are final (immutable)
        // Automatically generated constractor replaces @AllArgsConstructor
        
        // It's not possible for some values to be mutable and some not
        
        // Overriden constructors are not allowed
        // Custom constructor is:
        public Employee {
            if (firstName == null) {
                firstName = "Unknown";
            }
            if (lastName == null) {
                lastName = "Unknown";
            }
        }

        // Custom static method
        public static Employee unnamed(String lastName) {
            return new Employee("Unnamed", lastName);
        }
    }
}

Link to Bitbucket

Continue Reading Using “record”

Java SE 17 OCP Certification Resources

Certification from Oracle – OCP Java SE 17
I recently started preparing for this exam. Thanks to the following resources, I feel progress and gain a lot of knowledge.

Here is my list for the resources for preparation:

Continue Reading Java SE 17 OCP Certification Resources

Pattern matching beyond the statement

The pattern matching in Java 17 reduces the boilerplate code by removing the convertion step.

What interesting is the extended scope of pattern created variable. The function whatIsIt1 has a if block, but the airplane and ship variables are extended out of if scope.

import java.time.LocalDate;

public class App {
  public static void main(String[] args) {
      var app = new App();
      var t1 = new Titanic();
      var b1 = new Boing();

      app.whatIsIt(t1);
      app.whatIsIt(b1);
      app.whatIsIt1(t1);
      app.whatIsIt1(b1);
  }

    public void whatIsIt(Object obj) {
      if ( obj instanceof Airplane airplane )
        System.out.println(airplane.name);
      else if (obj instanceof Ship ship ) {
        System.out.println(ship.name);
      }
      else 
        System.out.println("I Don't Know");
    }

    public void whatIsIt1(Object obj) {
      if ( !(obj instanceof Airplane airplane)) {
        if ( !(obj instanceof Ship ship )) {
          System.out.println("I Don't Know");
          return;
        }

        System.out.println(ship.name);
        return; 
      }
      
      System.out.println(airplane.name);
    }

    static class Airplane {
      public String name = "Airplane";
    }

    static class Ship {
      public String name = "Ship";
    }

    static class Boing extends Airplane {

    }

    static class Titanic extends Ship {

    }
}
Continue Reading Pattern matching beyond the statement

All cases of the switch will be printed

Strange, but in Java that’s how switch works.

In the following program, once numOfballs falls in the 1st case, all the rst cases will be visited regardless the condition.


public class App {
    public static void main(String[] args) {

      var numOfBalls = 1;
      switch (numOfBalls) {
          case 1:  System.out.println("One ball");
          case 2:  System.out.println("Two balls");
          case 3:  System.out.println("Three balls");
          case 4:  System.out.println("Four balls");
          case 5:  System.out.println("Five balls");
      } 
    }
}

Continue Reading All cases of the switch will be printed

Effective Java practice is starting

Effective Java, the book of Joshua Bloch, is a classic of Java literature. As the book’s preface put it:

If you have ever studied a second language yourself and then tried to use i toutside the class-room, you kjnow that there are three things you must master: how the lanaguage is structured (grammar), how to name things you want to talk about (vocabulary), and the customary and effective ways to say everyday things (usage)…
It is much the same with a programming language. You need to understand the core language: is it algorithmic, functional, object-oriented? You need to know the vocabulary: what data structures, operations, and facilities are provided by the standard libraries? And you need to be familiar with the customary and effective ways to structure your code…
This book addresses your third need: Customeary and effective usage.

I believe every programmer should go through the best books every few years and refine its techniques and best-practice.

In this thread, I will cover the practical examples and practices from the items of the book.

Continue Reading Effective Java practice is starting