Java inline class is a feature by Project Valhalla aiming to improve the compatibility of Java applications with modern hardware. The Java Inline classes can enable Java developers to use data types that behave similarly to primitive data types in Java. In this article, we will be looking into Java inline classes, Their workings and functionalities. We will also explore the evolution of project Valhalla in 2022 and the progress of Java inline classes along with it.
Table of Contents
Launched in 2014, right after the release of Java 8, Project Valhalla is quite a large project in Java. It has been in development for years. It can be considered as an umbrella of projects that include various Java sub-projects. The basic aim of Valhalla is to provide new features for Java developers to get better memory latency and memory density by the efficient use of memory for their applications.
See Also: Project Valhalla And What Java Developers Need To Know About It
After introducing inline classes, Project Valhalla has more offerings and aims to fulfill two goals in 2022. The proper release of the inline type is “codes like a class, works like an int,” quoting their mission statement. They are also working on improving the type system of Java that separates the reference types from primitive types.
Previously, Java only supported primitive data types and object references. Project Valhalla extended this by the introduction of Java inline classes. As mentioned earlier, the introduction of Java inline classes showed the scope of significant improvements in the compatibility of Java programs with modern hardware.
In terms of compatibility, the data values model of Java plays an integral role. This model has been known for its simplicity as it had to deal with Java primitive data types and object references, making it very easy for developers to understand. Still, there have been some trade-offs in terms of performance. For instance, declaring an array of objects would involve unavoidable indirections, leading to cache misses and degrading the performance. This concerned various Java developers, and they were seeking a dire update for the model.
You can create two types of classes in Java; the traditional class of reference type with identity and the new Java inline class.
Although Java inline class can easily implement any interface, the creation of Java inline class requires these two interfaces:
Every newly created inline class implicitly implements these interfaces. You can create an inline class using the keyword “inline”. This is how you can create an inline class in Java.
1. inline public class demoClass { 2. public int v1; 3. public int v2; 4. 5. public demoClass(int v1, int v2) { 6. this.v1 = v1; 7. this.v2 = v2; 8. }
Java inline classes prove to be the awaited solution for memory efficiency issues as a better layout would result in fewer indirections, which means fewer cache misses and better performance.
Following are the highlighted features of Inline classes in Java:
1. No need for Object Headers
Another advantage of Java inline classes was eliminating the need for a full object header for each data composite. This was achieved by flattening the data. Now, each object declared in a Java code is stored in Java’s heap memory. It has a metadata header along with the actual data. This metadata makes sure that two object instances must not be considered equal just because they contain the same values for all their fields. The metadata is used to differentiate every object by its unique memory location despite the same content. The double equal to (‘==’) operator is used in Java to determine whether two references are pointing at the same memory location, and two objects would not be considered identical if they are stored in different memory locations.
However, in Java inline classes, the only thing that is required to check whether the data is equal is the bit pattern of the data because that would be different despite the same data values. As the header for each object is not needed anymore, it can be removed, which would free up enough memory during runtime that developers can now make significant optimizations in layout; they can call a convention, compilation, and allocation.
2. Flattened arrays
Arrays in Java have also been affected by the inline classes. You can create a flattened array of the inline type now as there are no references like in traditional classes. For instance, this is how an array of inline Point objects can be visualized,
It is quite evident that the use of Java inline consumes less memory as no point headers are needed. It improves the locality as the data is sequentially laid out in memory and it can be accessed directly without following any indirect reference pointers.
Starting with, Inline classes are immutable due to which they are implicitly final and cannot be abstract. All instance fields of an inline class will be implicitly final.
The removal of the object header also has some trade-offs for the design of inline classes. For instance, objects cannot be synchronized because now they do not possess a unique identity nor any memory space to store the monitor. The loss of objects also means that some identity-sensitive operations are restricted on inline objects due to a lack of unique identity and they can now only allow partial polymorphism.
Java Inline classes are not nullable; it does not allow to assign a null value, instead there is a set default value. Similar to primitive data types, the new data type will not contain the address of the value instead it will hold the value itself. Therefore, it cannot be null. Instead, each inline object will have a default value that can directly be accessed and can be later altered. This can generate some serious issues in migration and backward compatibility. Suppose, a previously made array might currently have some null values in it, using an inline class the array will have default values instead.
Although, two inline objects can still be compared using the “==” operator, it will now not only compare the memory addresses, instead the bit pattern of the data will be compared. Therefore, “==” needs to check if all fields are equal and have the same type of data. This can be a time and resources consuming process, especially for large objects with inline types as fields.
As Java implicitly assumes nullability of values, and inline classes are not nullable, which raises a problem for developers. To resolve this, a technique called indirect projection was introduced. Simply put, it can be understood as a form of autoboxing for inline classes. The indirect projection type can later be used as the parameter in a generic type, whereas the real inline type cannot be used.
See this code snippet below that demonstrates indirect projection in Java:
1. public static void main(String[] args) { 2. List<OptionalInt?> opt = new ArrayList<>(); 3. for (int i=0; i < 10; i++) { 4. opt.add(OptionalInt.of(i)); 5. opt.add(OptionalInt.empty()); 6. opt.add(null); 7. } 8. int sum = opt.stream() 9. .mapToInt(x -> { 10. if (x == null) 11. return 0; 12. OptionalInt opt1 = (OptionalInt)x; 13. return opt1.orElse(0); 14. }) 15. .reduce(0, (a, b) -> a + b); 16. 17. System.out.println("Final Sum : "+ sum); 18. }
Instances of an inline class can also be cast to an instance of the indirect projection, but that will require a null check, as demonstrated above in the body of the lambda.
As previously mentioned, restriction of backward compatibility and migration of existing classes is a significant problem with Inline classes due to being not nullable. Although classes can be migrated using the wrappers or by indirect projection. Inline widening proves to be a better solution for that.
Inline widening adds a companion interface to each inline class. The interface is automatically generated at the time of compilation. This indicates that there will then be a reference type representation of each inline type. This approach is also much faster compared to the boxing implementation. Along with providing nullability, inline widening also allows the JVM to take the decision of flattening as well.
Project Valhalla has been in development for a long time. The prospects for Java inline classes clearly showcase the potential features offered by project Valhalla. We discussed how Java inline classes have been introduced to help developers improve the compatibility of Java programs with modern hardware. It also offers to utilize memory more efficiently and also fewer cache misses along, resulting in higher performance.
Shaharyar Lalani is a developer with a strong interest in business analysis, project management, and UX design. He writes and teaches extensively on themes current in the world of web and app development, especially in Java technology.
Create a free profile and find your next great opportunity.
Sign up and find a perfect match for your team.
Xperti vets skilled professionals with its unique talent-matching process.
Connect and engage with technology enthusiasts.
© Xperti.io All Rights Reserved
Privacy
Terms of use