Java Platform Module System: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit
 
(8 intermediate revisions by 3 users not shown)
Line 67:
== Properties of modules ==
 
Modules are aused newto waygroup ofpackages groupingand codetightly control what packages belong to the public API. Contrary to [[JAR (file format)|Jar files]], modules explicitly declare which modules they depend on, and what packages they export.<ref>{{cite web
| url=http://openjdk.java.net/projects/jigsaw/spec/sotms/
| title=The State of the Module System
Line 99:
}}</ref> For example, the majority of the Java standard library is exported by the module <code>java.base</code>.
 
ModulesAs of Java 25, modules can themselves be imported, automatically importing all exported packages.<ref>{{Cite web|url=https://openjdk.org/jeps/494|title=JEP 494: Module Import Declarations (Second Preview)|website=openjdk.org}}</ref> This is done using <code>import module</code>. For example, <syntaxhighlight lang="Java" inline>import module java.sql;</syntaxhighlight> is equivalent to
<syntaxhighlight lang="Java">
import java.sql.*;
Line 106:
</syntaxhighlight>
Similarly, <syntaxhighlight lang="Java" inline>import module java.base;</syntaxhighlight>, similarly, imports all 54 packages belonging to <code>java.base</code>.
 
<syntaxhighlight lang="Java">
import module java.base;
 
/**
* importing module java.base allows us to avoid manually importing most classes
* the following classes (outside of java.lang) are used:
* java.text.MessageFormat
* java.util.Date
* java.util.List
* java.util.concurrent.ThreadLocalRandom
*/
public class Example {
public static void main(String[] args) {
List<String> colours = List.of("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet");
IO.println(MessageFormat.format("My favourite colour is {0} and today is {1,date,long}",
colours.get(ThreadLocalRandom.current().nextInt(colours.size())),
new Date()
));
}
}
</syntaxhighlight>
 
Modules use the following [[List of Java keywords|keywords]]:
Line 123 ⟶ 145:
{| class="wikitable plainrowheaders"
! scope="row" | {{code|java.base}}
| Defines the core APIs that form the foundation of the Java SE Platform.
Implicitly required by all modules and does not need to be declared with <code>requires</code> inside a module declaration.
|-
! scope="row" | {{code|java.compiler}}
Line 291 ⟶ 314:
|-
! scope="row" | {{code|jdk.xml.dom}}
| Defines the JDK's subset of the [[World Wide Web Consortium]] (W3C) [[Document Object Model]] (DOM) API not covered by Java SE. Exports packages outside of the <code>java</code> namespace (from <code>org.w3c.dom</code>).
|-
! scope="row" | {{code|jdk.zipfs}}
Line 318 ⟶ 341:
* [[Classpath]]
* [[Java class loader]]
* [[Precompiled header#Modules]], (C++ modules)]]
 
== References ==
Line 334 ⟶ 357:
 
[[Category:Java specification requests]]
[[Category:Modularity]]