Nicolai Parlog 🇺🇦🕊️ Profile picture
Nov 3, 2021 12 tweets 12 min read Read on X
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

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Nicolai Parlog 🇺🇦🕊️

Nicolai Parlog 🇺🇦🕊️ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @nipafx

Sep 7, 2022
In two weeks, #Java19 will be released with some of Java's most anticipated features: virtual threads and structured concurrency! 🤩

To make the best use of virtual threads, servers/frameworks need to support them - here's a list of those that do (reply with those I missed). 🧵
📣 All of this is still experimental!

Virtual threads and the new APIs are (you need `--enable-preview` and the incubator module `jdk.incubator.concurrent` to use them) and so is support in these projects. You can help them mature over the next months by testing with your app.
The @JakartaEE server @TheApacheTomcat provided experimental support as a part of their 10.1.0-M16 release back in June, but there was a bug that broke HTTP/2 support if async IO was disabled. The unreleased 10.1.0-M17 will include a fix.

Details: tomcat.apache.org/tomcat-10.1-do…
Read 15 tweets
Mar 22, 2022
#Java18 comes out later today - what can you expect from Java's newest release? Here's a 🧵 with one tweet per feature - short and sweet.

For more details and links, read this blog post: nipafx.dev/inside-java-ne…
For questions, join our #JDK18 AMA later today. 👇🏾
Pattern Matching for `switch` (2nd preview)
openjdk.java.net/jeps/420

Changes:
* constant case labels must now appear before guarded patterns of the same type
* exhaustiveness checking is now more precise when sealed classes and generics mix

Another preview is expected in #JDK19.
Vector API (3rd incubation)
openjdk.java.net/jeps/417

Changes:
* support for ARM Scalar Vector Extension (SVE)
* performance for masked ops improved on architectures that support masking in hardware

Vector API will stay incubating until Valhalla's value/primitive types.
Read 14 tweets
Dec 21, 2021
#ProjectLoom's virtual threads will make high performance in concurrent systems attainable with much simpler code. But Loom aims for even more and wants to make the code clearer and more robust by introducing *structured concurrency*.

Here's what that's all about. 🧵
Important: This is about concurrency, not parallelism. See yesterday's thread 👇🏾 for a more detailed distinction, but the gist is that concurrency is about processing lots of tasks that the environment throws at your system at the same time, ideally with high throughput.
Concurrency code is often unstructured:

* splitting and joining concurrent subtasks in different methods/classes
* little support to compose error-handling, cancellation, … across subtasks
* threading structure is opaque to the runtime, which only sees independent threads
* …
Read 9 tweets
Dec 20, 2021
What's it called when multiple threads execute at the same time? Parallelism? Concurrency? 🤔 Is there a difference? (Spoiler: Yes!)

Let me explain in a few tweets, ripping off @pressron's #InsideJava blog post "On Parallelism and Concurrency". 🧵

inside.java/2021/11/30/on-…
Parallelism:
Taking a task and splitting it up, so multiple CPUs can compute partial solutions in parallel to solve the task in less wall-clock time.

Concurrency:
Having a number of tasks that need to be arranged in a way that solves as many of them per $time_unit as possible.
Some parallelizable tasks:

* sorting an array
* inverting a matrix
* rendering graphics

Some examples for concurrency:

* processing incoming web requests
* making outbound calls to DB and/or other web services
* observe file system for changes
Read 11 tweets
Dec 10, 2021
I'm no security expert and don't know how ubiquitous this vulnerability is, but if you use Log4J 2.x, you should probably update to 2.15.x and read these:

logging.apache.org/log4j/2.x/secu…
lunasec.io/docs/blog/log4…
Here's something else you can do until the updates are rolled out.
The other option until you've updated your dependency is this command line flag.
Read 4 tweets
Mar 16, 2021
Here are 11 improvements you get when updating to #Java16 later today: from records and type patterns to `Stream`-additions and Unix domain sockets, from creating installers to more performance and observability. 🧵👇🏾

(Longer form with tons of links: nipafx.dev/java-16-guide/)
#1 Records

Express in a single line that a type is just a collection of data without need for encapsulation and let the compiler do the rest:

record Range(int low, int high) { }

That results in almost the same API as the attached class. *drops mic* Image
#2 Type Pattern Matching

This is actually two-for-one:

* first step into pattern matching
* type patterns with `instanceof`

With a type pattern you check whether a variable is of a certain type and, if so, create a new variable of that type, so you can use it without casting. Image
Read 14 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(