Home >Java >javaTutorial >What are the benefits of modules in Java 9?

What are the benefits of modules in Java 9?

WBOY
WBOYforward
2023-09-15 10:09:021424browse

Java 9中模块的好处是什么?

One of the important features introduced in Java 9 is Modules. By using modules, we can divide our code into smaller components, called modules. This means that each module has its own responsibilities and declares its dependencies on other modules to work properly.

Here are the steps to create a modular project in Java 9: ​​

Initially, we can create a file called "module-info.java file and add it to the package (module) created for it. For example, if our package name is com.mycompany.mypackage, then the file will go to the same package (src/com.mycompany.mypackage/module-info.java). We can create modules by declaring "exports" and "requires" expressions.

If our module requires another module, we can write the following code

<strong>module com.tutorialspoint.greetings {
   requires org.tutorix;
}</strong>

To expose the module content, we can write the following code

<strong>module org.tutorix {
   exports org.tutorix;
}</strong>

Advantages of the module:

  • The module hides unnecessary strong> and internalDetailsVery safe to improve security. This means that modules can only access exported package contents , not all contents of other modules or public/internal API, so public in a module The content is not public to other modules.
  • Apps become smaller and faster because we can only use any modules we want.
  • Easy to deploy on small devices as memory requirements are very high and less.
  • Easy to supportSingle Responsibility Principle.
  • Easy to support less coupling between components.

The above is the detailed content of What are the benefits of modules in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete