Let's keep this in mind What's new in Java 9

This article is the first day article of SRA Advent Calendar 2017.

Java 9 release content

Hello! This is Sasaki from the Kansai Division. In September 2017, Java 9 was released. Java 8 got a lot of attention because of the contents that have a big influence on coding, such as Lambda expressions, default methods of interface, method references and addition of Optional. Thanks to that, even if you don't watch the latest Java information, it seems that many people know some new features about Java 8. However, regarding Java 9, people who are not actively collecting information do not seem to be aware of what kind of release was made, and for those who do not have time for the time being, one of the contents of Java 9 release that "Keep this alone" I would like to introduce the department. "This is all" here is my personal judgment, so there may be some disagreements, but in that case I would be grateful if you could supplement it with comments. (Other power application)

jshell You can now use the REPL tool jshell. jshell.JPG You can omit the semicolon at the end of the sentence, and you don't have to create a class or method to execute Java code. (Of course, you can create classes and methods) Also, in the above example, after creating a Random object, type " r.` `` "and press the TAB key to complete the method. In addition, there are many commands in jshell that start with "/ (slash)". Below are just a few

Javadoc search function

You can now use the search function in Javadoc. javadoc_search2.JPG You can perform an incremental search such as package name and class name by entering a keyword in the search window on the upper right. For package names and class names, I think that the search function of the browser has been able to do it to some extent, but you can also search for method names suddenly. javadoc_search3.JPG

Modular system

In fact, the main feature of Java 9 is the "module system". The modular system is quite esoteric when trying to understand it in depth, but I'll keep it to a minimum here. First, before the modular system, I will briefly explain the following two problems.

JAR hell JAR hell is a problem caused by Java applications relying on many JARs. It depends too many JARs to manage and you get a ClassNotFoundException at runtime, or you get an error because you have the same JAR file in different versions.

The problem of being overlooked

This "problem of having trouble seeing too much" was arbitrarily named by me, but in Java, the classes included in the JAR can be "seen". You can control access to classes such as private and public, but you need to make them public in order to be able to access them from other packages, and as a result, you will be able to access them from other JARs as well. Also, the package did not have class-like access control.

module-info.java The module system solves the above problems. To use the module system, place a file called "module-info.java" in the top directory of src.

src -- kazsharp.lambda_logging.jcl
     |                          |
     |                          -- LambdaLog4jLog.java
     |
     -- module-info.java

And in module-info.java, describe the dependent modules, the packages to be published, etc.

module-info.java


module <Module name> {
  requires <Dependent module name>;
  exports <Package name to publish>;
}

You can still access packages that are not exported within your module, but other modules can only access packages that are exported. Also, if multiple modules have the same package, an error will occur.

Transitional measures

I'm worried about the module system when using Java 9

That being said, there is a period called "transitional measures". Transitional measures include "anonymous modules" and "automatic modules", even if module-info.java is not placed

However, since it is a transitional measure, if you use Java 9, you should use module-info.java to make the appropriate settings.

So far, we have introduced the content that Java 9 should hold down at least this amount. You'll need to know more about module systems and other content to actually develop, but I hope this article will give you a chance to find out more about Java 9.

Recommended Posts

Let's keep this in mind What's new in Java 9
What's new in Java 8
What's new in Java 9,10,11
[Java Bronze] 5 problems to keep in mind
Java history in this world
What's new in Apache Camel 2.19.0
Let's use Twilio in Java! (Introduction)
I tried the new era in Java
Pleiades Eclipse 2020-03 release ~ Java 14 Let's try new features!
Set HTTP Keep Alive Timeout in Java HTTP Client
Let's create a super-simple web framework in Java
Things to keep in mind when committing to CRuby
Let's use Java New FileIO! (Introduction, for beginners)
Let's study Java
Partization in Java
Changes in Java 11
java1.8 new features
Pi in Java
FizzBuzz in Java
[Java] Let's declare variables used in the loop in the loop [Variables in the block]
[LeJOS] Let's program mindstorm-EV3 in Java [Environment construction part 2]
Things to keep in mind when adding war to dependency
Things to keep in mind when using if statements
This and that for editing ini in Java. : inieditor-java