The latest JEP draft submitted by Brian Goetz, one of the chief Java Language Architects at Oracle, includes critical features in the JDK development. Java is often criticized for being excessively verbose and this hurt the Java architects.
Type Declaration and Sealed Types Enhancements
The draft “Records and Sealed Types” is intended to make Java code that models no complex data aggregates simpler to read, write, and to be modified/ corrected.
The JEP has two latest language constructs. Firstly, Records are there to extend Java. Records are introduced to provide a new, compact syntax for the class declaration that is “transparent holders for immutable data.” Secondly, “sealed types” are there with which classes and interfaces can be declared that limit their subtypes.
Both records and sealed type features are often implemented simultaneously in different languages under the name – “algebraic data types.”
Let’s understand the depth of Records and Sealed types
Records
Record is a new type of declaration proposed in the Java language. Like an enum, it is a restricted form of class that declares its representation and assigns to an API that suits that representation.
There will be no key degree of freedom offered by Records that classes usually enjoy; in other words, the ability to decouple a classes API from representation. However, there will be a significant degree of concision. A data is “the state, the complete state, and nothing but the state.”
Its form in the proposal looks like-
record Point(int x, int y) { }
From the above example, it is clear that using record point.
There is a name, a state description, and a body.
Records are classes, they can include maximum things other classes can – accessibility modifiers, an implements clause, Javadoc, annotations, type variables. The body may declare static methods, constructors, static fields, instance methods, instance initializers, and nested types.
Sealed types
Second type is sealed type that is suggested for implementing algebraic data types. There are two goals to be achieved –
1. To restrict which classes can be a subtype
2. To allow “exhaustiveness analysis at the use-site”
The proposed syntax of sealed types looks like-
sealed interface Node
permits A, B, C { … }
Java developers can make use of both Records and Sealed types together to model arithmetic expressions. You can take reference from the JEP draft-
sealed interface Expr { }
record ConstantExpr(int i) implements Expr { }
record PlusExpr(Expr a, Expr b) implements Expr { }
record TimesExpr(Expr a, Expr b) implements Expr { }
record NegExpr(Expr e) implements Expr { }
Goals and motivation
As we discussed earlier, Java has faced criticism for being over verbose or having excess of “ceremony” and one of the reasons for that is classes that are nothing more than just “simple data carriers”.
To write simple data carrier class in the right way, developers have to write more of low-value, repetitive, error-prone code like – accessors, constructors, hashCode(), equals(), toString(), etc. Thus, the feature is intended to make Java code writing simply to read, compose, and to be modified.
Now learn the favorite tools of java developers that help them in development
Java has been the base of enterprise apps for over a decade. Have you ever wonder how are java developers using the language? There is no surprise that most of them consider themselves to be back-end developers, composing apps and wrangling databases running over servers.
Java’s role as the language of choice to develop apps for android devices is also evident. Recently Kotlin is favored by Google and things changed.
When the industry talks about most popular IDEs and code editors- JetBrains IntelliJ IDEA comes on the top of the list followed by java developers. The most popular databases used by java developers are – MySQL, SQLite, PostgreSQL and MS SQL Server.
Most popular web frameworks among java developers are – JS web framework JQuery, Spring, MVC, and Express.js.
Top Tools for Java development used by developers
JRat –
it is a Java Runtime Analysis Toolkit. It is user-friendly, open-source performance profiler for the java platform. Jrat can help in monitoring the execution and performance measurements of the app.
JUnit –
Junit is an open-source unit testing tool for java developers. With the help of JUnit, programmers can initiate test-driven development and deployment.
NetBeans –
It is free, open-source IDE that allows developers to build apps for desktop, mobiles, and web.
Apache Maven –
It is a software project management tool based on the POM (Project Object Model) concept.
Mockito –
It is an open-source Java Mocking and Unit testing tool that helps developers to compose clean, compact and loosely-coupled code. It is an open-source Java mocking framework.
Clover –
It is a tool used for generating code coverage reports from unit tests. Clover can be executed as a plugin in Eclipse, Ant, or Maven. Developers can even use clover for collecting coverage data of integration tests.
Ehcache –
Ehcache is an open-source, standards-based caching tool intended for java community. Experts have made Ehcache as a robust, proven, and feature-rich tool that can be integrated with other popular frameworks and libraries.
- Latest Type Declaration and Sealed Types Enhancements by Java - September 13, 2019