Java is nowadays one of the worldwide spreadest programming languages with its Java SE 8 released on March 18, 2014 and that counts about 60% of usage in the main applications.

Java 14, and then Java 15, have been released respectively on March 17 and September 15 2020 increasing features for developers.

Let’s have an overview of the main JEP (Java Enhancement Proposal) for each release.

Java 14: Pattern Matching for instanceof(JEP 305), Helpful NullPointerExceptions (JEP 358), Records (JEP 359), Switch Expressions (JEP 361), Text Blocks (JEP 368)

**Java 15: **Sealed Classes or Interfaces(JEP 360)

Java 14

Pattern Matching for instanceof

Each Java programmer should know the older syntax used to define an instanceof block. It foresaw three main steps: test, declaration of new variable and a conversion (casting obj to String)

if (obj instanceof String) {
    String s = (String) obj;
    // use s
}

The istanceof operator is now extended to take a type test pattern instead of just a type. In the example below, _YourClass y _is the type test pattern.

public class YourClass {

		private int value;

		public YourClass(int value) {
			this.value = value;
		}

		public int multiply(Object o) {
			if (o instanceof YourClass y)
				return this.value * y.value
		}

		// other methods ...

		@Override
		public boolean equals(Object o) {
			return (o instanceof YourClass y)
	        	&& value == y.value;
		}

	}

As you can notice, the new instanceof operator allows you to test, declare and cast variables in a single step. Obviously, you can use the newly declared variable right away!

This kind of syntax simplifies the implementation of methods too. Take a closer look at the equals() and multiply() methods in the snippet above.

#programming #coding #training #spring #java

The Definitive Guide To Java 15
1.20 GEEK