Posts

Showing posts from July, 2018

JVM Language Summit 2018 and Valhalla EA Build 0

July 2018 ended with some interesting developments related to the future of Java and the JVM. JVM Language Summit 2018 was held at Oracle's Santa Clara campus on the last two days of the month and Build 0 of the Project Valhalla "L-World Value Types" Early-Access Builds was released on July 30. JVM Language Summit 2018 Videos of presentations from the JVM Language Summit 2018 are available on YouTube . The easiest way to access these videos is via the JVM Language Summit 2018 Playlist . The recorded presentations include the following: LWorld: the next steps on the journey to Valhalla with David Simms and Tobi Ajila Below the Fold, Adventures in Constancy with Brian Goetz and Vicente Romero How to Design an IDE-Friendly Language with Peter Gromov @donnerpeter CONSTANT Dynamic with Dan Heidinga and Paul Sandoz Project Loom with Ron Pressler and Alan Bateman Graal Without Truffle with Charles Nutter Just a Small Class File Change: Nestmates: A Case Study with Karen ...

Memory-Hogging Enum.values() Method

I'm a big fan of Java's enum . It seemed like we waited forever to get it, but when we did finally get it ( J2SE 5 ), the enum was so much better than that provided by C and C++ that it seemed to me " well worth the wait ." As good as the Java enum is, it's not without issues. In particular, the Java enum's method values() returns a new copy of an array representing its possible values each and every time it is called. The Java Language Specification spells out enum behavior. In The Java Language Specification Java SE 10 Edition , it is Section 8.9 that covers enums. Section 8.9.3 ("Enum Members") lists two " implicitly declared methods": public static E[] values() and public static E valueOf(String name) . Example 8.9.3-1 ("Iterating Over Enum Constants With An Enhanced for Loop") demonstrates calling Enum.values() to iterate over an enum. The problem, however, is that Enum.values() returns an array and arrays in Jav...

JDK 11: New Default Collection Method toArray(IntFunction)

Image
The " JDK 11 Early-Access Release Notes " indicate that Early Access Build 20 of JDK 11 includes a new default method on the Collection interface that "allows the collection's elements to be transferred to a newly created array of a desired runtime type". This new default method [ Collection.toArray(IntFunction) ] works similarly to the same-named method already available on the Stream interface [ Stream.toArray​(IntFunction) ]. The next code listing demonstrates this new JDK 11 default Collection method in action (on a Set in this case). final Set<String> names = Set.of("Fred", "Wilma", "Barney", "Betty"); out.println(Arrays.toString(names.toArray(String[]::new))); Because I used an (unordered) Set , order of the String s in the generated array can be different than the order the String s were specified for initialization of the Set . This is demonstrated in the next screen snapshot (which also indicates th...

Optional.isEmpty() Available in JDK 11 EA Builds

My recently posted question " Optional.isEmpty() Coming to Java? " was prompted by a core-libs-dev mailing list post titled " RFR: 8184693: (opt) add Optional.isEmpty ". The current JDK 11 Early Access builds (such as OpenJDK JDK Early Access Build 23 that I use in this post) now include the isEmpty() method on the "Optional" classes Optional , OptionalDouble , OptionalInt , and OptionalLong . This allows for more fluent expression in cases that formerly relied upon negation of Optional.isPresent() [or ! OptionalDouble.isPresent() , ! OptionalInt.isPresent() , or ! OptionalLong.ifPresent() ] as was done previously. The next simple and contrived code listing demonstrates Optional.isEmpty() . public static void demonstrateOptionalIsEmpty() { final Optional<String> middleName = getMiddleName(); if (middleName.isEmpty()) { out.println("There is no middle name!"); } } Although the same functionality that Optional.isEmp...

Deferred Execution with Java's Predicate

In the previous posts " Deferred Execution with Java's Supplier " and " Deferred Execution with Java's Consumer ", I looked at easily deferring execution in Java via standard Java APIs that accept, respectively, Supplier s and Consumer s. In this post, I take a similar look at how standard JDK-provided APIs allow for deferred execution via the standard functional interface Predicate . The Predicate is described in its Javadoc , "Represents a predicate (boolean-valued function) of one argument." In other words, a Predicate is like a JDK-supplied Function , but with its return value limited to either true or false . Perhaps the most common application of Predicate in the standard Java APIs is in the context of filters . Several of the examples in this post will demonstrate use of Predicate in conjunction with filtering methods on instances of Optional and on instances of Stream . Optional.filter(Predicate) The behavior of the Optional class...

Applying New JDK 11 String Methods

Image
In the posts " New Methods on Java String with JDK 11 " and " String#repeat Coming to Java? ", I discussed six new methods coming to the Java String with JDK 11 . The available early access JDK 11 builds already include these new methods and I use one of those early access builds to demonstrate them in this post. I am using OpenJDK JDK 11 Early Access Build 20 for compiling and running the examples shown in this post. The six methods added to String for JDK 11 that are demonstrated in this post via the OpenJDK JDK 11 Early Access Build 20 are: String.repeat(int) String.lines() String.strip() String.stripLeading() String.stripTrailing() String.isBlank() The source code for the examples demonstrated in this post is available on GitHub . String.repeat(int) The String.repeat(int) method provides handy functionality that I've wished to see in Java since experiencing this functionality in Groovy. As its name suggests, this method repeats the String it is r...