Skip to content

Instantly share code, notes, and snippets.

@islomar
Last active February 7, 2018 17:13
Show Gist options
  • Save islomar/b9cc7653a0c4a1cb0afd34c8d77ae806 to your computer and use it in GitHub Desktop.
Save islomar/b9cc7653a0c4a1cb0afd34c8d77ae806 to your computer and use it in GitHub Desktop.
# Java 8 and 9 notes
## Java 8
* In Java 1.8, parameters and local variables are final by default, you don't need to write it.
* Interfaces get static and default methods.
* Migrating to Java 8:
* https://www.jetbrains.com/help/idea/migrating-to-java-8.html
* https://javaspecialists.teachable.com/p/refactoring2j8
## Java 9
It's Java 9, not Java 1.9
### "Moving to Java 9: Better Design and Simpler Code " by Trisha Gee
https://www.safaribooksonline.com/library/view/moving-to-java/9780134857664/MJ9L_00_00.html
Example code: https://github.com/trishagee/sense
* **Getting ready for Java 9**
* JDeps: it helps you identify things you're using in Java 8 which are not accessible in Java 9.
* From inside your classes folder, having compiled with Java 8, run `$JAVA_HOME\bin\jdeps -jdkinternals -classpath=.` >> it searches if you're using something from JDK internal API that you should not (e.g. sun.misc.Base64Encoder)
* Compile your code on Java 9 to see if it's compatible.
* "_" is no longer a valid name (WTF, it was before!). It's because of lambdas.
* **The Java Platform Module System**
* Project Jigsaw: it's an umbrella project, it contains several JEP and JSR.
* Hide the API internals, make the JDK modular itself, JPMS (we can use it for modularizing it), jlink (it allows us to deploy our app in a much smaller "thing"), etc.
* A module is a collection of "things". It gives encapsulation. It sounds like a... nanoservice ^___^
* It disallows versio conflicts, since now you can depend only on the specific module that you're interested in.
* module-info.java
* Module path, not classpath: instead of -classpath, you see -p
* Module names are like package names
* By default, an empty module-info.java has an implicit `requires java.base`
* You need to export the package from a module if you want it to be used elsewhere.
* IntelliJ: you can visualize the module diagram
* JLink: use to create an ad-hoc image just with what we need
* Jlink is Java’s new command line tool which allows you to link sets of modules (and their transitive dependencies) to create a run-time image.
* https://blog.idrsolutions.com/2017/05/java-9-jlink-explained-in-5-minutes/
* $JAVA9_HOME/bin/jlink --modulepath $JAVA9_HOME/jmods:mlib:/home/islomar/workspace/poc-java9/target/production/one:/home/islomar/workspace/poc-java9/target/production/person --addmods com.islomar.mymodule --output mymodule-image
* `mymodule-path/bin/java --list-modules`
* Check the size: `du -sh mymodule-image`
* Automatic modules: those created for libraries which are not modularized yet.
* You can not cheat with reflection: you would need to explicitely allow it with, for example, `exports xxx to javafx.graphics`
* **Lesson 3: Reactive Programming with Java 9**
* Reactive Streams API
* Flow API ~ Reactive Streams API
* Lots of different implementations, e.g. Akka Streams, RxJava, Vert.x, Reactor.
* Publisher --(events/messages) --> (listens) Business logic (emits) --> (listens) Subscriber
* peek (Java8) ~ doOnEach(Java9)
* Reactive streams is about asynchronous messages, streams of messages which kind of continuous indefinetely (never ending data).
* Java 8 Streams <> Reactive Streams (RS)
* They don't have the same concurrency model.
* in RS you can combine two streams.
* in RS you can have multiple subscribers
* in RS there are several different implementations
* in RS you can reuse streams
* Back-pressure: when the publisher goes faster than the subscriber... very important to think about it!!
* **Lesson 4: JShell, the Java REPL**
* JShell: Read Eval Print Loop
* `$JAVA9_HOME/bin/jshell -v`
* You get tab completion and you don't need semicolons.
* It can be used from IntelliJ.
* **Lesson 5: API Changes in Java 9**
* Factory methods for Collections: e.g. List.of(), Set.of(), Map.of(), Map.ofEntries() >> it returns an immutable collection
* Milling Project Coin (minor improvements):
* e.g. private methods on interfaces, or the use of final values in try-with-resources.
* New methods on Stream API: takeWhile(), dropWhile()
* New methods on Optional: ifPresentOrElse(), or(), stream()
* Stack Walking API: you can walk through your error stack trace.
* Process API Updates: ProcessHandle.allProcesses(), etc.
* **Lesson 6: Other Interesting Changes**
* Multi release JAR files: you can have a JAR compatible with several Java versions (but only Java 9 onwards).
* The alternative compiled versions would go under /META-INF/versions/<version>
* You should use a build tool for doing it.
* On the MANIFEST.MF >> Multi-Release: true
* Looks like... porn :-/
* Updated Deprecation warnings: @Deprecated(since=x, forRemoval=true)
* Javadoc: you can generate HTML and it's searchable.
* Remove the JHat tool, deprecate the Applet API.
Current video: 33/36 (~90%)
### Interesting links for Java 9
* https://docs.oracle.com/javase/9/whatsnew/toc.htm#JSNEW-GUID-C23AFD78-C777-460B-8ACE-58BE5EA681F6
* https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/
* https://guides.gradle.org/building-java-9-modules/
## Docker and Java
https://jaxenter.com/nobody-puts-java-container-139373.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment