Chaper 12 - Modules Flashcards

1
Q

What does the Java Platform Module System (JPMS) do?

A
  • Groups code at a higher level than packages
  • Developers can declare what packages are available outside the module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the special file called needed for using modules?

A

module-info.java
This file contains the module declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 6 benefits of using modules?

A
  1. Better access control
    You can specifically allow a package to be accessed by a certain package.
  2. Clearer dependency management
  3. Custom Java Builds
    You can create a java runtime that only needs a small part of JDK, so you won’t need to load the full 150MB JDK.
  4. Improved security
  5. Improved performance
    Because the program starts op in smaller java packages (/modules) it improves startup time and has a lower memory requirement.
  6. Unique package enforcement
    Modules specify exposed packages, so Java can ensure that each package comes from only one module and avoid confusion about what needs to run
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What naming conventions do modules follow?

A

The naming conventions from a package. so a Module name should be: food.eat.module

and NOT EatModule

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the command line format when to run to compile your module?

A

javac –module-path <module-path> -d <directory> <module-path>/module-info.java</module-path></directory></module-path>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the command line syntax to access Directory for class files?

A

-d{dir}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the command line syntax to access the module path?

A

-p <module_path> // --module-path <module_path></module_path></module_path>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the command line format when you want to run a module?

A

java – module-path <module_path> ---module <module_name>/<package_name>.<class_name></class_name></package_name></module_name></module_path>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the command line syntax to access the module name?

A

-m <module_name> Or --module <module_name></module_name></module_name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the command line syntax to access the module path?

A

-p <path> OR --module-path <module_path></module_path></path>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which modules are present on the module path?

A

only named and automatic modules

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

is this a legal module name:
com.amazing.movierental

A

Yes, it is. small letters only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

is this a legal module name:
com.amazing.$movierental

A

Yes it is, allow to start a module and package with ‘$’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

is this a legal module name:
com.amazing.1movierental

A

No, you can’t start a module, or package-, name with a number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

is this a legal module name:
com.amazing.movie-rental

A

No, you are not allowed to use a ‘-‘

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

is this a legal module name:
com.amazing.movie_rental

A

yes, you are allowed to use an underscore

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

True or false. The module system has 3 phases:
- compile-time
- link-time
- runtime

A

True;
“link time”, is an optional phase between the two in which a set of modules can be assembled and optimized into a custom run-time image. The benefit of this phase is that the size of the resultingapplication is reduced because only those classes that are actually used are included in the application artifacts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which statement would you need to have in the module-info of a Java Swing based desktop application?

A

requires java.desktop;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does “exports <package_name>" do in the module-info.java file?</package_name>

A

It makes a package available outside it’s own module.

If we don’t add the exports statement it is only possible to run that package from the command line within it’s own package.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does “requires <package_name>" do in the module-info.java file?</package_name>

A

It makes clear that <package_name> (from an other module) is needed to run this module.</package_name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How can you export a package only to specific package?

A

“exports <package_name> to <module_name>;"</module_name></package_name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Is a PRIVATE method available within the module and/or outside the module?

A

in the module: available but only in the class
outside the module: no access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Is a PACKAGE-PRIVATE method available within the module and/or outside the module?

A

in the module: Available in the package
outside the module: no access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Is a PROTECTED method available within the module and/or outside the module?

A

in the module: only available within package or to subclasses
outside the module: available to subclasses IF package is exported

25
Q

Is a PUBLIC method available within the module and/or outside the module?

A

in the module: available for all classes
outside the module: available IF package is exported

26
Q

What does “requires transitive <package_name>" do in the module-info.java file?</package_name>

A

it makes sure it able to import al needed packages also if it’s via-via.

e.g.

See image page 677

27
Q

is it possible to state the following in your module-info.java file?
requires zoo.animal.talks;
requires transitive zoo.animal.talks;

A

No, it won’t compile because you require two times the same thing

28
Q

With what technique does java allow you to open a package at runtime?

A

reflection

29
Q

What is reflection?

A

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or “introspect” upon itself, and manipulate internal properties of the program. For example, it’s possible for a Java class to obtain the names of all its members and display them

30
Q

What is the syntax to open an entire module?

A

open module <module.name> {
}</module.name>

  • whole module = open -> specific package = opens
31
Q

What syntax do you use to open an specific package in the module-info.java file?

A

module <module.name> {
opens <package.name.name>
}</package.name.name></module.name>

  • whole module = open -> specific package = opens
32
Q

What class helps you to find any classes that implement a service provider interface?

A

the ServiceLoader class. It looks likes this:

ServiceLoader<object> loader = ServiceLoader.load<Object.class>; Eventually you can itterate over this list to see what classes implement a serviceprovider interface.</Object.class></object>

33
Q

What is the Service provider interface?

A

An interface or abstract class that acts as a proxy or an endpoint to the service.
If the service is one interface, then it is the same as a service provider interface.

Service and SPI together are well-known in the Java Ecosystem as API.

34
Q

What does the Service provider do?

A

A specific implementation of the SPI. The Service Provider contains one or more concrete classes that implement or extend the service type.

35
Q

What does the Service Locator do?

A

It loads the implementations of the service. At the heart of the Service Provider Interface is the ServiceLoader class. This has the role of discovering and loading implementations lazily. It uses the context classpath to locate provider implementations and put them in an internal cache.

36
Q

What does the Consumer do?

A

The module that uses the service?

37
Q

In what build-in module is Swing located?

A

java.desktop

38
Q

In what build-in module is Logging located?

A

java.logging

39
Q

what is the prefix of java api build-in modules?

A

java.

40
Q

what is the prefix of jdk build-in modules?

A

jdk.

41
Q

What is the command line to describe a module?

A

java -p <module_path> --describe-module <module_name>
or
java -p<module_path> -d <module_name></module_name></module_path></module_name></module_path>

42
Q

what is the command line to get a list of all available modules?

A

java –list-modules

43
Q

What is command line to get all available modules in specific folder

A

java <module_path> --list-modules</module_path>

44
Q

What kind of insights does the jdeps command gives you?

A

It gives information about dependencies within a module. It really takes a look at the code in the module. This is different from describing a module

45
Q

What are named modules?

A
  • modules that contain a module-info.java file
  • where the name is in this module file.
  • This modules appear in the module path
46
Q

What are automatic modules?

A
  • appears in the module path
  • but does NOT contain a module-info.java file
  • but get’s treated like a module
  • the module name is automatically generated

this automatically exports all packages? (p 701)

47
Q

How does the name of an automatic module get determined?

A

based on the MANIFEST.MF file. This file is in every jar file.

this manifest file contains info about the jar like the Java version, and the manifest version. Like kev-value pairs

48
Q

What are unnamed modules?

A
  • Appear on the class path – and NOT the module path
  • ## it does not contain an module.info.java file
49
Q

True or False – code on the class path can access the module path

A

true

50
Q

True or False –code on the module path can access the class path?

A

false

51
Q

True or false – modules with circular dependencies will compile and work fine.

A

False, modules with circular dependencies won’t compile

52
Q

With automatic modules. How does the jar file name converted to the module fie name?

A
  1. dashes will be converted to dots
  2. file extensions will be ignored
53
Q

What happens when the jar’s manifest contains the automatic-module-name (for converting the name automatically?

A

the name of the Jar file will be completely ignored.

54
Q

True / False – Modules allows a sealed class and it’s direct subtypes to be members of different packages.

A

True, Normally, the direct subclasses of a seal class must reside in the same package as the package of the sealed class. However, if a sealed class belongs to a named module, it is allowed to list classes from different packages of the same module in its permits clause.

55
Q

True / False – If a module wants to read another module but only temporarily, it can request such access using command line options.

A

true, a command like this could achieve that:

–add-reads moduleA=moduleB implies that moduleA wants to read all exported packages of moduleB.

56
Q

What does the command line:
–jdk-internals do?

A

List usage of internal api’s

57
Q

What are 3 thing the Java Module System achieves?

A
  1. It encapsulates the packages.
  2. It provides a reliable mechanism to let components of an application specify their dependencies on each other
  3. It provides a way to specify services and declare dependence on such services.
58
Q

What is the classpath?

A

Classpath is an environment variable that tells the Java Virtual Machine (JVM) where to find classes and resources during runtime.

59
Q
A