Java 13 PDF
Java 13 PDF
Java 13
WRITTEN BY SIMMON RITTER
AZUL SYSTEMS
We are now well into the new six-month release cadence of the JDK and, The opening quotes must be followed by a line terminator. Text blocks can-
with the release of JDK 13, it is clearly working well. not be used on a single line, thus the following code would generate an error:
One thing that is obvious is that, whilst the overall rate of change for the String smallBlock = \"\"\"Only one line\"\"\";
Java platform has increased, individual releases will have fewer new
A simple piece of HTML can now be assigned to a String like this:
features. This is the case with JDK 13, which includes only five JDK En-
hancement Proposals (JEPs) and 76 new core library elements (of which String htmlBlock = " " "
blocks (JEP 355). Java has always suffered a little from the way strings
are defined. A string starts with a double-quote and ends with a dou-
ble-quote, which is fine, except that the string cannot span more than
Zulu
one line. This leads to workarounds, like including \n where line breaks
are required or concatenating strings and newlines, but these do not
lend themselves to clear code. In JDK 12, there was a proposal for raw-
string literals (JEP 326), but the feedback from the early access builds of Community
the JDK was such that the decision was made to remove it before release.
Open Source Java
Text blocks are a different solution to the same challenge. A text block
from the Java Runtime
starts with a triple double-quote and is terminated with the same. Any-
thing between them is interpreted as a part of the string, including new
Performance Leader
lines. There is nothing magical about the end result, which is still a nor-
mal java.lang.String object. Text blocks can be used anywhere that a FREE DOWNLOAD
string literal can be used in Java with no difference to the compiled code.
1
Zulu
Community
Open Source Java from the
Java Runtime Performance Leader
DOWNLOAD NOW
INCLUDES:
There are a couple of subtleties that you need to be aware of in using The --release flag is newer but can be replaced with --source . To run
text blocks. the application, we need to enable the preview features:
The positioning of the closing quotes is significant because it deter- java --enable-preview TextBlock
In the above example, the closing quotes are placed on their own line; in at the same time. However, because these relate directly to a language
addition to controlling the amount of incidental space eliminated, this preview feature, which may be changed or removed, it makes perfect
will also add a newline to the end of the generated string. If this is not sense. There is a new proposal to add an annotation, @PreviewFeature ,
required, the closing quotes can be placed directly after the end of the that would make this situation easier to handle.
text. However, it is then not possible to control incidental space. This can
There is another (small) language change in JDK 13, covered by JEP 354.
be done explicitly using the indent() method of String .
This applies to the switch expression feature that was included in JDK 12
Regular Java language escape sequences work as expected and double as the first-ever preview feature. Here, we have a perfect example of why
quotes do not need to be escaped unless you want a sequence of three, preview features are an excellent idea.
in which case, at least one of them must be escaped (if you want, you
Until JDK 12, a switch could only be used as a statement, where it
can escape two or three of the quotes or the second or the third; just
performed an action but did not return a result. In JDK 12, a switch can
escaping the first probably makes the most sense, though).
now be used as an expression, meaning it returns a result that can be
assigned to a variable. There were also several changes to the syntax of
Jim Laskey and Stuart Marks have written a very useful Programmer's
case statements within the switch expressions. Let's use the example
Guide to Text Blocks. This provides more detail of the feature with a
from the JEP to understand how this works.
variety of examples. It is thoroughly recommended.
case SATURDAY Feedback from the Java community indicated that overloading the use
of break to indicate the value to return could be confusing. The Java
numberOfLetter = 8;
language also allows the use of break (and continue ) with a label to
break;
case WEDNESDAY perform a form of goto . JEP 354 changes the use of break , in this case,
numberOfLetter = 9; to yield , so, in JDK 13, our code becomes:
break;
default: int numberOfLetters = switch (dayOfWeek) {
}; case FRIDAY:
case SUNDAY:
In this example, we're using the value dayOfWeek to assign a value to yield 6;
case TUESDAY
numberOfLetters . Due to the way the switch statement works, the
yield 7;
code is more error-prone than is ideal. Firstly, if we neglect to include a
case THURSDAY
break statement for each case label group, the default is to fall-through case SATURDAY
to the next label group. This can lead to some subtle and hard to find yield 8;
bugs. Secondly, we must make an assignment in each case label group. case WEDNESDAY
In this case, we would get a compiler error if we forgot, but it is still not yield 9;
default:
ideal. Our code is also quite verbose, since each value of dayOfWeek
throw new IllegalStateException("Huh?: " + day);
must have its own case label.
};
Using the new case-statement syntax, we get much cleaner, less er-
Here, we can see the benefit of preview features that allow a change like
ror-prone code:
this to be made easily before committing the syntax to the standard.
int numberOfLetters = switch (dayOfWeek) {
JDK 13 also includes three JEPs that relate to the virtual machine.
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
JEP 350: Dynamic CDS Archive. This is an extension to the application
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9; class data sharing functionality (AppCDS) that was contributed by
default -> throw new IllegalStateException("Huh?: Oracle to OpenJDK 10. Prior to that, it was included in the Oracle JDK
" + day); as a commercial feature. This extension allows the dynamic archiving of
}; classes at the end of the execution of a Java application. The archived
classes include all loaded application classes and library classes that are
Now, we need to make the assignment only once (from the return value
not present in the default, base-layer CDS archive. This also eliminates
of the switch expression) and can use a comma-separated list for the
the need to perform a training run of an application. Using AppCDS prior
case labels. Since we don't use a break statement, we also eliminate the
to this was a multi-step process involving the creation of a list of relevant
problem of fall-through.
classes and using this list to generate the archive to be used for subse-
The syntax of the switch expression allows the use of the older-style quent runs. Now, all that is required is a single run of an application with
syntax so, in JDK 12, we can write it like this: the -XX:ArchiveClassesAtExit flag providing the location where the
archive will be written.
int numberOfLetters = switch (dayOfWeek) {
case MONDAY: JEP 351: ZGC: Uncommit unused memory. ZGC is an experimental
case FRIDAY:
low-latency garbage collector that was introduced in JDK 11. ZGC's
case SUNDAY:
original design did not allow for memory pages to be returned to the
break 6;
case TUESDAY operating system when they were no longer required, e.g. when the
break 7; heap shrinks and the memory is unused for an extended period of time.
case THURSDAY For environments such as containers, where resources are shared be-
case SATURDAY tween a number of services, this can limit the scalability and efficiency
break 8;
of the system.
case WEDNESDAY
break 9;
The ZGC heap consists of a set of heap regions called ZPages. When
default:
ZPages are emptied during a GC cycle, they are returned to the ZPage-
throw new IllegalStateException("Huh?: " + day);
Cache. ZPages in this cache are organized in order of those used least
};
recently. In JDK 13, the ZGC will return pages that have been identified
as unused for a sufficiently long period of time to the operating system. • The type-specific Buffer classes of java.nio now have abso-
This allows them to be reused for other processes. Uncommitting lute (as opposed to relative) bulk get and set methods. They,
memory will never cause the heap size to shrink below the minimum as well as the base abstract Buffer class, include a slice()
size specified on the command line. If the minimum and maximum heap method to extract part of the buffer.
sizes are set to the same value, no memory will be uncommitted. • MappedByteBuffer has a force() method that will force a
nized methods. This will make it simpler to integrate with fibers, which the asType() method. This returns a pseudo-type.
are being provided as part of Project Loom when that is integrated with • DocumentBuilderFactory and SAXParserFactory in ja-
space-aware instances.
As mentioned previously, JDK 13 includes only 76 new APIs in the core
class libraries. These cover the following areas: As you can see, JDK 13 does not have a large number of new features
and APIs. However, it continues to deliver the incremental evolution of
• Updates to Unicode support. Java, ensuring it remains the most popular programming language on
• Three new methods in String to support text blocks already the planet.
described.
Devada, Inc.
600 Park Offices Drive
Suite 150
Research Triangle Park, NC
888.678.0399 919.678.0300