Posts

Showing posts with the label java 14

ZGC | Using -XX:SoftMaxHeapSize

In JDK 13 we introduced a new JVM option called -XX:SoftMaxHeapSize=<size>. ZGC is so far the only garbage collector in HotSpot that supports this options, but work to also support it in G1 is ongoing. Since this option is relatively new, and perhaps not yet widely known, I thought I’d write a few words about when and how to use it. When is -XX:SoftMaxHeapSize useful? As the name implies, this new option sets a soft limit on how large the Java heap can grow. When set, the GC will strive to not grow the heap beyond the soft max heap size. But, the GC is still allowed to grow the heap beyond the specified size if it really needs to, like when the only other alternatives left is to stall an allocation or throw an OutOfMemoryError. There are different use cases where setting a soft max heap size can be useful. For example: When you want to keep the heap footprint down, while maintaining the capability to handle a temporary increase in heap space requirement. Or when you want...

Java Local Variable Type Inference

Java Local Variable Type Inference var variable / keyword: You can declare local variables with non-null initializers with the var identifier, which can help you write code that’s easier to read. Consider the following example, which seems redundant and is hard to read: URL url = new URL("http://www.oracle.com/"); URLConnection conn = url.openConnection(); Reader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); You can rewrite this example by declaring the local variables with the var identifier. The type of the variables are inferred from the context: var url = new URL("http://www.oracle.com/"); var conn = url.openConnection(); var reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); var is a reserved type name, not a keyword, which means that existing code that uses var as a variable, method, or package name is not affected. However, code that uses var as a class or interface name is affected and ...

Java with Nashorn engine

In this document we will discuss about how to use Java program with Nashorn engine for scripting features provided by Java SE. Introduction to the Nashorn Engine It was fully developed in the Java language as part of the Nashorn Project. The code is based on the new features of the Da Vinci Machine, which is the reference implementation of Java Specification Request (JSR) 292: Supporting Dynamically Typed Languages on the Java Platform. The Nashorn engine is included in the Java SE Development Kit (JDK). You can invoke Nashorn from a Java application using the Java Scripting API to interpret embedded scripts, or you can pass the script to the jjs or jrunscript tool. The Nashorn engine implements many new features introduced in ECMAScript 6 including template strings, let, const, and block scope, iterators and for..of loops, Map, Set, WeakMap, and WeakSet data types, symbols, and binary and octal literals. Invoking Nashorn from Java Code To invoke Nashorn in your Java app...

Java Scripting Programming Tutorial

Scripting Languages and Java Scripting languages are programming languages that support the ability to write scripts. Unlike source files for other programming languages that must be compiled into bytecode before you run them, scripts are evaluated by a runtime environment (in this case, by a script engine) directly. The JavaScript Package The Java Scripting API consists of classes and interfaces from the javax.script package. It is a relatively small and simple package with the ScriptEngineManager class as the starting point. How to Use the Java Scripting API to Embed Scripts To use the Java Scripting API: 1. Create a ScriptEngineManager object. 2. Get a ScriptEngine object from the manager. 3. Evaluate the script using the script engine's eval() method. Java Scripting API Examples with Java Classes The following examples show you how to use the Java Scripting API in Java. Evaluating a Statement In this example, the eval() method is called on the script engin...

Java Records example

Records introduces JDK 14, a new kind of type declaration. It’s ideal for "plain data carriers," classes that contain data not meant to be altered and only the most fundamental methods such as constructors and accessors. Consider the following class definition: final class Rectangle implements Shape {     final double length;     final double width;          public Rectangle(double length, double width) {         this.length = length;         this.width = width;     }          double length() { return length; }     double width() { return width; } } It has the following characteristics: All of its members are declared final Its only methods consist of a constructor, Rectangle(double length, double width) and two accessors, length() and width() You can represent this class with a record: record Rectangle(float length, float width) {...

Java 14 Has Been Released | What's new in JDK 14?

The following are some of the important additions and updates in Java SE 14 and JDK 14: Switch Expressions is now a permanent feature. G1 is enhanced to improve allocation performance on non-uniform memory access (NUMA) memory systems. The Flight Recorder event steaming API enables you to continuously consume Flight Recorder data. New JDK-specific file mapping modes have been added so that the FileChannel API can be used to create MappedByteBuffer instances that refer to non-volatile (NVM) memory. Currencies format with locale-specific accounting formats The class com.sun.management.OperatingSystemMXBean has been enhanced to ensure that it reports values based on the current operating environment, such as a container environment. The following are experimental, preview, or incubator JDK 14 features: Records, a preview feature, introduces a new kind of type declaration that's ideal for "plain data carriers," classes that contain data not meant to be altered ...

Java - APIs for Creating Unmodifiable Collections

Several new APIs have been added that facilitate the creation of unmodifiable collections. The List.copyOf Set.copyOf Map.copyOf  methods create new collection instances from existing instances. New methods have been added to the Collectors class in the Stream package. toUnmodifiableList toUnmodifiableSet toUnmodifiableMap   These allow the elements of a Stream to be collected into an unmodifiable collection.

Lambda expressions - implicitly typed parameters

For formal parameters of implicitly typed lambda expressions, allow the reserved type name `var` to be used, so that:     (var x, var y) -> x.process(y) is equivalent to:     (x, y) -> x.process(y) An implicitly typed lambda expression must use `var` for all its formal parameters or for none of them. In addition, `var` is permitted only for the formal parameters of implicitly typed lambda expressions --- explicitly typed lambda expressions continue to specify manifest types for _all_ their formal parameters, so it is not permitted for some formal parameters to have manifest types while others use `var`. The following examples are illegal:     (var x, y) -> x.process(y)         // Cannot mix 'var' and 'no var' in implicitly typed lambda expression     (var x, int y) -> x.process(y)     // Cannot mix 'var' and manifest types in explicitly typed lambda expression In theory, it would be...