Catch up on new features from Java 7 to Java 9 at once

Introduction

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.

Major new features in Java 8

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.

Lambda expression

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");
	}
}

Type annotation

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).

Major new features in Java 9

Next are the changes from java8 to java9. Perhaps the biggest addition is modularization. In addition, there seem to be many functional improvements.

Modular "Project Jigsaw"

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.

SnapCrab_コマンド プロンプト - jshell_2017-12-17_15-14-15_No-00.png

private method on interface

Private methods can now be added to the functions added in java8 that allow default methods and static methods to be described in interface.

Stream API function added

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.

at the end

It contains a lot of other changes, but this time we've rushed to see the major changes.

Recommended Posts

Catch up on new features from Java 7 to Java 9 at once
New features from Java7 to Java8
java1.8 new features
I touched on the new features of Java 15
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
6 features I missed after returning to Java from Scala
Introducing New Relic to Java apps running on Heroku
Migration from Cobol to JAVA
Connect from Java to PostgreSQL
Java 14 new features that could be used to write code
From Ineffective Java to Effective Java
Java Converts disparate character codes to the same character code at once
Notes on building Kotlin development environment and migrating from Java to Kotlin
[Java] From new project creation to automatic test / build / deployment realization
[Java] How to update Java on Windows
Java 9 new features and sample code
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
Notes on migrating from CircleCI 1.0 to 2.0
The road from JavaScript to Java
[Java] Conversion from array to List
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (2/3)
[Java] How to retrieve the parameters passed from html on the server side
[Java] Flow from introduction of STS to confirmation of dynamic page on localhost (1/3)
How to check Java installed on Mac
Kick ShellScript on the server from Java
Convert from java UTC time to JST time
How to switch Java versions on Mac
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
Steps to register Java files on GitHub
Git commands that new engineers should look back on [updated from time to time]