What new features and improvements will Java have in 2022, I believe many Java developers want to know. Combined with some recent sharing by Java language architect Brian Goetz ( Brian Goetz Old rules, like and go.
Valhalla
Brian Gertz published an article called State of Valhalla at the end of last year, which contains a lot of information. It mentioned that as early as 2014, the Java project team started a project called Valhalla . This project will bring more flexible, flattened data types JVM In 2021 the project will have further action, value objects (value Objects) , original class (primitive classes) , specialized generic (Specialized generics) the upcoming introduction of the JVM platform. Today, let's talk about what this value object is.
We know what a "value" is and what an "object" is, but what is a "value object"? Not only you are stunned, but I am also stunned, come and study together.
Weaknesses of the Java Type System
The Java type system consists of 10 built-in types. These 10 types cannot directly express complex data structures, such as strings, three-dimensional coordinates, space vectors, etc., but developers can use these 10 types to model business entities , Java's type system is very useful.
But Java types still have "defects", two objects of the same class contain exactly the same properties, but their memory addressing is not the same.
So in a sense, they have their own identity.
But it's different for primitive types, if a variable of type int
7
and the other is also 7
, does it make sense to distinguish them? This 7
or that 7
? Obviously pointless.
Let's take another real-life example. Two red clothes of the same size and material must be two different clothes, but their material must be the same material, and the color must be the same color. No fool will think that this is true. are two colors. The size here can of course be described by the primitive types in Java, but the material and color cannot (although the color can be expressed in hexadecimal), the size, material and color here should be considered as primitives.
This pain point led to the birth Valhalla
object header
To understand what Value Object / Class and Primitive Object / Class concepts introduced by Valhalla bring us, we need to look at how the JVM objects in memory.
The object header is very important for the object of the class, which determines which thread can access the object, the garbage collector mark, the object hash; more importantly, the object's type pointer, which can dynamically access the object's class at runtime, and from its class to the object's type pointer. Details of the object, such as inheritance polymorphism, reflection.
But everything has two sides, the size of Java object memory 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 JVM You can set how to save the object header through the configuration item). Many of the objects do not need multi-threaded, does not require any object identity, as mentioned above color of the clothes , only color values is something we care about. This redundant memory footprint makes Java criticized.
Value Class
For many objects, the equality of its property values is what we care about, other class information is useless, and a very large percentage of all classes are written just to hold values and operate on them. Valhalla project introduces a new class type for such scenarios: Value Class . It's still only a JEP , but it has already taken shape:
value class Substring implements CharSequence {
private String str;
private int start;
private int end;
public Substring(String str, int start, int end) {
checkBounds(start, end, str.length());
this.str = str;
this.start = start;
this.end = end;
}
public int length() {
return end - start;
}
public char charAt(int i) {
checkBounds(0, i, length());
return str.charAt(start + i);
}
public Substring subSequence(int s, int e) {
checkBounds(s, e, length());
return new Substring(str, start + s, start + e);
}
public String toString() {
return str.substring(start, end);
}
private static void checkBounds(int start, int end, int length) {
if (start < 0 || end < start || length < end)
throw new IndexOutOfBoundsException();
}
}
Value Class similar to our common class, but it may (still under discussion here) have the following characteristics:
- The value object is an object without identity. Usually, we use the
==
operator to check the identity, which may not be different fromequals()
- The value class itself and all its fields are
final
by default. - This class does not directly or indirectly implement
java.lang.IdentityObject
(a new superclass of the Identity class). This means that the superclass is either a stateless abstract class, orObject
is a stateless abstract class. - The value classes are all implicit implementations
java.lang.ValueObject
- No constructor
super
function call constructor. Instances will be created without executing any superclass initialization code. - Cannot use the
synchronized
keyword in a value class. - (probably) the class does not declare the
finalize()
method. - (Probably) the constructor does not use
this
to set the fields in the constructor body, or maybe after all fields have been explicitly memory allocated.
Other operations should be little different from ordinary classes, but it should be noted that if some original classes in the JDK standard library are identified as Value Class compatibility processing is required.
value be a reserved word or a keyword?
that's not all
Value Class the Java class object header, which is beneficial to reduce Java's memory consumption, but this is not the whole plan of Valhalla For this part of the content that is too advanced, it is actually very difficult for me to write it. After thinking about it for several days, understanding the design of a programming language from the scene is beneficial to fundamentally improve myself. If you want to know more about Valhalla , you can follow me and I will continue to share relevant knowledge.
: Felordcn for more information
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。