Home > Java > javaTutorial > body text

What are the benefits of modules in Java 9?

WBOY
Release: 2023-09-15 10:09:02
forward
1410 people have browsed it

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>
Copy after login

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

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template