What are switch expressions and how are they different from switch statements?

The switch statement: Unlike the if/else if/else statement, a switch statement can have a number of possible execution paths. A switch works with the primitive types, byte, short, char, and int, their respective wrapper types (Byte, Short, Character, and Integer), enumerated types, and the String type1. While an if-else statement is used to test expressions … Read more

How to document Java Record parameters?

IntelliJ Bug / Missing Feature Using the in-built JDK tool for javadoc with the version 14-ea and above, I could easily generate Javadoc for a record. The command used for the same is \ /jdk-14.jdk/…/javadoc –release=14 –enable-preview …/src/main/java/…/CityRecord.java So this would certainly be something missing in IntelliJ. (Since the ‘Add Javadoc’ option also doesn’t include … Read more

How to have placeholder for variable value in Java Text Block?

You can use %s as a placeholder in text blocks: String str = “”” { “someKey”: “someValue”, “date”: %s, } “”” and replace it using format() method. String.format(str, LocalDate.now()); From JEP 378 docs: A cleaner alternative is to use String::replace or String::format, as follows: String code = “”” public void print($type o) { System.out.println(Objects.toString(o)); } … Read more

How is NullPointerException in Java 14 different from its predecessor?

The JVM throws a NullPointerException at the point in a program where code tries to dereference a null reference. With Java SE 14, the NullPointerException offers helpful information about the premature termination of a program. Java SE 14 onwards, the JVM describes the variable (in terms of source code) with a null-detail message in the … Read more

What is the use of @Serial annotation as of Java 14

What I don’t understand, does the annotation affect the de/serialization itself No. Its retention is ‘source’, so it’s discarded after compilation. The bytecode will contain no trace of it. It has no way to influence runtime behaviour (besides possibly compile-time code generation, which does not happen). Like @Override, it is optional and is supposed to … Read more

Do you need to override hashCode() and equals() for records?

No you do not need to define your own hashCode and equals. You may do so if you wish to override the default implementation. See section 8.10.3 of the specification for details https://docs.oracle.com/javase/specs/jls/se14/preview/specs/records-jls.html#jls-8.10 Note, specifically, the caveat on implementing your own version of these: All the members inherited from java.lang.Record. Unless explicitly overridden in the … Read more

Define default constructor for record

To split hairs, you cannot ever define a default constructor, because a default constructor is generated by the compiler when there are no constructors defined, thus any defined constructor is by definition not a default one. If you want a record to have a no-arg constructor, records do allow adding extra constructors or factory methods, … Read more

Post Java-14 getter/setter naming convention

Quote from JEP 359: It is not a goal to declare “war on boilerplate”; in particular, it is not a goal to address the problems of mutable classes using the JavaBean naming conventions. My understanding, based on the same document is that records are transparent holders for shallowly immutable data. That being said: Records are … Read more

Why is Java 14 not LTS?

Take look at this announcement: For product releases after Java SE 8, Oracle will designate a release, every three years, as a Long-Term-Support (LTS) release. This means another LTS (ver. 17) should be 3 years after Java 11, so in September 2021. Java 14, however, was released in March 2020, so no LTS. Java 8 … Read more

tech