I've been doing front-end development with TypeScript for the past two years. I used to do server-side development using Java, but the last version I used was Java 7. Java 9 was released in September 2017, and it feels like it's completely left behind, so from Java 7 to Java 9 I will try to catch up on the changed points at once.
Java 8 was released in March 2014. Java 8 adds more concise code writing features such as lambda expressions and Stream APIs. New in Java 8 is that the code is smarter and Java is a bit more sophisticated.
Java is finally able to use lambda expressions.
java7
//Use anonymous class
Arrays.sort(array, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
});
In Java8, you can write using the arrow operator as follows. There are many ways to write a lambda expression, but I'll omit it here.
java8
//You can use lambda expressions
Arrays.sort(array, (s1, s2) -> s1.getAge() - s2.getAge());
You can also define a method that allows the user to use a lambda expression by writing a method that takes a function interface as an argument.
Stream API A convenient API for handling arrays etc. has been added. Processing that used for etc. up to Java 7 can now be described concisely in the method chain.
java7
String[] list = new String[] {
"ABC", "CDE", "EFG"
};
//Use Java7 for statement
String target = "";
for (String str : list) {
if (str.contains("C")) {
target += str;
}
}
java8
Optional<String> result = Arrays.stream(list).filter(s -> s.contains("C")).reduce((v1, v2) -> v1 + v2);
Optional Optional is an API for handling null well. You will be able to wrap null and write what to do if it might be null.
For example, up to Java 7, the code that was null checked as follows
java7
String str1 = "hoge";
if (str1 != null) {
System.out.println(str1);
}
If you use Optional, you can write neatly as follows.
java8
Optional<String> str2 = Optional.ofNullable("hoge");
str2.ifPresent(x -> System.out.println(x));
Also, the following code
java7
String str1 = "hoge";
if (str1 != null) {
System.out.println(str1);
} else {
System.out.println("null");
}
You can write it neatly as follows.
java8
String str2 = Optional.ofNullable("hoge").orElse("null");
System.out.println(str2);
Date and Time API With java8, a new java.time package has been added, and various ISO8601 based date and time classes have been added. The minimum API to remember is:
New API | Overview |
---|---|
LocalDateTime | Date and time without time zone |
ZonedDateTime | Date and time with time zone |
DateTimeFormatter | Formatter. Equivalent to SimpleDateFormat |
interface You can now have a default implementation for interface. You can write methods and static methods as follows. The default method can be overridden in the implementation class.
java8
public interface IDefaultSample {
default void Print(String value) {
System.out.println(value);
}
public static void Hello() {
System.out.println("hello");
}
}
Up to java7, annotations can only be used to declare classes and methods, but from java8 It can now also be used for types (of variables and generics).
Next are the changes from java8 to java9. Perhaps the biggest addition is modularization. In addition, there seem to be many functional improvements.
It is now possible to set dependencies, public settings, version settings, etc. in units called modules by grouping multiple classes together.
It seems that module definition can be easily done by writing a file called "module-info.java". I referred to the program of here.
In module-info.java, declare the module and the package to be published as follows. Here, the org.astro module is declared to publish the org.astro package.
** Description of Declaration ** --module: Module declaration --exports: Specify packages to publish
java:src/org.astro/module-info.java
module org.astro {
exports org.astro;
}
java:src/org.astro/org/astro/World.java
package org.astro;
public class World {
public static String name() {
return "world";
}
}
Here's how to specify which modules it depends on. Because the com.greetings module uses the org.astro module in its inner class This declaration is required.
** Description of Declaration ** --Requires: Specify the dependent module
java:src/com.greetings/module-info.java
module com.greetings {
requires org.astro;
}
java:src/com.greetings/com/greetings/Main.java
package com.greetings;
import org.astro.World;
public class Main {
public static void main(String[] args) {
System.out.format("Greetings %s!%n", World.name());
}
}
Details can be found at here.
JShell The jshell, which is a REPL of java, is installed. REPL is to read ** R ** (Read), evaluate ** E ** (Eval), and repeat ** P ** (Print) output ** L ** (Loop). Some are included in other languages.
To use it, pass the path to the JDK or go to the JDK and execute the jshell command.
Now you can immediately execute the Java syntax as follows: I can't think of a lot of uses, but it might be a good place to try a little code.
Private methods can now be added to the functions added in java8 that allow default methods and static methods to be described in interface.
The takeWhile and dropWhile methods have been added to the Stream API added in java8.
Reactive Streams A Framework that adopts the Publish / Subscribe model has been added to realize asynchronous streams. It is explained in detail in this article of ITpro.
It contains a lot of other changes, but this time we've rushed to see the major changes.
Recommended Posts