Nicolai Parlog 🇺🇦🕊️ Profile picture
#Java enthusiast with a passion for learning and sharing - in posts, newsletters, videos, streams, talks… Developer Advocate at Oracle, organizer of @AccentoDev

Nov 3, 2021, 12 tweets

From compact record constructors to pattern matching, from generic wildcards to chaining predicates and comparators, from jpackage to jlink - here are 11 #Java tricks handpicked from dev.java. 🧵

(Musical version of this thread: )

0/11

A #record's *canonical* constructor has one arg per component but in its *compact* form, you don't have to list them. You also don't have to (in fact, can't) assign fields, just do the checks/reassignments you need, rest happens automatically.

More: dev.java/learn/using-re…

1/11

Serialization works great with #records, no black magic needed! The guaranteed presence of constructor parameters and accessors makes serialization work with the object model and makes it easy to create reliable serializable records.

More: dev.java/learn/using-re…

2/11

The command line tool #jpackage takes a whole Java app as input and produces a fully self-contained application image that includes your code, dependencies, and a Java runtime. The cool thing: Unlike jlink, your app doesn't need to be modular!

More: dev.java/learn/jpackage/

3/11

Speaking of #jlink, you can use it to create runtime images across OS (e.g. for Windows on a Linux server):

* download/unpack JDK for target OS
* use `jlink` binary from host OS
* to its module path, add the target OS JDK's `jmods` folder

More: dev.java/learn/creating…

4/11

The #break and #continue statements can be followed by a label. You can use this to stop the execution of an outer loop (with `break`) or skip to its next iteration (with `continue`).

(Usually, other approaches are more readable, though.)

More: dev.java/learn/control-…

5/11

When #pattern #matching, you can use the introduced variables right away to make additional checks by adding `&& $boolean_expression`. This is called a guarded pattern.

More: dev.java/learn/using-pa…
More on Twitter:

6/11

While `List<Integer>` doesn't extend `List<Number>`, you can create type relationships by throwing in #generic #wildcards:

* `List<? extends Integer>` extends `List<? extends Number>`
* `List<? super Number>` extends `List<? super Integer>`

More: dev.java/learn/wildcard…

7/11

Check out the #Predicate methods:

* use `and`, `or`, or `negate` to create boolean formulas
* invert a method reference with the static factory `not`
* create an equality check with a specific object by passing it to the static `isEqual`

More: dev.java/learn/combinin…

8/11

Same for #Comparator:

* to compare objects by an attribute, use `Comparator.comparing`
* to sort by several attributes, chain comparators with `thenComparing`
* reverse with `reversed`
* use `nullsFirst` or `nullsLast` to handle `null`

More: dev.java/learn/writing-…

Write #Java #scripts (on Unix-based OS):

* create a single source file
* add shebang line `#!/path/to/bin/java --source 17`
* make file executable with `chmod +x`
* rename file and drop `.java` file extension
* run it with `./file-name` 🏃🏾

More: dev.java/learn/launchin…

10/11

Launch #JShell with predefined scripts:

* `JAVASE` imports all Java SE packages to get started right away
* `PRINTING` defines `print`, `println`, and `printf` for easier printing

E.g.: `jshell JAVASE PRINTING`

More: dev.java/learn/jshell-t…

11/11

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling