What new features and improvements are coming to Java in 2022 is something I’m sure many Java developers would like to know. Combine that with some recent sharing from Java language architect Brian Goetz (Brian Goetz). Here are some blurbs.
Valhalla
Brian Goetz published a piece late last year called State of Valhalla, which Very informative, it mentions that back in 2014 the Java project team started a project called Valhalla, which will bring more flexible, flat data types to the JVM platform. In 2021 there will be further moves to bring value objects, primitive classes and specialized generics to the JVM platform. Let’s start today by talking about what value objects are.
We know what a “value” is and what an “object” is, but what is a “value object”? Not only are you confused, I’m confused too, so let’s look at it together.
Deficiencies of the Java type system
The Java type system consists of 10 built-in types that cannot directly express complex data structures such as strings, 3D coordinates, spatial vectors etc., but developers can use these 10 types to model business entities and the Java type system is very useful.
However, Java types are still ‘flawed’ in that two objects of the same class contain exactly the same properties, but they are not addressed in memory in the same way.
So in a sense they have their own identifier.
But not for primitive types. If one variable of type int
has value 7
and another also has value 7
, does it make sense to distinguish between them? Is this 7
or that 7
? Obviously, it is meaningless.
Let us take another realistic example. Two red dresses of the same size and material are certainly two different dresses, but they are certainly of one material and one colour, and no fool would think that they are two colours. The size here can certainly be described by the primitive types in Java, but the material and colour cannot (although the colour can be represented in hexadecimal), and here the size, material and colour should all be considered primitives.
This pain point led to the creation of the Valhalla project.
Object headers
In order to understand what the Value Object / Class and Primitive Object / Class concepts introduced by Valhalla can bring to the table, we need to look at how the JVM keeps objects in memory.
The object header is very important for the object of the class, determining which thread can access the object, the rubbish collector tag, the object hash; and more importantly the object’s type pointer, which enables dynamic access to the object’s class at runtime and details from its class to that object, such as inheritance polymorphism and reflection.
But there are two sides to everything, the size of the memory footprint of a Java object depends on the sum of the information it contains, the object header needs at least 16 bytes on 64-bit systems and at least 8 bytes on 32-bit systems (of course the JVM can set how the object header is stored via a configuration item). Many objects don’t need to be multi-threaded and don’t need any object identification, like the color of the dress mentioned above, only the value of the color is something we care about. This redundant memory footprint makes Java a criticism.
Value Class
For many objects, the equivalence of the values of their properties is what we care about, other class information is of little use, and classes written only to hold values and operate on them represent a very large proportion of all classes. The Valhalla project has introduced a new class type for such scenarios: Value Class. It is still only a JEP draft, but it is already taking shape.
|
|
Value Class is similar to our common classes, but it may (and is still under discussion here) have some of the following properties.
- A value object is an object without an identity; normally we check the identity with the
==
operator, which may no longer be distinct fromequals()
here. - The value class itself and all its fields are
final
by default. - The class does not directly or indirectly implement
java.lang.IdentityObject
(a new superclass with an identity class). This means that the superclass is either a stateless abstract class, orObject
is a stateless abstract class. - Value classes are implicit implementations of
java.lang.ValueObject
. - There is no constructor
super
function to call the constructor. Instances will be created without executing any superclass initialization code. - Cannot use the
synchronized
keyword in the value class. - (Possibly) The class does not declare a
finalize()
method. - (Possibly) The constructor does not use
this
to set the fields in the body of the constructor, or possibly after all fields have been explicitly memory allocated.
Other operations should not differ much from normal classes, but note that some of the original classes in the JDK standard library need to be handled for compatibility if they are identified as Value Class.
value to be a reserved word or a keyword?
This is not the whole story
Value Class neuters the Java class object header in favour of reducing Java’s memory consumption, but that’s not all that Valhalla plans to do. I actually had a hard time writing this part, which is too far ahead of its time, and it took me several days to conceive it. Understanding the design of a programming language from a scenario is good for improving yourself from the ground up.