Adapter Pattern help in changing the third party integration without making much changes to the Core code.

Why to use Adapter Pattern?

  1. It Allows Incompatible interfaces between classes to work together without modifying their source code.
  2. It acts as a bridge between two interfaces, making them compatible so that they can collaborate and interact seamlessly.
  3. This pattern is useful when integrating existing or third-party code into an application without modifying the existing codebase.

When to use Adapter Pattern?
You can use the Adapter design pattern when you have to deal with different interfaces with similar behavior (which usually means classes with similar behavior but with different methods).

How to implement Adapter Pattern?
In your code instead of hard coding the method calls of each vendor (or Adaptee), you could then create a generic interface to wrap these similar behaviors and work with only one type of object. The Adapters will then implement the Target interface delegating its method calls to the Adaptees that are passed to the Adapters via constructor.

Code Example:

  1. Below we have PhonePay implementing BankAPI methods
  2. If Phonepay directly tries to use the classes of hdfcAPI or Any other bank api the code would become tightly coupled and would become difficult to change
  3. To get rid of tight coupling what we do in adaptor is we override the methods in the BankAPI and in method definition we call respective banks which the phone pay application has tie ups
  4. In case there is a need tomorrow for the phonepay application to change the Bank Gateway the only place to change is where the Adapter is called.
  5. The transferAmount of the bank API interface is overridden in the corresponding adapter classes and the definition is changed based on the way the corresponding way Bank APIs are getting called

BankAPI.java

public interface BankAPI {
    public void transferAmount(String fromAcc, String toAcc, String amount);
}

HDFCAdapter.java

public class HDFCAdapter implements BankAPI {
    HDFCBankAPI objHDFCBankAPI = new HDFCBankAPI();

    @Override
    public void transferAmount(String fromAcc, String toAcc, String amount) {
        objHDFCBankAPI.performTransaction(fromAcc, toAcc, Float.valueOf(amount));
    }
}

HDFCBankAPI.java

public class HDFCBankAPI {
    public void performTransaction(String creditorAcc, String debitorAcc, Float Amount){
        System.out.println("Transfering Funds");
    }
}

YesAdapter.java

public class YesAdapter implements BankAPI {
    YesBankAPI objYesBankAPI = new YesBankAPI();

    @Override
    public void transferAmount(String fromAcc, String toAcc, String amount) {
        objYesBankAPI.sendMoney(fromAcc, toAcc);
    }
}

YesBankAPI.java

public class YesBankAPI {
    public boolean sendMoney(String creditAcc, String debitAcc){
        System.out.println("Transaction Steps.....");
        return true;
    }
}

SBIAdapter.java

public class SBIAdapter implements BankAPI {
    SBIBankAPI objSBIBankAPI = new SBIBankAPI();

    @Override
    public void transferAmount(String fromAcc, String toAcc, String amount) {
        objSBIBankAPI.transfer(fromAcc, toAcc, amount);
    }
}

SBIBankAPI.java

public class SBIBankAPI {
    public String transfer(String account1, String account2, String Amount){
        System.out.println("Transaction from Account1 to Account2");
        return "Transaction Successful";
    }
}

PhonePay.java

public class PhonePay {
    public static void main(String[] args) {
        //Incase you want to change the Bank Gateway tomorrow you can change the Adapter Alone
        BankAPI objBankAPI = new YesAdapter();
        objBankAPI.transferAmount("AccNo 154264", "AccNo 4264755", "452");
    }
}

What is prototype Pattern?

The Prototype pattern is a creation pattern based on cloning a pre-configured object. The idea is that you pick an object that is configured for either the default or in the ballpark of some specific use case and then you clone this object and configure to your exact needs.

When to use prototype Pattern?

  1. Prototype patterns are required when object creation is a time-consuming, and costly operation, so we create objects with the existing object itself.
  2. The Prototype Design Pattern allows you to create new objects by cloning existing ones without being bound to the specific classes of the objects being cloned. This promotes decoupling between the client code and the concrete classes, making it easier to introduce new types of objects without modifying the client code.

Where to use prototype Pattern?

  1. If you want to create an object but do not want to go through the expensive object creation procedure where network or database calls are made, then use the prototype pattern. Just create a copy of the object and do your changes on it.

How prototype Pattern is acheived?

  1. Create a Interface with clone method
  2. Create a Concrete prototypes which implements the interface. In the below example it is PersonPrototype. This Concrete class would be DB classes which consume lot of resource and the one which you dont want to create again and again
  3. Classes use the above Concrete prototype to add their own attributes and create new classes EmployeePrototype, StudentPrototype, SoftwareEmployee(Concrete Class)
  4. Prototypes are added to Registry which is nothing but an HashMap to Store String and Prototype classes
  5. Concrete classes which want to extend the Prototype uses the above hashmap registry to fetch the protype and add their custom attributes.

There are two things to note in code

  • One is Clone method which makes the extending class mandatory to implement clone method. The same can be done by implementing Clone interface
  • Second thing is how the Concrete Classes implements the clone and creates copy of itself at the same time calling the parent prototype class
    .
    .
        public StudentPrototype(StudentPrototype studentPrototype) {
            super(studentPrototype);
            this.institution = studentPrototype.institution;
            this.rollNo = studentPrototype.rollNo;
        }
    
    .
    
  • PersonPrototype is Concrete Prototype which is costly to create
  • We add Custom attributes at each level and create prototype and concrete class from PersonPrototype

Prototype.java

public interface Prototype<T> {
    T clone();
}

ConcretePersonPrototype.java

public class ConcretePersonPrototype implements Prototype<ConcretePersonPrototype>{
    private String name = "Mugilvannan G";
    private String aadhar = "1452-5874-5124-847";

    public ConcretePersonPrototype() {
    }

    @Override
    public ConcretePersonPrototype clone() {
        return new ConcretePersonPrototype(this);
    }

    public ConcretePersonPrototype(ConcretePersonPrototype concretePersonPrototype) {
        this.name = concretePersonPrototype.name;
        this.aadhar = concretePersonPrototype.aadhar;
    }

    public String getName() {
        return name;
    }

    public String getAadhar() {
        return aadhar;
    }
}

EmployeePrototype.java

public class EmployeePrototype extends ConcretePersonPrototype {
    private Integer pfAccountNo;
    private String startDate;
    private String endDate;

    public EmployeePrototype() {
    }

    public EmployeePrototype(EmployeePrototype employeePrototype) {
        super(employeePrototype);
        this.pfAccountNo = employeePrototype.pfAccountNo;
    }

    public EmployeePrototype clone() {
        return new EmployeePrototype(this);
    }

    public Integer getPfAccountNo() {
        return pfAccountNo;
    }

    public void setPfAccountNo(Integer pfAccountNo) {
        this.pfAccountNo = pfAccountNo;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        return "Career Details : {" + '\'' +
                " Employee Name ='" + super.getName() + '\'' +
                " Employee Aadhar ='" + super.getAadhar() + '\'' +
                " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' +
                '}';
    }
}

StudentPrototype.java

public class StudentPrototypeConcrete extends ConcretePersonPrototype {
    private String institution;
    private  Integer rollNo;

    public StudentPrototypeConcrete() {
    }

    public StudentPrototypeConcrete(StudentPrototypeConcrete studentPrototype) {
        super(studentPrototype);
        this.institution = studentPrototype.institution;
        this.rollNo = studentPrototype.rollNo;
    }

    public Integer getRollNo() {
        return rollNo;
    }

    public void setRollNo(Integer rollNo) {
        this.rollNo = rollNo;
    }

    public String getInstitution() {
        return institution;
    }

    public void setInstitution(String institution) {
        this.institution = institution;
    }

    public StudentPrototypeConcrete clone(){
        return new StudentPrototypeConcrete(this);
    }

    @Override
    public String toString() {
        return "Education Details : {" +
                " Student Name='" + super.getName() + '\'' +
                " Student Aadhar='" + this.getAadhar() + '\'' +
                " Student Institution='" + this.getInstitution() + '\'' +
                " Student Rollno='" + this.getRollNo() + '\'' +
                '}';
    }
}

EmployeePrototype.java

public class EmployeePrototype extends PersonPrototype {
    private Integer pfAccountNo;
    private String startDate;
    private String endDate;

    public EmployeePrototype() {
    }

    public EmployeePrototype(EmployeePrototype employeePrototype) {
        super(employeePrototype);
        this.pfAccountNo = employeePrototype.pfAccountNo;
    }

    public EmployeePrototype clone() {
        return new EmployeePrototype(this);
    }

    public Integer getPfAccountNo() {
        return pfAccountNo;
    }

    public void setPfAccountNo(Integer pfAccountNo) {
        this.pfAccountNo = pfAccountNo;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        return "Career Details : {" + '\'' +
                " Employee Name ='" + super.getName() + '\'' +
                " Employee Aadhar ='" + super.getAadhar() + '\'' +
                " Employee pfAccountNo='" + this.getPfAccountNo() + '\'' +
                '}';
    }
}

SoftwareEmployee.java

public class SoftwareEmployee extends EmployeePrototype {
    public Integer empId;
    public String designation;
    public String practice;
    public String organisation;

    public SoftwareEmployee() {
    }

    public SoftwareEmployee(SoftwareEmployee softwareEmployee) {
        super(softwareEmployee);
        this.empId = softwareEmployee.empId;
        this.designation = softwareEmployee.designation;
        this.practice = softwareEmployee.practice;
        this.organisation = softwareEmployee.organisation;
    }

    public SoftwareEmployee clone(){
        return new SoftwareEmployee(this);
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getDesignation() {
        return designation;
    }

    public void setDesignation(String designation) {
        this.designation = designation;
    }

    public String getPractice() {
        return practice;
    }

    public void setPractice(String practice) {
        this.practice = practice;
    }

    public String getOrganisation() {
        return organisation;
    }

    public void setOrganisation(String organisation) {
        this.organisation = organisation;
    }

    @Override
    public String toString() {
        return "Present Work Details: {" +
                "Software Engineer Name ='" + super.getName() + '\'' +
                " Aadhar No ='" + super.getAadhar() + '\'' +
                " EmpId ='" + this.empId + '\'' +
                " Designation='" + this.designation + '\'' +
                " Organisation='" + this.organisation + '\'' +
                " Practice='" + this.practice + '\'' +
                '}';
    }
}

Client.java

public class Client {
    public static void fillRegistry(PersonRegistry registry) {
        StudentPrototype objStudPrototype = new StudentPrototype();
        objStudPrototype.setRollNo(15462);
        objStudPrototype.setInstitution("Mowbarys");
        registry.register("StudentPerson", objStudPrototype);

        EmployeePrototype objEmpPrototype = new EmployeePrototype();
        objEmpPrototype.setPfAccountNo(4151542);
        objEmpPrototype.setStartDate("2012");
        objEmpPrototype.setEndDate("NA");
        registry.register("EmployeePerson", objEmpPrototype);

        SoftwareEmployee objSoftwareEmployee = new SoftwareEmployee();
        objSoftwareEmployee.setPfAccountNo(4151542);
        registry.register("SoftwareEmployee", objSoftwareEmployee);
    }

    public static void main(String[] args) {
        PersonRegistry registry = new PersonRegistry();
        fillRegistry(registry);

        EmployeePrototype objEmployee = (EmployeePrototype) registry.get("EmployeePerson").clone();

        StudentPrototype objStudent = (StudentPrototype)registry.get("StudentPerson").clone();
        objStudent.setRollNo(15425);
        objStudent.setInstitution("Mowbarys");

        SoftwareEmployee objSoftEmployee1 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee1.setStartDate("2011");
        objSoftEmployee1.setEndDate("2012");
        objSoftEmployee1.setEmpId(1001);
        objSoftEmployee1.setOrganisation("Fuchsia Software");
        objSoftEmployee1.designation = "Programmer";
        objSoftEmployee1.practice = "Classic ASP, HTML, CSS";

        SoftwareEmployee objSoftEmployee2 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee2.setStartDate("2012");
        objSoftEmployee2.setEndDate("2015");
        objSoftEmployee2.setEmpId(754185);
        objSoftEmployee2.setOrganisation("Infosys");
        objSoftEmployee2.designation = "Technology Analyst";
        objSoftEmployee2.practice = "Java, Spring Boot";

        SoftwareEmployee objSoftEmployee3 = (SoftwareEmployee) registry.get("SoftwareEmployee").clone();
        objSoftEmployee3.setStartDate("2018");
        objSoftEmployee3.setEndDate("NA");
        objSoftEmployee3.setEmpId(152);
        objSoftEmployee3.setOrganisation("Cognizant");
        objSoftEmployee3.designation = "Technology Lead";
        objSoftEmployee3.practice = "Java, Devops";


        System.out.println(objStudent);
        System.out.println(objEmployee);
        System.out.println(objSoftEmployee1);
        System.out.println(objSoftEmployee2);
        System.out.println(objSoftEmployee3);
    }
}

Output

Education Details : { Student Name='Mugilvannan G' Student Aadhar='1452-5874-5124-847' Student Institution='Mowbarys' Student Rollno='15425'}
Career Details : {' Employee Name ='Mugilvannan G' Employee Aadhar ='1452-5874-5124-847' Employee pfAccountNo='4151542'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='1001' Designation='Programmer' Organisation='Fuchsia Software' Practice='Classic ASP, HTML, CSS'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='754185' Designation='Technology Analyst' Organisation='Infosys' Practice='Java, Spring Boot'}
Present Work Details: {Software Engineer Name ='Mugilvannan G' Aadhar No ='1452-5874-5124-847' EmpId ='152' Designation='Technology Lead' Organisation='Cognizant' Practice='Java, Devops'}

What is Telescoping Pattern
A telescoping constructor is a series of constructors where each one has a different number of parameters. Each constructor calls a more specific constructor in the hierarchy, providing a default value for the remaining parameters.

Disadvantage of Below Approach

  1. Constructors are hard to read when they have many parameters
  2. When adding new parameters, you have to add new constructors to the chain. This pattern can therefore not scale very well
  3. It becomes difficult to remember the required order of the parameters, especially when many of them are of the same type. Additionally, the code becomes less readable, and it’s not always clear what is being passed to the constructor. This is addressed by using Builder Pattern

Report

import java.util.Date;

public class Report {
    private String reportName;
    private Integer reportSize;
    private String  reportFormat;
    private Date reportDate;

    public Report() {
    }

    //Constructor1
    public Report(String reportName) {
        this.reportName = reportName;
    }

    public Report(String reportName, Integer reportSize) {
        this(reportName); //Call to before Constructor1
        this.reportSize = reportSize;
    }

    public Report(String reportName, Integer reportSize, String  reportFormat) {
        this(reportName, reportSize); //Call to before Constructor2
        this.reportFormat = reportFormat;
    }

    public Report(String reportName, Integer reportSize, String  reportFormat, Date reportDate) {
        this(reportName, reportSize, reportFormat); //Call to before Constructor3
        this.reportDate = reportDate;
    }
}

Why builder Pattern?
If there are many attributes in the class and object is created over several calls it may be in an inconsistent state partway through its construction. To address this builder pattern is introduced. If Object Creation should be delayed until all the validation of class attributes are performed builder pattern addresses the need.

If there are 2 attributes to a Class then we can create 2^N Constructor based on the two attributes. This Results in Boiler plate code. What if the number of attributes keeps increasing over a period of time.

Report.java

import java.util.InvalidPropertiesFormatException;

public class Report {
    private String reportName;
    private String reportFormat;

    private Report(){}

    public static ReportBuilder getReportBuilder(){
        return new ReportBuilder();
    }


    @Override
    public String toString() {
        return "Report{" +
                "reportName='" + reportName + '\'' +
                ", reportFormat='" + reportFormat + '\'' +
                '}';
    }

    public static class ReportBuilder {
        private String reportName;
        private String reportFormat;

        private ReportBuilder(){}

        public ReportBuilder setReportName(String reportName) {
            this.reportName = reportName;
            return this;
        }

        public ReportBuilder setReportFormat(String reportFormat) {
            this.reportFormat = reportFormat;
            return this;
        }

        public Report build() throws InvalidPropertiesFormatException {
            if(this.reportFormat == "CSV"){
                throw new InvalidPropertiesFormatException("Format Not Supported");
            }

            if(this.reportName.length() < 10){
                throw new InvalidPropertiesFormatException("Name should be greater than 10");
            }

            //Object for Report should not be created before validation is completed as above
            Report objReport = new Report();
            objReport.reportName = this.reportName;
            objReport.reportFormat = this.reportFormat;
            return objReport;
        }
    }
}

BuilderPatternClient.java

import java.util.InvalidPropertiesFormatException;

public class BuilderPatternClient {
    public static void main(String[] args) throws InvalidPropertiesFormatException {
        Report objReport = Report.getReportBuilder()
                .setReportFormat("PDF")
                .setReportName("Monthly Transactions")
                .build();

        System.out.println(objReport);
    }
}

Output

Report{reportName='Monthly Transactions', reportFormat='PDF'}

What is Class Loading?
Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.

When Class is Loaded?

  1. Class loading done by Class Loaders may eagerly load a class or does lazy load
  2. Eager loading happens as soon as another class references it and lazy load happens when a need of class initialization occurs.
  3. If a Class is loaded before it’s actually being used, it can sit inside before being initialized which may vary from JVM to JVM.
  4. However, it’s guaranteed by JLS that a class will be loaded when there is a need of static initialization.

Whenever the JVM determines it needs a class (to use its static variables, to create a new object, to use its static methods etc) it will load the class and static initialization blocks will run, static variables are initialized etc. This is (at least under normal circumstances) done only once

SomeClass.someStaticMethod(); //SomeClass is loaded (if its not already)
SomeClass.someStaticVariable; //SomeClass is loaded (if its not already)
SomeClass var=new SomeClass(); //SomeClass is BOTH loaded (if its not already) AND instantiated

Class loader actually loads byte code into JVM , runs static initializers when you want to use static fields within class without creating an instance of it, class must be loaded by class loader first.Default classloader in java is java.lang.ClassLoader

Class Loading is mostly unpredictable

When Class is Initialized?
Class Initialization happens after class loading. A class is initialized in Java when

  1. An Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.
  2. an static method of Class is invoked.
  3. an static field of Class is assigned.
  4. an static field of class is used which is not a constant variable.
  5. if Class is a top level class and an assert statement lexically nested within class is

Which classes are Eagerly Loaded and Lazily Loaded

Eagerly Loaded Classes
are those the JVM must be able to load JVM class files. The JVM class loader loads referenced JVM classes that have not already been linked to the runtime system. Classes are loaded implicitly because: The initial class file – the class file containing the public static void main(String args[]) method – must be loaded at startup. Depending on the class policy adopted by the JVM, classes referenced by this initial class can be loaded in either a lazy or eager manner.

An eager class loader loads all the classes comprising the application code at startup. Lazy class loaders wait until the first active use of a class before loading and linking its class file.

The first active use of a class occurs when one of the following occurs: • An instance of that class is created • An instance of one of its subclasses is initialized • One of its static fields is initialized. Certain classes, such as ClassNotFoundException, are loaded implicitly by the JVM to support execution. You may also load classes explicitly using the java.lang.Class.forName() method in the Java API, or through the creation of a user class loader.

Based on the above rules, lazy class loading can be forced on the system.

Use java -XX:+TraceClassLoading to trace the loading of classes.

Use java -XX:+PrintCompilation to trace when methods are JITed.

Thread – direction or path that is taken while a program is executed

  1. Below code would explain how dirty read happens when multiple thread(2 threads) tries to access Instance variable at once from two different class objects
  2. Below we have 3 classes, One for Printing Report and Other for removing the report printed. Third for tracking the report status
  3. The Output of the code would be Consistently 0 every time which is expected when the number of reports to be printed is in range of less than 100. However the output changes with more the no of reports to be printed. I.E. totalReportsToBePrinted = 10000000
  4. This happens because for lower value of reports to be printed the thread executes fast with out context switching however for higher values other thread(RemovePrintedReports thread takes control before PrintExcelReports get completed) takes control which leads to inconsistency
  5. The Same code would return 0 every time if one thread(PrintExcelReports) completes before other(RemovePrintedReports) as below when we use join and didn’t start both the threads at once.
    .
    .
            t1.start();
            t1.join();
            
            t2.start();
            t2.join();
    
    .
    .
    

PrintExcelReports.java

public class PrintExcelReports implements Runnable {
    TotalReportCount totalReportCount;

    public PrintExcelReports(TotalReportCount totalReportCount) {
        this.totalReportCount = totalReportCount;
    }

    int totalReportsToBePrinted = 1000000;

    @Override
    public void run() {
        for(int i=0;i<totalReportsToBePrinted;i++){
            totalReportCount.totalReportsCntVal -= i;
        }
    }
}

RemovePrintedReports.java

public class RemovePrintedReports implements Runnable {
    TotalReportCount totalReportCount;

    public RemovePrintedReports(TotalReportCount totalReportCount) {
        this.totalReportCount = totalReportCount;
    }

    int totalReportsToBePrinted = 1000000;

    @Override
    public void run() {
        for(int i=0;i<totalReportsToBePrinted;i++){
            totalReportCount.totalReportsCntVal -= i;
        }
    }
}

ReportCurrentStatus.java

public class ReportCurrentStatus {
    public static void main(String[] args) throws InterruptedException {
        TotalReportCount objTotalReportCount = new TotalReportCount();
        objTotalReportCount.totalReportsCntVal = 0;

        PrintExcelReports objPrinter1 = new PrintExcelReports(objTotalReportCount);
        RemovePrintedReports objPrinter2 = new RemovePrintedReports(objTotalReportCount);

        Thread t1 = new Thread(objPrinter1);
        Thread t2 = new Thread(objPrinter2);

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(objTotalReportCount.totalReportsCntVal);
    }
}

Output when totalReportsToBePrinted is greater than 1000

RANDOM NUMBER

Output when totalReportsToBePrinted is less than 100

0

Why we need Custom Exception
Exceptions are technical causes which should be wrapped in a presentable way with a specific business logic and workflow. adding a higher abstraction layer for the exception handling, which results in more meaningful and readable API

How to write Custom Exception
All you need to do is create a new class and have it extend Exception. If you want an Exception that is unchecked, you need to extend RuntimeException.
Note: A checked Exception is one that requires you to either surround the Exception in a try/catch block or have a ‘throws’ clause on the method declaration. (like IOException) Unchecked Exceptions may be thrown just like checked Exceptions, but you aren’t required to explicitly handle them in any way (IndexOutOfBoundsException).

Exception, if you want your exception to be checked (i.e: required in a throws clause).RuntimeException, if you want your exception to be unchecked.

Creating Custom Checked Exception
Note:constructor takes a Throwable’s subclass which is the origin (cause) of the current exception

public class StudentStoreException extends Exception {
     public StudentStoreException(String message, Throwable cause) {
        super(message, cause);
    }
}

Creating Custom Unchecked Exception

public class IncorrectFileExtensionException 
  extends RuntimeException {
    public IncorrectFileExtensionException(String errorMessage, Throwable err) {
        super(errorMessage, err);
    }
}

Methods in Custom Exception

public class MyOwnException extends Exception {
    public MyOwnException() {

    }

    public MyOwnException(String message) {
        super(message);
    }

    public MyOwnException(Throwable cause) {
        super(cause);
    }

    public MyOwnException(String message, Throwable cause) {
        super(message, cause);
    }
}

Minimal Requirment

  1. Java 17 or higher
  2. Jakarta EE 10
  3. Spring Framework 6
  4. Works on Maven 3.5+
  5. Tomcat 10.0
  6. Improved observability with Micrometer and Micrometer Tracing

Improvements
Performance enhancements and optimizations to boost application responsiveness and efficiency.These improvements focus on reducing startup times, minimizing memory footprint, and optimizing resource utilization.

Changes in Code

  1. When we wanted to configure the Security settings, we had to extend the WebSecurityConfigurerAdapter class.This class has been deprecated and removed in Spring Security 6.
    Instead, we should now take a more component-based approach and create a bean of type SecurityFilterChain.

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig {
     @Bean
      public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(requests -> requests
                .requestMatchers(new AntPathRequestMatcher("/openapi/openapi.yml")).permitAll()
                .anyRequest().authenticated())
            .httpBasic();
        return http.build();
      }
    }
    
  2. Instead of using authorizeRequests, which has been deprecated, we should now use authorizeHttpRequests.This method is part of the HttpSecurity configuration and allows you to configure fine-grained request matching for access control.
  3. Spring Security 6, AntMatcher, MvcMatcher, and RegexMatcher have been depreciated and replaced by requestMatchers or securityMatchers for path-based access control. This allows us to match requests based on patterns or other criteria without relying on specific matchers.
  1. New methods to the String class: isBlank, lines, strip, stripLeading, stripTrailing, and repeat.
  2. readString and writeString static methods from the Files class
    Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
    String fileContent = Files.readString(filePath);
    assertThat(fileContent).isEqualTo("Sample text");
    
  3. java.util.Collection interface contains a new default toArray method
    List sampleList = Arrays.asList("Java", "Kotlin");
    String[] sampleArray = sampleList.toArray(String[]::new);
    assertThat(sampleArray).containsExactly("Java", "Kotlin");
    
  4. A static not method has been added to the Predicate interface. Predicate.not(String::isBlank)
  5. The new HTTP client from the java.net.http package was introduced in Java 9. It has now become a standard feature in Java 11.The new HTTP API improves overall performance and provides support for both HTTP/1.1 and HTTP/2:
    HttpClient httpClient = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .connectTimeout(Duration.ofSeconds(20))
        .build();
    HttpRequest httpRequest = HttpRequest.newBuilder()
        .GET()
        .uri(URI.create("http://localhost:" + port))
        .build();
    HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
    assertThat(httpResponse.body()).isEqualTo("Hello from the server!");
    
  6. A major change in this version is that we don’t need to compile the Java source files with javac explicitly anymore:
    Before Java 11Version

    $ javac HelloWorld.java
    $ java HelloWorld 
    Hello Java 8!
    

    New Version

    $ java HelloWorld.java
    Hello Java 11!
    
  1. forEach method in java.lang.Iterable interface

    Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic. The forEach method takes java.util.function.Consumer object as an argument, so it helps in having our business logic at a separate location that we can reuse.

  2. myList.forEach(new Consumer < Integer > () {
        public void accept(Integer t) {
            System.out.println("forEach anonymous class Value::" + t);
        }
    
    });
    
    //traversing with Consumer interface implementation
    MyConsumer action = new MyConsumer();
    myList.forEach(action);
    

    Consumer implementation that can be reused

    class MyConsumer implements Consumer<Integer>{
    
    	public void accept(Integer t) {
    		System.out.println("Consumer impl Value::"+t);
    	}
    }
    
  3. Interfaces with default and static methods

    From Java 8, interfaces are enhanced to have a method with implementation. We can use default and static keyword to create interfaces with method implementation

  4. We know that Java doesn’t provide multiple inheritance in Classes because it leads to Diamond Problem. So how it will be handled with interfaces now since interfaces are now similar to abstract classes. Compiler will throw an exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.

    @FunctionalInterface
    public interface Interface1 {
    
        void method1(String str);
    
        default void log(String str) {
            System.out.println("I1 logging::" + str);
        }
    
        static void print(String str) {
            System.out.println("Printing " + str);
        }
    }    
    

    interfaces are not allowed to have Object default methods.

  5. @FunctionalInterface annotation. Functional interfaces are a new concept introduced in Java 8. An interface with exactly one abstract method becomes a Functional Interface. We don’t need to use @FunctionalInterface annotation to mark an interface as a Functional Interface.

    @FunctionalInterface annotation is a facility to avoid the accidental addition of abstract methods in the functional interfaces. You can think of it like @Override annotation and it’s best practice to use it. java.lang.Runnable with a single abstract method run() is a great example of a functional interface.

    Anonymous Class Implementation

    Runnable r = new Runnable() {
        @Override
        public void run() {
            System.out.println("My Runnable");
        }
    };
    

    Lambda Expression Implementation

    Runnable r1 = () - > {
        System.out.println("My Runnable");
    };
    

    If you have single statement in method implementation anonymous class can be instantiated using lambda expression as below
    lambda expressions Implementation

    Interface1 i1 = (s) -> System.out.println(s);		
    i1.method1("abc");
    
  6. New java.util.stream has been added in Java 8 to perform filter/map/reduce like operations with the collection. Stream API will allow sequential as well as parallel execution.
    public static void main(String[] args) {
    
        List < Integer > myList = new ArrayList < > ();
        for (int i = 0; i < 100; i++) myList.add(i);
    
        //sequential stream
        Stream < Integer > sequentialStream = myList.stream();
    
        //parallel stream
        Stream < Integer > parallelStream = myList.parallelStream();
    
        //using lambda with Stream API, filter example
        Stream < Integer > highNums = parallelStream.filter(p - > p > 90);
        //using lambda in forEach
        highNums.forEach(p - > System.out.println("High Nums parallel=" + p));
    
        Stream < Integer > highNumsSeq = sequentialStream.filter(p - > p > 90);
        highNumsSeq.forEach(p - > System.out.println("High Nums sequential=" + p));
    
    }
    

    parallel processing values are not in order, so parallel processing will be very helpful while working with huge collections.

  7. IO improvements known to me are:

    Files.list(Path dir) that returns a lazily populated Stream, the elements of which are the entries in the directory.
    Files.lines(Path path) that reads all lines from a file as a Stream.
    Files.find() that returns a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.
    BufferedReader.lines() that return a Stream, the elements of which are lines read from this BufferedReader.

  8. Java Time API packages, I can sense that they will be very easy to use. It has some sub-packages java.time.format that provides classes to print and parse dates and times and java.time.zone provides support for time zones and their rules.