Latest Features in Java 17

Record
JDK 17 incorporates several new language features that have been added to Java since the previous LTS version, which was JDK 11. One such new feature is a Record.
As a preview in Java 14 records were introduced and became a full release in Java 16 and since Java 17 developers started using them in their projects.
The main idea behind the creation of records is to reduce the effort required to create a class whose primary purpose is to organize two or more values into a single unit.
Records are a way to create data classes that are immutable and concise. They can save you a lot of boilerplate code and make your code more readable and maintainable.
Syntax :
record recordName(component-list) {
// optional body statements
}
As shown in the above syntax, To create a record, we use the keyword record followed by the name of the record and the list of fields in parentheses.
Records are a new kind of type declaration in Java that allows us to define the state of an object as a list of fields.
The compiler will automatically generate the constructor, getters, equals, hashCode, and toString methods for us based on the fields we specify and we don't have to write any of them ourselves.
Here is how we can create an Employee record:
public record Employee(String name, int age, double salary, String department) {}
That's it! we created an Employee record. We don't need any semicolons or curly braces. Just one line of code. And this record has all the functionality that our previous class had. We can create an instance of it using the constructor
Employee employee = new Employee("John", 25, 5000.0, "IT");
A key point about a record is that its data is held in private final fields and only getter methods are provided. Thus, the data that a record holds is immutable.
Example :
package seeta.demos;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;
// Declare an employee record. This automatically creates a
// record class with private, final fields called name and idNum,
// and with read-only accessors called name() and idNum().
record Emp(String name , String idNum){};
public class RecordDemo {
public static void main(String[] args) {
Emp e1 = new Emp("seetaram","101");
Emp e2 = new Emp("sandeep","102");
Emp e3 = new Emp("Reyansh" , "103");
Emp e4 = new Emp("saketh","104");
List<Emp> emplist = Arrays.asList(e1,e2,e3,e4);
/* ListIterator<Emp> it = emplist.listIterator();
while (it.hasNext()){
System.out.println(it.next());
}*/
emplist.stream()
.filter(item->item.name().startsWith("s"))
.collect(Collectors.toList())
.stream()
.forEach(emp-> System.out.println(emp));
}
}
In other words, once you construct a record, its contents cannot be changed.
However, if a record holds a reference to some object, you can make a change to that object, but you cannot change to what object the reference in the record refers.
Thus, in Java terms, records are said to be shallowly immutable.
A record cannot inherit another class. However, all records implicitly inherit java.lang.Record, which specifies abstract overrides of the equals( ), hashCode( ), and toString( ) methods declared by Object.
Implicit implementations of these methods are automatically created, based on the record declaration
A record type cannot be extended. Thus, all record declarations are considered final.
Although a record cannot extend another class, it can implement one or more interfaces
package seeta.demos;
import java.util.Arrays;
import java.util.List;
interface Database{
public void displayDB(String databaseName);
}
// A record can implement an interface
record MysqlDB(String dbVersion , String dbName) implements Database{
//String company; //Instance field is not allowed in record
static String company; //any other fields must be static inside Records
@Override
public void displayDB(String databaseName) {
System.out.println("The DB Details are " + databaseName);
}
//we can override the canonical constructor provided inside the record
MysqlDB(String dbVersion,String dbName){
this.dbVersion = dbVersion;
this.dbName = dbName.toUpperCase();
}
}
public class RecordDemo1 {
public static void main(String[] args) {
MysqlDB db1 = new MysqlDB("1.A-2020" ,"mysql database" );
MysqlDB.company="Oracle Corp";
db1.displayDB(db1.dbName());
System.out.println(MysqlDB.company);
// A record can be Generic
List<MysqlDB> dbList = Arrays.asList(
new MysqlDB("1.C-1998" ,"mysql database" ),
new MysqlDB("1.B-2020" ,"mysql database" )
);
System.out.println(dbList);
}
}
- That's all for now, Happy Learning and let's learn Together :)
Subscribe to my newsletter
Read articles from Seetaram directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
