SKP's Java/Java EE Gotchas: Revisiting Java SE 9 Features, Part 2
SKP's Java/Java EE Gotchas: Revisiting Java SE 9 Features, Part 2
As I had promised, albeit very late, I am back with the Code Samples of my earlier article Java SE 9... What's New? — code samples for all features.
Join the DZone community and get the full member experience.Join For Free
As I had promised, albeit very late, I am back with the Code Samples of my earlier article Java SE 9... What's New? You may refer to the earlier article to understand the newly introduced features at a High Level. This article provides the concluding code samples for each of the features.
You may also like: SKP's Java/Java EE Gotchas: Revisiting Java SE 9 Features, Part 1
You may Download the Code Samples for the following New Features Here. (Import as Eclipse Project, Set Compiler/Environment as Java 9. Run the Main Class 'Java9Application' to see the Output/Outcome of the Code Samples.)
B. Core Library Changes in Java 9 (Continued)
09. Stack-Walking API
Prior to Java 9, The way to access Stack Trace was very limited and provided the entire dump or stack information at once. This was inefficient and did not allow any direct way of filtering data. With Java 9, a Lazy StackWalker API has been introduced. This will allow fetching data based on filtering conditions and is more efficient.
10. Compact Strings
Although this has no external ramification to a developer in terms of syntax or semantics change — It may impact the way we design for memory and performance. The current UTF-16 representation uses 2 Bytes for Storage. Most of the string contains characters that are only Latin-1 in nature.
The Latin-1 characters require only 1 Byte for Storage. With Java 9, String storage has been modified to start with an Additional Encoding Flag. This flag indicates whether it contains ISO-8859-1/Latin-1 characters or the UTF-16 characters. As per the official word, it has to lead to an Improved Usage of Memory and Efficient GC, but with Some Loss in Performance at Peak Loads.
The Compact Strings are always enabled in Java 9, but it can be disabled by passing in the VM Argument +XX:-CompactStrings
It has to be noted that in Java 9, the implementation of java.lang.String decides at runtime, whether the storage size is to be 2 Bytes or 1 Byte, as per the actual String size (UTF-16 or Latin-1 Character).
11. Spin-Wait Hints
For multi-threading applications, this brings in some performance improvements under Busy-Waiting or Spin-Waiting conditions. Usually, Busy-Waiting is done for synchronization of some state of the object between two or more invokers — Waiting for a condition to occur before processing starts or continues. Thread.onSpinWait() has been introduced as a static method in the Thread class and can be optionally called in Busy-Waiting loops — This will allow the JVM to issue processor instructions on some system architectures to improve Reaction Time in such Spin-Wait Loops and also Reduce the Power Consumed by the Core Thread or Hardware thread. This benefits the Overall Power Consumption of a Program, and possibly Allowing other Cores or Hardware Threads to execute at Faster Speeds within the Same Power Consumption Envelope.
12. New Version-String Scheme
From Java 9, $MAJOR.$MINOR.$SECURITY.$PATCH is the naming scheme for releases in Java. These details are also contained in the Runtime.Version Class. From Java 9, $MAJOR.$MINOR.$SECURITY.$PATCH is the naming scheme for releases in Java. These details are also contained in the Runtime.Version Class.
13. Enhanced Method Handles
A method handle is a typed, directly executable reference to an underlying method, constructor, field, or similar low-level operation, with optional transformations of arguments or return values. These transformations are quite general and include such patterns as conversion, insertion, deletion, and substitution. In Java 9, Method Handles have been Enhanced to include static methods for creating a different kind of Method Handles.
14. Variable Handles
Java's Concurrent Package (java.util.concurrent.atomic) provide all Atomic Types for performing atomic operations. Apart from this, Unsafe Operations (sun.misc.unsafe) such as creating objects without calling the constructor used in Java Low-Level Programming require to be hidden from the outside world as per JEP 260: Encapsulate Most Internal APIs.
This has led to creation of a new abstract class type named VarHandle — This will allow a developer to assign different types to the same reference (dynamically typed reference). It can also take care of performing atomic operations on the held variable, including compare and swap (set or exchange) operations. It also provides memory fencing operations, to order the in-memory representation of the object, by providing finer grain control.
Firstly, You need to understand Memory Ordering Effects, as VarHandle is entirely based on the Understanding of Plain, Volatile, Opaque, Acquire/Release Memory Ordering Modes. You may refer to them at https://www.baeldung.com/java-variable-handles (Under Memory Ordering Effects). You have to try and visualize this correctly and then proceed to the Code Samples.
15. Filter Incoming Serialization Data
This feature is related to the addition of filters at the serialization incoming streams to improve security and robustness. The core mechanism is a filter interface implemented by serialization clients and set on an ObjectInputStream.
The filter interface methods are called during the de-serialization process to validate the classes being de-serialized, the sizes of arrays being created, and metrics describing stream length, stream depth, and a number of references as the stream are being decoded.
The filter returns a status to accept, reject, or leave the status undecided. Some Helpful Information On this Feature can be found at https://goo.gl/bRezWt You have to remember the words Security, Vulnerabilities and Robustness as the main reasons behind the creation of this feature in Java. This will thwart possible Security Attacks like Denial Of Service.
The core mechanism is a Filter Interface implemented by serialization clients and set on an ObjectInputStream. The filter interface methods are called during the Deserialization Process to Validate the classes being Deserialized, The Sizes of Arrays being Created, The Stream Length, Graph Depth and Number of References as the Stream are being Decoded.
A filter determines whether the arguments are ALLOWED or REJECTED and should return the appropriate status. If the filter cannot determine the status it should return UNDECIDED. Filters are designed for the specific use case and expected types. A filter designed for a particular user may be passed a class that is outside of the scope of the filter. If, for example, the purpose of the filter is to Black-List Classes then it can reject a candidate class that matches and report UNDECIDED for others.
Check out the Eclipse Console Output provided Below from Running the 'Java9Application' (Refer Attached Code Samples). Please refer to the Individual Classes, provided for each of the features to understand the features better. Go ahead — Add, Modify, Delete to Experiment with all of the New Java 9 Features. The Output for the Features Marked [01-08] is shown in Part 1 of the Article at Java SE 9... What's New? [Code Samples — 01/02].
16. More Concurrency Updates
[Will be Covered In a Separate Article, As there are Many Changes to Cover and Merits an Article on Its Own]
Happy Coding with Java 9!
Published at DZone with permission of Sumith Puri , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.