Java 21 - Language Features and Beyond

Java
28 Apr 202429:20

Summary

TLDRこのビデオスクリプトでは、Java開発者向けにJDKの最新機能と改善点を紹介しています。Oracleの開発アドボケート、Annaは、Javaの6か月ごとのリリースケイデンスと新機能の利便性を強調し、特にJDK 21以降の簡素化されたメインメソッドやレコードの活用、パターンマッチングの強化について詳述しています。また、Javaの柔軟性と安全性、そして後方互換性を維持しながら、新しい言語機能を試し、フィードバックを提供することでコミュニティに貢献するよう呼びかけています。さらに、実用的な例として贈り物アプリの構築を通じて、Javaの最新言語機能を応用する方法も紹介しています。

Takeaways

  • 😀 Javaの新しい機能として、複数のファイルからなるプログラムを実行する機能がJDK 22で追加された。
  • 👩‍💻 Oracleの開発アドボケートとして働くAnnaは、Javaの美しさとクラウドやセキュリティとの組み合わせを楽しんでいる。
  • 📈 Javaは6ヶ月ごとにリリースされ、新機能が徐々に追加されている。これはフィードバックを取り入れることでより良い言語機能を提供する。
  • 🔍 ValhallaプロジェクトやLoomプロジェクトなどのJavaの進化には時間がかかるが、プレビュー機能を通じてユーザーは安定性と後方互換性を確保しながら新しい機能を試すことができる。
  • 📚 Brian GSのアドバイスでは、コードを書くときには言語機能を慎重に選んで、将来の互換性とコードの読みやすさを考慮するよう勧めている。
  • 🔑 Javaの新しいメイン関数の構文は、よりシンプルで、void mainを単純なJavaクラスに書くだけで良い。
  • 📦 JDK 22では、複数のクラスからなるプログラムを一つのコマンドで実行できるようになった。
  • 📝 新しい機能を学ぶ際には、JEP(Java Enhancement Proposals)や関連する記事、動画を参照する。
  • 🛠️ Java開発者は、JDK 21以降で提供されているレコード、パターンマッチング、文字列テンプレートなどの新機能を活用して、より簡潔で安全なコードを書くことができる。
  • 🔗 dev.java.netやinside.java、JavaチャンネルのYouTubeなどの信頼できる情報源を活用して、Java開発者コミュニティと繋がりながら情報を得ることができる。

Q & A

  • Java 22の新機能として紹介されたプログラム実行に関する変更とは何ですか?

    -Java 22では、複数のファイルからなるプログラムを一度に実行することができる新機能が導入されています。これは、Javaクラスパスに星号(*)を指定して、複数のクラスを含むJavaプログラムを実行する機能です。

  • AnnaさんはなぜJavaを楽しいと感じていると述べていますか?

    -AnnaさんはJavaが美しい言語であると認識しており、12年以上Javaを扱い、Kubernetesやクラウド空間での動きにも興味を持っていると述べています。また、セキュリティと異なるツールを組み合わせてデモや他のアプリケーションを活性化するのも楽しんでいると言っています。

  • Javaの6ヶ月ごとのリリースケイデンスにはどのような意味がありますか?

    -Javaの6ヶ月ごとのリリースケイデンスにより、新しいバージョンがより頻繁に提供され、開発者は新しい言語機能に早くアクセスできます。これにより、開発者は新しい機能を試してフィードバックを提供し、アプリケーションがそれらの機能とどのように連携するかを評価することができます。

  • ValhallaプロジェクトとLoomプロジェクトとはどのようなものですか?

    -ValhallaとLoomはJava言語に新しい機能を追加するプロジェクトです。ValhallaはJavaの性能を向上させるための機能を提供する一方で、Loomは並行性と仮想スレッドを扱うための機能を提供するプロジェクトです。これらのプロジェクトは、開発者がより効率的でスケーラブルなアプリケーションを作成できるように支援することを目的としています。

  • Brian GSからの引用とはどのようなアドバイスですか?

    -Brian GSからの引用は、コードを書くときには慎重に考え、将来もコードを読む人がいることを念頭に置く必要があるというアドバイスです。また、言語機能を選ぶ際には、それらがプロジェクトに有用である理由に基づいて選択する必要があると強調しています。

  • Java開発者として新しいプロジェクトに参加する際の一般的なプロセスは何ですか?

    -新しいJavaプロジェクトに参加する際には、JDKをダウンロードし、IDEを設定し、プロジェクトに必要なビルドツールを用意し、既存のJavaコードを理解することが一般的に求められます。また、プロジェクトの要件に応じてコードを書く必要があります。

  • jshellツールとは何ですか?どのように役立ちますか?

    -jshellはJavaの対話式シェルツールで、開発者が数行のコードを書くだけで迅速に実行し、結果を確認できるように支援します。これはIDEを使わずにコードをテストするのに役立ち、迅速なプロトタイピングに役立ちます。

  • Java 22での新しいmainメソッドの構文にはどのような変更がありますか?

    -Java 22では、新しいmainメソッドの構文として、void main(String[] args)を使わずに、より簡素なvoid main()を使用できるようになりました。これは、Javaクラス内で単純なプログラムを書く際に必要な最小限の構文を提供することを目的としています。

  • JEP 405とJEP 458とはどのような機能ですか?

    -JEP 405は、Javaのレコード機能を簡素化し、クラスとインスタンスメソッドの名前を省略できるようにする機能です。一方、JEP 458は、Javaスクリプトをより簡単に作成できるようにする機能です。これにより、Javaを用いたスクリプト作成が簡略化され、開発者の生産性が向上することが期待されます。

  • Java 21での文字列テンプレートの変更とは何ですか?

    -Java 21では、文字列テンプレートがより柔軟になり、リテラルテキストと埋め込み式の式を組み合わせて特殊な結果を生成することができます。これは、Javaの既存のリテラルとテキストブロックを補完する機能です。

  • パターンマッチングを使用したswitch式とは何ですか?

    -パターンマッチングを使用したswitch式は、JDK 21で導入された機能で、switch式にパターンを含めることができるようになりました。これにより、switch式の表現力が向上し、より安全で簡潔なコードを書くことができます。

  • 変数バインディングのためのパターンと名前付きパターンとは何ですか?

    -変数バインディングのためのパターンと名前付きパターンは、JDK 21でプレビューされ、JDK 22で正式に採用された機能です。これにより、switch式において、ケースの左側の変数を右側で使用しない場合に、簡潔にコードを書くことができます。

Outlines

00:00

🚀 Java新機能の紹介と開発者へのアドバイス

Oracleの開発者アドボケイト、AnnaはJavaの新機能と開発者へのアドバイスについて語ります。JDK 22では複数のファイルからなるプログラムを実行する機能が追加され、Javaクラスにvoid mainを書くだけで始められます。AnnaはJavaの6か月ごとのリリースケジュールと新機能の小さなチャンスについても触れています。また、ValhallaプロジェクトやLoomプロジェクトの進捗についても言及しています。最後に、Brian GSの言葉を引用し、言語機能を選ぶ際にはコードを書く時間よりもコードを読む時間の方が長いため、言語機能は非常に重要であるとアドバイスしています。

05:02

🌱 Java初心者向けの開発環境構築と新機能

AnnaはJava初心者向けに開発環境の構築方法と新機能について説明しています。JDKのダウンロード、IDEの準備、ビルドツールの導入、Javaコードの理解について詳述しています。JShellの使い方やJDK 11からのJavaプログラムの実行方法、JDK 22の新機能である複数のファイルを1つのコマンドで実行する機能についても紹介されています。さらに、Javaの基本概念を理解することの重要性と、JEP 345で提案された簡素化されたメインメソッドの構文についても触れています。

10:04

🎁 ギフトアプリケーションのモデル링とJavaのレコード機能

AnnaはJavaのレコード機能を使ってギフトアプリケーションをモデル化する方法を紹介しています。システム要件に基づいてポストカード、クーポン、体験、プレゼントを表現し、それらをレコードとして扱うことでデータの安全性と表現力を高めると説明しています。また、レコードパターンと密封型のインターフェースを使ってシステムの柔軟性と限定性を両立する方法も紹介されています。このセクションではJavaの新しい機能を実際のアプリケーション開発にどのように適用するかが具体的に示されています。

15:07

🔍 レコードと文字列テンプレートを使ったデータの扱い

AnnaはJavaのレコードと文字列テンプレートを使ってデータモデルを操作し、JSON形式でデータを送受信する方法について説明しています。レコードの安全性と簡便性、文字列テンプレートによる柔軟な文字列操作、そしてそれらを組み合わせたデータフォーマットの変換方法について詳述しています。JEP 405とJEP 410の機能がどのように役立つか、実際のコード例とともに解説されています。

20:08

🔧 パターンマッチングとswitch式の拡張

Java 21でfinalizeされたパターンマッチングとswitch式の拡張についてAnnaが解説しています。switch式にパターンを追加することで表現力と安全性の向上、null値の扱い方、新しいパターンでのswitch式の記述方法について紹介されています。さらに、Java 21でのパターンマッチングの変更点や、enum型の扱い方、exhaustive switchの重要性についても触れています。

25:09

🛠 Java開発におけるコードの最適化とメンテナンス性

AnnaはJava 21と22でのパターンマッチングの進化とコードの最適化について話しています。新しいnameとnameパターン変数を使ってコードのメンテナンス性を高める方法について説明し、具体的なコード例を紹介しています。また、dev.java、inside.java、JavaのYouTubeチャンネルなどのリソースを紹介し、開発者コミュニティへの貢献を呼びかけています。

Mindmap

Keywords

💡JDK

JDKとはJava Development Kitの略で、Javaプログラミング言語の開発と実行に必要なツール群を提供するソフトウェアパッケージです。ビデオでは、JDKの最新バージョンであるJDK 22について紹介しており、新しい機能や改善点を探求しています。例えば、「coming in jdk, 22 is a new feature」という文脈でJDK 22の新機能について話されています。

💡Java

Javaは広く使われるオブジェクト指向プログラミング言語で、高い互換性と安全性を持ちます。ビデオではJavaの言語機能や最新の進化について議論しており、開発者にとって魅力的な要素として紹介されています。例えば、「I enjoy working with Java because let's recognize it's a beautiful language」というフレーズでJavaの美しさと開発者の情熱が語られています。

💡records

Java 14で導入されたrecordsは、データクラスのような機能で、フィールドとそのフィールドに対するイミュータブルなクラスを作成するのに便利です。ビデオでは、recordsを使ってデータモデルを簡素化し、コードの安全性を高める方法が説明されています。例えば、「I thought of using records for postcard」という文脈でrecordsを使ってポストカードのモデルを定義する例が示されています。

💡pattern matching

pattern matchingはJava 16から導入され、switch文の表現力を向上させる機能です。ビデオでは、pattern matching for switchという機能を使って、より表現力豊かなswitch文を作成し、コードの安全性と簡潔さを向上させる方法が紹介されています。例えば、「pattern matching for switch」というフレーズで、switch文におけるパターンマッチングの活用方法が議論されています。

💡string templates

string templatesはJava 21で導入された機能で、文字列リテラルと埋め込み式を組み合わせて柔軟性のある文字列処理を可能にします。ビデオでは、string templatesを使って複雑な文字列処理を簡潔に行う方法が説明されています。例えば、「string templates were thought to complement Java's existing literals」という文脈で、文字列テンプレートがJavaの既存のリテラルを補完する機能として紹介されています。

💡sealed

sealedクラスはJava 15で導入され、他のクラスがそのクラスを拡張できる範囲を制限する機能です。ビデオでは、sealedを使えばシステムの設計をより簡潔かつ安全に保つ方法が紹介されています。例えば、「I made it sealed because I wanted for the moment to limit what other classes can extend」というフレーズでsealedクラスの利点が説明されています。

💡JSON

JSONはJavaScript Object Notationの略で、データ交換用の軽量なデータフォーマットです。ビデオでは、JavaプログラムがJSONデータを処理し、必要に応じてJSON形式で応答することができる機能が紹介されています。例えば、「I'm returning my Json with my customized content」というフレーズで、JSONデータをカスタマイズして返す方法が議論されています。

💡main method

main methodはJavaプログラムのエントリーポイントです。ビデオでは、Java 21と22でのmain methodの新しい構文について説明しており、より簡潔な方法でmainメソッドを定義できるようになった点に注目しています。例えば、「you just need to write a void Main in a Java class」というフレーズで、新しいmainメソッドの構文が紹介されています。

💡JShell

JShellはJavaの対話式シェルで、コマンドラインからJavaコードを直接実行・テストすることができるツールです。ビデオでは、JShellを使って迅速にJavaコードをプロトタイプ作成することができると紹介されています。例えば、「one of them being, jshell」というフレーズで、JShellがJava開発における迅速なプロトタイピングに役立つツールであることが説明されています。

💡cloud space

cloud spaceはクラウドコンピューティングの分野を指し、ビデオではJavaがクラウド環境でどのように役立つかが議論されています。スピーカーはJavaとクラウド空間での働きを楽しんでいると語っており、Javaがクラウド技術とどのように調和しているかについて触れています。例えば、「I also love kubernetes and what's happening in the cloud space」というフレーズで、クラウド空間におけるJavaの活用が語られています。

Highlights

JDK 22 introduces a new feature for running programs supplied as multiple files.

The new 'hello world' in Java can be written with just a void main method in a class.

Anna, a developer advocate at Oracle, discusses the benefits of Java's six-month release cycle.

Java's new features are designed to be tried and given feedback by the community.

Project Valhalla and Loom are mentioned as ongoing Java projects with preview features.

The importance of being thoughtful when choosing language features for coding is emphasized.

JShell is recommended for fast prototyping with Java.

JDK 11 introduced the ability to compile and run Java programs directly from the command line.

JDK 22 allows running Java programs with multiple files using 'java -classpath *'.

JEP 405 proposes simplifying Java for beginners by allowing 'void main' in a class.

The new main method simplifies the traditional 'public static void main(String[] args)'.

The importance of maintaining compatibility for future code evolution is discussed.

Pattern matching for switch expressions in JDK 21 expands expressiveness and safety.

String templates in Java 21 are a preview feature for specialized text processing.

Records in Java provide a safer way to model data and reduce boilerplate code.

Nested record patterns and type inference simplify complex data modeling in Java.

The 'wrap App' example demonstrates using new Java features for a gift-giving application.

The use of sealed interfaces in Java helps limit what classes can extend the interface.

JDK enhancements aim to improve code maintainability and simplify complex operations.

The presentation concludes with an invitation to contribute to dev.java and check inside Java and Java YouTube channel.

Transcripts

play00:00

coming in jdk

play00:02

22 is a new feature about running a

play00:05

program that is supplied as multiple

play00:08

files so I'm going now in the every of

play00:10

my records and I'm returning my Json

play00:13

with my customized content if you're

play00:16

looking at what's the new main the new

play00:18

hello world you can see there that you

play00:21

just need to write a void Main in a Java

play00:23

class so I had my intention interface

play00:26

and I made it sealed because I wanted

play00:28

for the moment to limit what other

play00:30

classes can extend

play00:33

it my name is Anna currently working as

play00:36

a developer advocate in the ja plat from

play00:38

group team at Oracle I enjoy working

play00:41

with Java because let's recognize it's a

play00:43

beautiful language I've been working

play00:45

with Java for more than 12 years um and

play00:49

I also love kubernetes and what's

play00:51

happening in the cloud space I also

play00:52

enjoy working with security and you know

play00:55

combining different tools to make my

play00:58

demos and other applications s to life

play01:02

and um apart from that well I hope you

play01:07

will enjoy the demo from today um

play01:11

speaking of java we've seen this slide

play01:14

probably many times about the six month

play01:16

release Cadence of java and you know

play01:19

that nowadays every six months with

play01:21

every release of java we get features

play01:25

well a smaller amount of features like

play01:27

in the past when we need to wait for

play01:28

years for that for Java version to be

play01:31

delivered now we get them faster smaller

play01:34

chance for us to accommodate with what's

play01:37

coming new in terms of language features

play01:39

for also for us to not only just see

play01:42

what's coming new to try them and give

play01:44

feedback on them to see like how if our

play01:46

applications will work with these

play01:49

features and of course say that to the

play01:52

community say that to uh the Java

play01:54

Engineers that hey this probably doesn't

play01:57

cover my use case and that to be taken

play01:58

into consideration

play02:00

and of course when we talk about these

play02:03

features many of them fall in the

play02:05

chunks in these projects there's just a

play02:09

bunch of just a small portion of the jav

play02:11

projects that have been enrolled in the

play02:12

past years uh not all projects get

play02:15

updates of

play02:17

course um and well sometimes people

play02:21

already ask about what's going on with

play02:23

Valhalla when we're going to get

play02:24

Valhalla there's always this question um

play02:27

for Loom again there was a a little

play02:31

weit uh but all these wait and see of

play02:34

preview features incubator features and

play02:37

so on until the features are made final

play02:39

is for us all of us to take advantage of

play02:42

the stability of java of having also the

play02:44

backwards compatibility have the safety

play02:46

in it and have all the cases all of us

play02:49

being covered by

play02:51

it um and this slide doesn't have some

play02:54

other projects but uh they also make our

play02:57

lives better as

play02:58

well

play03:01

so I want to start this talk uh with a

play03:04

few with a nice quote from Brian GS from

play03:07

his talk in

play03:09

Belgium uh but this is a very good

play03:11

advice also for yourself when you're

play03:14

picking language features in your

play03:15

projects as well the reason well one

play03:19

first thing that you can think about

play03:21

when we're writing code well let's face

play03:23

it we spend little time writing code we

play03:26

spend more time thinking about what

play03:27

we're going to write but we also spend a

play03:30

lot of time reading that code so

play03:33

language features mean a lot for us

play03:35

because I mean for the future we and our

play03:38

colleagues are going to read that code

play03:41

and of course we need a thoughtful way

play03:43

to move

play03:44

forward um again when we're coding we

play03:47

also need to think about how that piece

play03:49

of code that we're writing is going to

play03:51

evolve in the future and moreover not

play03:55

only how it's going to evolve in the

play03:57

future but what's it compatibility with

play03:59

with other plans of

play04:01

Integrations and well there is another

play04:05

catch to this quote that I'm going to

play04:07

share a little later but just keep it in

play04:09

mind to be thoughtful when you're

play04:11

choosing features and language features

play04:13

when you're coding just don't pick them

play04:15

because they're beautiful but they're

play04:16

because they're useful to your

play04:18

project so let's

play04:21

start I want to ask you something um can

play04:25

you please raise your hand if in the

play04:27

audience are any Junior Jaa developers

play04:30

if you consider yourselves Junior Java

play04:32

developers so we have one

play04:35

two okay you can okay it's all about how

play04:39

you consider yourself so we have three

play04:42

uh Juniors that's great

play04:44

welcome um how many of you think of

play04:46

themselves like having medium Java

play04:51

knowledge okay that's surprising from

play04:54

some people good you see like kind of

play04:57

half room and senior experts in Java you

play05:01

know everything one two please don't be

play05:05

shy come

play05:08

on let's say equal to the menu um so as

play05:13

the seniors we're used to writing code

play05:17

um after let's say 12 years of java

play05:19

feels natural to write code it just

play05:21

flows right um and we know how to write

play05:25

code we we know how to test it all

play05:27

becoming very let's say automatic and

play05:30

normalized to us however those that are

play05:32

about to start with the

play05:35

language they feel it

play05:37

differently um

play05:40

because when you join a new Java project

play05:43

that's something that I always related

play05:44

in the past whenever I was joining a new

play05:46

project it was always like this well

play05:48

let's say the ID was there before but

play05:51

they were always telling me okay install

play05:53

the jdk x that the project is using then

play05:58

this is the project setup which was

play06:00

usually cumbersome related in some areas

play06:03

with the infrastructure and so on and uh

play06:07

well this is Java code and most of the

play06:09

Java projects that have maturity that

play06:11

reach production back in the day when I

play06:14

started they had a lot of

play06:16

code uh which is good because mature

play06:19

projects usually have a lot of code

play06:21

however if we're looking at this piece

play06:23

of code my some say that's not that much

play06:27

but we just have a simple hello world so

play06:30

from operational point of view I would

play06:33

say that you need for starting with Java

play06:35

download the jdk have an ID uh make sure

play06:38

that you are having a build tool because

play06:41

well we're working on real life projects

play06:42

and those are using a build tool and of

play06:46

course the Java code that uh if you're a

play06:48

junior that probably others have have

play06:50

coded on or if you're just joining the

play06:52

project others have coded on so in order

play06:55

to enhance the usability of working with

play06:58

Java there have been uh put there have

play07:00

been out there some tools to help and

play07:03

make our life nicer it comes to fast

play07:05

prototyping with Java one of them being

play07:08

jshell and totally recommend it uh to

play07:12

just if you want to write a few lines of

play07:13

code and see if it works and just not go

play07:16

to the ID make a new Java file and try

play07:18

everything just go nicely to the command

play07:20

line jshell and just drop it

play07:24

there um secondly since jdk 11 you can

play07:29

just say Java your hello world Java or

play07:32

however your name your file is called

play07:35

and that is being compiled and run for

play07:36

you and you get the hello world so no

play07:39

more Java C first and then Java and all

play07:42

this

play07:44

ceremony and coming in jdk

play07:48

22 is a new feature about running Java

play07:52

programs multi running a program that is

play07:55

supplied as multiple files in just one

play07:59

go with Java minus class path star there

play08:03

and of course hello world Java so if

play08:05

hello world Java is provided as two

play08:07

files you can grun it nicely like this

play08:10

uh if you're trying by the way the jdk

play08:13

22 Early Access build it can already

play08:16

Works um so it's pretty cool and

play08:18

simplified way of running Java

play08:21

right but

play08:24

still uh let's talk about beginning with

play08:27

Java the beginning still looks like this

play08:29

right so we need to know some Concepts

play08:32

if we want to write the hello world job

play08:34

the hello world here right we need to

play08:36

know about um access modifiers we need

play08:39

to know about what's that a class what's

play08:40

that a method why can't I put methods

play08:43

outside classes so you need to know all

play08:45

the ceremony of

play08:47

building just five lines of code you

play08:50

need to know all the ceremony of java to

play08:52

write uh more complex things to know

play08:55

what are the what's the static versus

play08:56

instance Behavior what are parameters

play08:59

what are return types and so on so in

play09:02

order to help the people that are just

play09:05

learning

play09:07

Java there's a new there's a Jeb Jeb

play09:10

445 that thinks on how to simplify

play09:14

things for those that are learning Java

play09:16

but not only so and name classes and

play09:19

instance main methods is a preview uh

play09:22

feature in 21 gets its second preview in

play09:25

22 by the way um and well

play09:31

if you're looking at what's the new main

play09:33

the new hello world you can see there

play09:36

that you just need to write a void Main

play09:38

in a Java class and you system out print

play09:41

line the hello world and that's it now

play09:44

this is not just the idea of

play09:46

eliminating U you know make a shorter

play09:49

syntax right it's not about shorter

play09:51

syntax it's about the fact that this

play09:54

simple program should actually contain

play09:56

the minimum that you need to write it

play09:58

right you don't you don't need the

play09:59

string arguments since you're not you

play10:01

know processing that further in your

play10:04

program um and to be to be honest when

play10:07

whenever we have a new idea and whenever

play10:09

want to try something we always start

play10:11

with simple methods we build up classes

play10:14

and inheritance and everything else at a

play10:18

at a bigger rate we're not just thinking

play10:20

of everything out of the box just from

play10:22

the

play10:23

beginning but this is also useful for us

play10:26

in general for writing scripts in Java

play10:28

so think of that as well it's not just J

play10:31

Junior developers that are helped but us

play10:33

as well when writing scripts in

play10:36

Java and I promise you the second part

play10:40

of the quote so here's the second part

play10:42

of the

play10:42

quote like don't do today um things

play10:46

don't do things today that you don't

play10:48

want to be compatible with tomorrow now

play10:52

this is some of you might say okay this

play10:53

is easy to

play10:54

say um it's always tough whenever when

play10:58

there's pressure on you

play11:00

uh to uh deliver and to put things in

play11:03

action however always think of tomorrow

play11:06

and the people that are going to come

play11:07

after you how are we going to work with

play11:09

that piece of

play11:10

code and in the context of this job

play11:14

there might be the question like okay so

play11:16

this is the new main however what if my

play11:19

program has two types of main because

play11:23

that one was the most simple way to

play11:25

write the void M but maybe there are

play11:27

other ways that I can you know

play11:29

truncate the old syntax so there's an

play11:32

invocation order with the Syntax for the

play11:34

new main so for example the traditional

play11:38

public static void main string ARS will

play11:40

have precedence if it's used in a Java

play11:42

class um next if you're having one

play11:46

static void main without the string arcs

play11:48

that's going to be evaluated

play11:51

second uh the third evaluation comes for

play11:54

avoid main string arcs so no static

play11:56

modifier there and last but not least

play12:00

the one that you saw earlier in my Slide

play12:02

the very very very very simplified

play12:04

version of void main please pay

play12:06

attention that all these are not private

play12:08

so don't make it private then it's not

play12:10

going to work so this is um a simplify

play12:15

way um nice language gift to start

play12:18

simpler with Java if you want to know

play12:21

more on this feature you can know more

play12:24

this feature uh by reading the two chps

play12:27

of course um there there's a very nice

play12:29

video done by my colleague Nikolai on

play12:32

how to script Java easily in 21 and

play12:36

Beyond and of course Java 22 unpacking

play12:39

that contains more about this feature

play12:41

but also about the other one that I

play12:43

mentioned the jet

play12:47

458 okay so now we talk about Beginnings

play12:50

but we'll write most of the time uh Java

play12:54

programs that contain business logic

play12:56

right so let's go to something me with

play12:58

more

play13:00

meet so most of the time our developer

play13:04

Works requires to model the business

play13:05

logic of our software right there's

play13:07

always requirements coming to us from

play13:10

business uh Sim formulated in a uh I

play13:14

don't know more generic or more

play13:16

specified more specific uh

play13:19

manner so with that in mind let me bring

play13:24

to your attention a small application

play13:26

that I built for the jvm advant

play13:29

calendar so this is called the wrap App

play13:32

application

play13:34

and is uh sending gifts so the idea is

play13:38

pretty simple and the reason why I put

play13:40

the QR there is for you to scan it

play13:43

because we're going to use these

play13:44

requirements throughout the application

play13:45

so it's better like to have them on

play13:47

mobile if you want to refresh on the

play13:48

requirements since they're new so a

play13:52

center can do a nice gesture and send

play13:54

somebody a very nice gift right and a

play13:58

center can choose to send a nice

play14:01

postcard or or is very important a

play14:04

surprise depending on celebration and

play14:07

the surprise can be of different types

play14:10

it can be a coupon an experience I don't

play14:12

know like jumping with parachute or a

play14:16

present a physical present now a

play14:18

postcard does not have an Associated

play14:20

cost but all the other ones they

play14:23

do um and an online coupon has an expiry

play14:27

date while the others

play14:29

they don't a present can be placed

play14:31

inside a box which can have a cost as

play14:33

well and of course a sender can never

play14:36

give two postcards as a gift the system

play14:39

will pick up the first postcard and ship

play14:42

it to the receiver so these are some

play14:46

let's say minimal requirements for an

play14:48

application an imagined application we

play14:51

not I didn't use any framework for it

play14:53

now in real life probably you're going

play14:55

to pick a good framework to make this a

play14:57

reality but I just wanted to use a few

play15:01

simple language features just to you

play15:03

know prototype it fast prototype it so

play15:06

first

play15:07

thing I thought about my model and I was

play15:10

thinking like okay how I'm going to

play15:12

model this and I thought about what I

play15:16

have in my system so I have postcards I

play15:18

have surprises which can be of three

play15:21

types uh well all of them while

play15:24

postcards don't have a price the others

play15:26

have a price so

play15:29

whenever a Senter wants to send some of

play15:31

these to a

play15:33

receiver this feels like data and I need

play15:36

something to carry my data throughout my

play15:38

system but if I'm sending this request

play15:40

let's say to the system as Json I don't

play15:43

want to process Json and strings

play15:46

throughout all my system I want to make

play15:48

it into a model that's more closer to me

play15:51

so I thought of using records for

play15:54

postcard for example for just to start

play15:56

off and not just because well they're

play15:59

nice and they reduce boilet plate and uh

play16:02

they're a new cool feature but it also

play16:05

because well they're safer this

play16:09

realization um proceeding in records

play16:11

through the Constructor preventing

play16:13

forging of instances so that's one first

play16:17

design choice that I took

play16:20

secondly I thought about use more of the

play16:23

versatility of records and use record

play16:26

patterns because if some of you have

play16:29

been to the talk about the Java language

play16:32

Futures well they fuse a type test with

play16:35

conditionally uh a type test with

play16:37

conditionally extracting the record

play16:38

components so what you're seeing here

play16:42

you can see that I can deconstruct a

play16:44

record into its

play16:46

components in a more easier way compared

play16:49

to Java

play16:51

16 and of

play16:53

course I can do it like this but I can

play16:56

also use type inference for buying dat

play16:58

binding two record components

play17:02

so let's look again at our system our

play17:07

system like this the class diagram that

play17:09

resulted from

play17:10

it um and having in mind the

play17:12

requirements that we initially had uh I

play17:15

thought okay so postcard coupon and

play17:18

experience and and present they're all

play17:21

good intentions from someone so I had my

play17:23

intention

play17:24

interface and I made it sealed because I

play17:27

wanted for the moment to

play17:29

limit what other classes can extend it

play17:32

and at the moment all I know from my

play17:33

business is that only these four types

play17:36

are permitted by my system I'll deal

play17:38

with the rest later so I'll make this

play17:41

sealed uh

play17:43

secondly I need to think about modeling

play17:46

so I said already that postcard is a

play17:48

record but besides that coupon

play17:51

experience and present are also

play17:54

records and last one gift because gifts

play17:58

can be either

play18:00

postcard or one coupon or one experience

play18:04

or one present so it feels like it's

play18:07

something nested there so gift was

play18:09

represented using nested record patterns

play18:13

and this is well composing pattern

play18:17

because while records are not just for

play18:18

representing simple like for

play18:20

representing the simple data like we saw

play18:21

earlier with postard but they also can

play18:24

be composed with nesting and it's more

play18:27

natural to my system to describe my

play18:30

system

play18:31

thirdly if I'm looking again to working

play18:34

with the nested record

play18:35

patterns um as of java

play18:39

21 I can deconstruct I said earlier the

play18:41

record into its components so gift is it

play18:44

can be a postcard or an experience I can

play18:46

check it always and have the instance

play18:48

off against that but as I said earlier I

play18:51

can go even a little

play18:53

deeper and if for example I need access

play18:57

to the currency I can do that directly

play18:59

without looking and saying hey gift give

play19:02

me the postcard give so give me the

play19:04

experience give me the

play19:06

currency and of course last but not

play19:08

least use type INF ference for binding

play19:10

to record components so for example

play19:13

since I'm just using uh the currency

play19:16

here I'm not interested in representing

play19:17

the type of the rest of the record

play19:19

components and I just used

play19:22

VAR so we have one stage for Ming our

play19:25

system if you want to know more here

play19:28

here are some useful links and the je W

play19:31

440 which by the way this feature is

play19:34

final so safe to kind think of it safe

play19:37

to use in production but it's final use

play19:40

it enjoy using

play19:43

it um another feature that came with

play19:45

Java 21 it was very much discussed was

play19:48

about string templates so string

play19:50

templates were thought to complement

play19:52

Java's existing literals and text blocks

play19:55

probably use text blocks so far but what

play19:57

they have special is that they couple a

play19:59

literal text with embedded expressions

play20:02

and template processors to produce

play20:03

specialized results this is a preview

play20:08

feature and as I said earlier from my

play20:11

sender I get a Json right I'm

play20:13

transforming that into something that's

play20:17

okay for my system that I can work with

play20:19

it's I make my new model but I need to

play20:20

send my model back I need to format the

play20:23

gift back right so maybe string

play20:27

templates can help me to format again to

play20:29

string I can use this it's a way so I

play20:33

can use a simple string template the SD

play20:36

that's given by by the

play20:39

jdk and just use the simple template

play20:42

processor the dot and of course the

play20:44

template where I can embed

play20:47

Expressions but I can also produce

play20:51

specialized

play20:52

format by using the template processor

play20:55

API and making my temp template to

play20:58

process strings but return gson right so

play21:03

you can always use the string template

play21:04

processor off to make a processing to

play21:07

process a string and of course return

play21:09

the

play21:10

Json uh and the first time that I

play21:14

implemented this and a simple way to

play21:15

implement this for Json is like in the

play21:18

slide so I'm just hanging the string

play21:19

template processor of string template

play21:22

and then I'm just returning a new Json

play21:24

object so we have now this in the our

play21:28

interface and of course the method as

play21:31

Json for each of our records so I'm

play21:35

going now in the every of my records and

play21:37

I'm just you know validating my J I'm

play21:39

returning my Json with my customized

play21:43

content and I can also Place expressions

play21:45

like the turn your operator to calculate

play21:47

the price that can happen as well in

play21:49

this in the templates I just don't need

play21:51

to calculate that beforehand to put it

play21:53

there so pretty much simplified looks

play21:56

like this and it's all very clean and

play22:00

nice so if you want to know more there's

play22:03

a newscast about how to interplate

play22:06

strings in Java 21 uh more on uh string

play22:10

templates in the jeps themselves there's

play22:12

getting a new there's a new a second

play22:14

preview in

play22:15

jk22 and of course there is an article

play22:18

on inside Java with the code that I

play22:20

showed you earlier and more around

play22:24

it but let's model a little further our

play22:27

system so now we know that our system

play22:28

has records is processing Json can

play22:31

return Json further but we need

play22:33

operations right so in order to have

play22:36

clean

play22:37

operations we're going to use pattern

play22:39

matching for switch um feature that was

play22:42

finalized also on jdk 21 je

play22:46

441 and has many

play22:49

flavors

play22:51

so this one this feature was to made to

play22:54

expand the expressiveness and of the

play22:57

switch expressions and allowing them to

play23:00

have patterns in the case

play23:03

labels uh of course they can also treat

play23:06

the null hosti that we know about like

play23:08

you can have null in a case that's what

play23:10

we know for the switches of course

play23:13

secondly well they increase the safety

play23:16

of switch

play23:17

statements by requiring the pattern

play23:19

switch statements to cover all possible

play23:22

input

play23:23

values and last but not least as always

play23:26

very important for Java backwards

play23:28

compatibility so your previous switches

play23:30

are also safe to use they didn't affect

play23:33

that uh what's new with jdk 21 I don't

play23:37

want to cover all the changes that

play23:39

happen with pattern matching so with jdk

play23:41

21 there were no more parenthesized

play23:43

patterns and you can use qualified inum

play23:46

constants in case uh La in uh case

play23:51

labels so what this means is that like

play23:53

for example a postcard can have fonts

play23:56

right and let's say font

play23:58

there are also two types they can you

play24:00

can have color and you can have weight

play24:02

for the font it can be bold uh stra it

play24:05

can be bold italic or

play24:07

regular uh and you want to treat those

play24:09

cases differently PR prior to jdk 21 you

play24:14

couldn't achieve the cases with W on

play24:17

enams now you can do this and elegantly

play24:20

and nice so these were the what the the

play24:22

changes that occurred uh for um pattern

play24:26

matching and switch only for

play24:28

21 but this is just for the processing

play24:31

of the color I said we need more

play24:33

operations so if I am to think again to

play24:36

my requirements for processing the

play24:41

request and returning the gifts based on

play24:44

what the system was requiring a possible

play24:46

modeling of the system is to have an

play24:48

exhaustive switch that treats all the

play24:51

cases for my system yeah there're a lot

play24:54

but this you what you get um and you

play24:57

need to check of course for some edge

play24:59

cases when maybe somebody says Okay I

play25:02

want everything for free um and you need

play25:05

to say hey if you want for free you're

play25:07

just getting a postcard you know you

play25:08

don't trick the system um but for the

play25:11

other cases where you're wanting to send

play25:14

a postcard and a gift or or another

play25:18

surprise to someone you can just merge

play25:20

the two and happen there so this is the

play25:23

first date of the switch a lot of

play25:26

options and I think some of you already

play25:27

see some way to optimize this and that's

play25:30

very

play25:31

good um so let's optimize this if you

play25:35

want to know more for the moment about

play25:36

pattern matching I can recommend you to

play25:39

of course read the Jeb but moreover

play25:42

there are some uh nice writing from

play25:44

Brian GS about uh pattern matching in

play25:46

the Java object model and pattern

play25:48

patterns exhausted inconditional and

play25:50

reminder and of course the state of

play25:53

pattern matching from 2022 of

play25:56

Brian's so we want to let's say improve

play26:00

the last Improvement to give to our code

play26:03

because there were some things that were

play26:06

overlapping but that's very um very easy

play26:10

to do thanks to another uh preview in

play26:13

jdk 21 but becoming Final in jdk 22 and

play26:17

name and name patterns and variables to

play26:19

improve code mainten ability somebody

play26:22

these already might suggest that to you

play26:24

by the

play26:25

way this um Fe Fe comes in the context

play26:29

for the fact that is common for programs

play26:31

to just need only some of the components

play26:34

for further processing like we saw

play26:36

earlier right so for example the code we

play26:40

had earlier if we are to look at it can

play26:43

become this the reason being is because

play26:47

on the right side of the each case we

play26:51

don't always use everything that's on

play26:54

the left in the case so for example in

play26:57

case case of the gifts we can easily

play27:00

replace with underscore the ports that

play27:03

we're not using further right so this

play27:06

switch earlier gets simplified to less

play27:10

cases to treat and everything gets put

play27:13

together in this short

play27:15

sentence

play27:18

um so it's great that's finalizing jdk

play27:21

22 but I suggest we give it a try

play27:24

starting with

play27:25

21 as it would be F to give some time

play27:28

for others as

play27:30

well I would like to tell you to stay

play27:32

tuned for more to go and check the

play27:35

articles on dev. Java and to invite you

play27:37

to contribute to dev. Java as we welcome

play27:39

Community contributions from you to in

play27:42

writing better articles to help

play27:45

developers improve

play27:47

themselves uh I welcome you to also

play27:49

check inside Java where we publish it's

play27:51

like an aggregate of news what's going

play27:53

on with the latest Chups that are

play27:55

published for each new jdk release but

play27:58

also can be like examples or research by

play28:01

some J uh by some jdk engineers and all

play28:05

is aggregated by date author and tag tag

play28:08

is very useful because for example if

play28:09

you need any everything that has ever

play28:11

been aggregated there in garbage

play28:13

collection you get a lot of learning on

play28:15

garbage collection so and of course as a

play28:18

trusted source as those people that are

play28:20

contributing there they really TR try to

play28:23

do their best to to write uh helpful

play28:26

content but not only that but they are

play28:28

the experts in Java that right there and

play28:31

last but not least the youtube.com Java

play28:33

if you enjoy videos and uh yeah there

play28:36

we're trying to make a video

play28:38

format of the written part that is most

play28:41

of the time on dev. Java or inside. Java

play28:46

that was it from my side I want to

play28:48

welcome some questions these are the

play28:49

slides and there's the code so enjoy The

play28:53

Code by the way has a little bit more

play28:55

complexity but because it has some other

play28:58

cases treated but you're going to see

play29:00

most of it matching

play29:03

the what I showed you in the slides here

play29:06

so if any our questions are there

play29:08

welcoming them all

play29:11

clear okay thank

play29:13

[Applause]

play29:18

you

Rate This

5.0 / 5 (0 votes)

Related Tags
Java開発JDK21JDK22言語機能コードサンプル初心者向け開発ツールパターンマッチレコード文字列テンプレート
Do you need a summary in English?