This is the presentation about new additions in Java.
Versions included are: Java 9, Java 10 and Java 11 LTS.
Java 12 and Java 13 are omitted.
Slides can be found in resources/presentation
directory.
P.S. I DO NOT own any pictures of Duke.
Duke appeared on slides out of his own will.
☕︎ Wha' do ye mean ain't fer free?
☕︎ Collecting Garbage efficiently
☕︎ public static void main(String[] args){}
no more
This chapter is an introduction to the presentation.
Approximate speech:
Java 8 was a revolutionary release in 2014.
The features we take now for granted made Java easier to use, improved productivity and performance.
However, the IT world doesn't stand in place.
Java 8 happened 5 years ago and in 2020 Oracle will no longer update our beloved.
Does it end here or this story has a happy continuation?
This chapter focuses on the main differences between Oracle JDK and Open JDK.
Approximate speech:
With Java 11, Oracle changes its' license from Binary Code License to General Public License v2 with the Classpath Exception. It means that using Oracle JDK that is being developed by Oracle is only allowed in development and test environments. Any other use would imply buying the license. However, there is another option - Open JDK that is being developed not only by Oracle, Open JDK, and Java Community but collaborated as well by IBM, Apple, Redhat, and other top-notch companies.
For more in-depth analysis, please, check the following links:
In this chapter we will dive in main difference between Parallel Full GC and Garbage First GC.
Approximate speech:
In Java 8 the default GC is Parallel Full Garbage Collector with Java 9 things change in favor of so-called G1.
We all know the story of how JVM manages memory in Java 8. There is heap structure that looks as follow:
Young Generation | Old Generation | Permanent Generation |
---|---|---|
Eden & Survivor Space | Tenured | Permanent |
Newly created objects live in the young generation, when the young generation is full, all survived objects are eventually moved to the old generation.
So, what's so special about G1? The heap structure of Garbage First GC looks as follow:
O | O | E | E | O | |
---|---|---|---|---|---|
E | O | O | S | ||
E | O | S | O | ||
O | E | E | O | O | |
O | S | S | O | O | |
O | O | E | O |
Garbage First splits heap into many small regions. It still assigns the same roles (eden, survivor, old) but there is no limit on how many of each we have. After the marking phase, G1 knows which regions are almost empty and collects those first. The rest of alive objects are being copied to unassigned space and become survivors or old, depending on what they were before (eden or survivors). In case if the whole region was full of garbage, it becomes unassigned.
For more in-depth information, please, follow these links:
- Java Garbage Collection Basics
- Getting Started with the G1 Garbage Collector
- Understanding the G1 Garbage Collector – Java 9
This chapter is about JShell that was introduced with Java 9.
Approximate speech:
JShell is a Read-Eval-Print Loop tool for exploring the Java language.
Commands to review:
- Start the tool:
jshell
- create method
greetJShell(String name)
:
String greetJShell(String name) {
...> return name + " greets JShell!";
...> }
- call created method:
greetJShell("World") // no semicolon required!
- getting help:
/help
- listing created methods or variables:
/v
/m
- saving workspace to a file:
/save -all <file>
- open file:
/open <file>
- Tab completion tool:
System.out.<Tab>
- exit the tool
/exit
Important things to mention:
- Checked exceptions are being wrapped for us automatically;
- Whenever we insert a value in parameters or returning a value in a method, JShell automatically places the result in a variable.
For a short JShell tutorial go here:
P.S. "Snug as a bug in a rug" idiom means to be warm, cozy and comfortable
This chapter has aims to get you comfortable with Java 9, 10, 11 features.
All the code is provided in com/codecool/organismjava
directory.
Approximate speech:
-
Compile and run a single file in one line with Java 11.
- there is no need in
javac
command anymore - in order to try this feature out navigate to
organismjava
directory - type:
java CompileMe.java
- there is no need in
-
Java 11 brings out new features of String class, these features can be found in
strings
packagepublic String strip()
public String stripLeading()
public String stripTrailing()
public boolean isBlank()
public Stream<String> lines()
public String repeat(int count)
-
Additions to interfaces can be found in
InterfaceFeatures.java
class- private methods in interfaces are now possible with Java 9.
-
Features of immutable collections can be found in
ImmutableCollectionFeatures.java
class- creating an immutable collection became easier in Java 9.
-
Try with Resources improvement can be found in
trywithresources
package- with Java 9 it is possible to use resource reference in try with resources
-
New Optional features broke through with Java 9 and 11. Showcase methods can be found in
OptionalFeatures.java
classpublic void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
public Stream<T> stream()
public boolean isEmpty()
-
Stream API features of Java 9 can be found in
StreamFeatures.java
classdefault Stream<T> takeWhile(Predicate<? super T> predicate)
default Stream<T> dropWhile(Predicate<? super T> predicate)
-
New Type
var
can be found inVarType.java
class- Java 10 introduced a new type name available for our use
-
Here come Java 11 and new Files methods that can be found in
FilesFeatures.java
classpublic static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException
public static String readString(Path path) throws IOException
For more information about possible questions related to this section, check these links: