coming in jdk 22 is a new feature about running a program that is supplied as multiple files so I'm going now in the every of my records and I'm returning my Json with my customized content if you're looking at what's the new main the new hello world you can see there that you just need to write a void Main in a Java class so I had my intention interface and I made it sealed because I wanted for the moment to limit what other classes can extend it my name is Anna currently working as a developer advocate in the ja plat from group team at Oracle I enjoy working with Java because let's recognize it's a beautiful language I've been working with Java for more than 12 years um and I also love kubernetes and what's happening in the cloud space I also enjoy working with security and you know combining different tools to make my demos and other applications s to life and um apart from that well I hope you will enjoy the demo from today um speaking of java we've seen this slide probably many times about the six month release Cadence of java and you know that nowadays every six months with every release of java we get features well a smaller amount of features like in the past when we need to wait for years for that for Java version to be delivered now we get them faster smaller chance for us to accommodate with what's coming new in terms of language features for also for us to not only just see what's coming new to try them and give feedback on them to see like how if our applications will work with these features and of course say that to the community say that to uh the Java Engineers that hey this probably doesn't cover my use case and that to be taken into consideration and of course when we talk about these features many of them fall in the chunks in these projects there's just a bunch of just a small portion of the jav projects that have been enrolled in the past years uh not all projects get updates of course um and well sometimes people already ask about what's going on with Valhalla when we're going to get Valhalla there's always this question um for Loom again there was a a little weit uh but all these wait and see of preview features incubator features and so on until the features are made final is for us all of us to take advantage of the stability of java of having also the backwards compatibility have the safety in it and have all the cases all of us being covered by it um and this slide doesn't have some other projects but uh they also make our lives better as well so I want to start this talk uh with a few with a nice quote from Brian GS from his talk in Belgium uh but this is a very good advice also for yourself when you're picking language features in your projects as well the reason well one first thing that you can think about when we're writing code well let's face it we spend little time writing code we spend more time thinking about what we're going to write but we also spend a lot of time reading that code so language features mean a lot for us because I mean for the future we and our colleagues are going to read that code and of course we need a thoughtful way to move forward um again when we're coding we also need to think about how that piece of code that we're writing is going to evolve in the future and moreover not only how it's going to evolve in the future but what's it compatibility with with other plans of Integrations and well there is another catch to this quote that I'm going to share a little later but just keep it in mind to be thoughtful when you're choosing features and language features when you're coding just don't pick them because they're beautiful but they're because they're useful to your project so let's start I want to ask you something um can you please raise your hand if in the audience are any Junior Jaa developers if you consider yourselves Junior Java developers so we have one two okay you can okay it's all about how you consider yourself so we have three uh Juniors that's great welcome um how many of you think of themselves like having medium Java knowledge okay that's surprising from some people good you see like kind of half room and senior experts in Java you know everything one two please don't be shy come on let's say equal to the menu um so as the seniors we're used to writing code um after let's say 12 years of java feels natural to write code it just flows right um and we know how to write code we we know how to test it all becoming very let's say automatic and normalized to us however those that are about to start with the language they feel it differently um because when you join a new Java project that's something that I always related in the past whenever I was joining a new project it was always like this well let's say the ID was there before but they were always telling me okay install the jdk x that the project is using then this is the project setup which was usually cumbersome related in some areas with the infrastructure and so on and uh well this is Java code and most of the Java projects that have maturity that reach production back in the day when I started they had a lot of code uh which is good because mature projects usually have a lot of code however if we're looking at this piece of code my some say that's not that much but we just have a simple hello world so from operational point of view I would say that you need for starting with Java download the jdk have an ID uh make sure that you are having a build tool because well we're working on real life projects and those are using a build tool and of course the Java code that uh if you're a junior that probably others have have coded on or if you're just joining the project others have coded on so in order to enhance the usability of working with Java there have been uh put there have been out there some tools to help and make our life nicer it comes to fast prototyping with Java one of them being jshell and totally recommend it uh to just if you want to write a few lines of code and see if it works and just not go to the ID make a new Java file and try everything just go nicely to the command line jshell and just drop it there um secondly since jdk 11 you can just say Java your hello world Java or however your name your file is called and that is being compiled and run for you and you get the hello world so no more Java C first and then Java and all this ceremony and coming in jdk 22 is a new feature about running Java programs multi running a program that is supplied as multiple files in just one go with Java minus class path star there and of course hello world Java so if hello world Java is provided as two files you can grun it nicely like this uh if you're trying by the way the jdk 22 Early Access build it can already Works um so it's pretty cool and simplified way of running Java right but still uh let's talk about beginning with Java the beginning still looks like this right so we need to know some Concepts if we want to write the hello world job the hello world here right we need to know about um access modifiers we need to know about what's that a class what's that a method why can't I put methods outside classes so you need to know all the ceremony of building just five lines of code you need to know all the ceremony of java to write uh more complex things to know what are the what's the static versus instance Behavior what are parameters what are return types and so on so in order to help the people that are just learning Java there's a new there's a Jeb Jeb 445 that thinks on how to simplify things for those that are learning Java but not only so and name classes and instance main methods is a preview uh feature in 21 gets its second preview in 22 by the way um and well if you're looking at what's the new main the new hello world you can see there that you just need to write a void Main in a Java class and you system out print line the hello world and that's it now this is not just the idea of eliminating U you know make a shorter syntax right it's not about shorter syntax it's about the fact that this simple program should actually contain the minimum that you need to write it right you don't you don't need the string arguments since you're not you know processing that further in your program um and to be to be honest when whenever we have a new idea and whenever want to try something we always start with simple methods we build up classes and inheritance and everything else at a at a bigger rate we're not just thinking of everything out of the box just from the beginning but this is also useful for us in general for writing scripts in Java so think of that as well it's not just J Junior developers that are helped but us as well when writing scripts in Java and I promise you the second part of the quote so here's the second part of the quote like don't do today um things don't do things today that you don't want to be compatible with tomorrow now this is some of you might say okay this is easy to say um it's always tough whenever when there's pressure on you uh to uh deliver and to put things in action however always think of tomorrow and the people that are going to come after you how are we going to work with that piece of code and in the context of this job there might be the question like okay so this is the new main however what if my program has two types of main because that one was the most simple way to write the void M but maybe there are other ways that I can you know truncate the old syntax so there's an invocation order with the Syntax for the new main so for example the traditional public static void main string ARS will have precedence if it's used in a Java class um next if you're having one static void main without the string arcs that's going to be evaluated second uh the third evaluation comes for avoid main string arcs so no static modifier there and last but not least the one that you saw earlier in my Slide the very very very very simplified version of void main please pay attention that all these are not private so don't make it private then it's not going to work so this is um a simplify way um nice language gift to start simpler with Java if you want to know more on this feature you can know more this feature uh by reading the two chps of course um there there's a very nice video done by my colleague Nikolai on how to script Java easily in 21 and Beyond and of course Java 22 unpacking that contains more about this feature but also about the other one that I mentioned the jet 458 okay so now we talk about Beginnings but we'll write most of the time uh Java programs that contain business logic right so let's go to something me with more meet so most of the time our developer Works requires to model the business logic of our software right there's always requirements coming to us from business uh Sim formulated in a uh I don't know more generic or more specified more specific uh manner so with that in mind let me bring to your attention a small application that I built for the jvm advant calendar so this is called the wrap App application and is uh sending gifts so the idea is pretty simple and the reason why I put the QR there is for you to scan it because we're going to use these requirements throughout the application so it's better like to have them on mobile if you want to refresh on the requirements since they're new so a center can do a nice gesture and send somebody a very nice gift right and a center can choose to send a nice postcard or or is very important a surprise depending on celebration and the surprise can be of different types it can be a coupon an experience I don't know like jumping with parachute or a present a physical present now a postcard does not have an Associated cost but all the other ones they do um and an online coupon has an expiry date while the others they don't a present can be placed inside a box which can have a cost as well and of course a sender can never give two postcards as a gift the system will pick up the first postcard and ship it to the receiver so these are some let's say minimal requirements for an application an imagined application we not I didn't use any framework for it now in real life probably you're going to pick a good framework to make this a reality but I just wanted to use a few simple language features just to you know prototype it fast prototype it so first thing I thought about my model and I was thinking like okay how I'm going to model this and I thought about what I have in my system so I have postcards I have surprises which can be of three types uh well all of them while postcards don't have a price the others have a price so whenever a Senter wants to send some of these to a receiver this feels like data and I need something to carry my data throughout my system but if I'm sending this request let's say to the system as Json I don't want to process Json and strings throughout all my system I want to make it into a model that's more closer to me so I thought of using records for postcard for example for just to start off and not just because well they're nice and they reduce boilet plate and uh they're a new cool feature but it also because well they're safer this realization um proceeding in records through the Constructor preventing forging of instances so that's one first design choice that I took secondly I thought about use more of the versatility of records and use record patterns because if some of you have been to the talk about the Java language Futures well they fuse a type test with conditionally uh a type test with conditionally extracting the record components so what you're seeing here you can see that I can deconstruct a record into its components in a more easier way compared to Java 16 and of course I can do it like this but I can also use type inference for buying dat binding two record components so let's look again at our system our system like this the class diagram that resulted from it um and having in mind the requirements that we initially had uh I thought okay so postcard coupon and experience and and present they're all good intentions from someone so I had my intention interface and I made it sealed because I wanted for the moment to limit what other classes can extend it and at the moment all I know from my business is that only these four types are permitted by my system I'll deal with the rest later so I'll make this sealed uh secondly I need to think about modeling so I said already that postcard is a record but besides that coupon experience and present are also records and last one gift because gifts can be either postcard or one coupon or one experience or one present so it feels like it's something nested there so gift was represented using nested record patterns and this is well composing pattern because while records are not just for representing simple like for representing the simple data like we saw earlier with postard but they also can be composed with nesting and it's more natural to my system to describe my system thirdly if I'm looking again to working with the nested record patterns um as of java 21 I can deconstruct I said earlier the record into its components so gift is it can be a postcard or an experience I can check it always and have the instance off against that but as I said earlier I can go even a little deeper and if for example I need access to the currency I can do that directly without looking and saying hey gift give me the postcard give so give me the experience give me the currency and of course last but not least use type INF ference for binding to record components so for example since I'm just using uh the currency here I'm not interested in representing the type of the rest of the record components and I just used VAR so we have one stage for Ming our system if you want to know more here here are some useful links and the je W 440 which by the way this feature is final so safe to kind think of it safe to use in production but it's final use it enjoy using it um another feature that came with Java 21 it was very much discussed was about string templates so string templates were thought to complement Java's existing literals and text blocks probably use text blocks so far but what they have special is that they couple a literal text with embedded expressions and template processors to produce specialized results this is a preview feature and as I said earlier from my sender I get a Json right I'm transforming that into something that's okay for my system that I can work with it's I make my new model but I need to send my model back I need to format the gift back right so maybe string templates can help me to format again to string I can use this it's a way so I can use a simple string template the SD that's given by by the jdk and just use the simple template processor the dot and of course the template where I can embed Expressions but I can also produce specialized format by using the template processor API and making my temp template to process strings but return gson right so you can always use the string template processor off to make a processing to process a string and of course return the Json uh and the first time that I implemented this and a simple way to implement this for Json is like in the slide so I'm just hanging the string template processor of string template and then I'm just returning a new Json object so we have now this in the our interface and of course the method as Json for each of our records so I'm going now in the every of my records and I'm just you know validating my J I'm returning my Json with my customized content and I can also Place expressions like the turn your operator to calculate the price that can happen as well in this in the templates I just don't need to calculate that beforehand to put it there so pretty much simplified looks like this and it's all very clean and nice so if you want to know more there's a newscast about how to interplate strings in Java 21 uh more on uh string templates in the jeps themselves there's getting a new there's a new a second preview in jk22 and of course there is an article on inside Java with the code that I showed you earlier and more around it but let's model a little further our system so now we know that our system has records is processing Json can return Json further but we need operations right so in order to have clean operations we're going to use pattern matching for switch um feature that was finalized also on jdk 21 je 441 and has many flavors so this one this feature was to made to expand the expressiveness and of the switch expressions and allowing them to have patterns in the case labels uh of course they can also treat the null hosti that we know about like you can have null in a case that's what we know for the switches of course secondly well they increase the safety of switch statements by requiring the pattern switch statements to cover all possible input values and last but not least as always very important for Java backwards compatibility so your previous switches are also safe to use they didn't affect that uh what's new with jdk 21 I don't want to cover all the changes that happen with pattern matching so with jdk 21 there were no more parenthesized patterns and you can use qualified inum constants in case uh La in uh case labels so what this means is that like for example a postcard can have fonts right and let's say font there are also two types they can you can have color and you can have weight for the font it can be bold uh stra it can be bold italic or regular uh and you want to treat those cases differently PR prior to jdk 21 you couldn't achieve the cases with W on enams now you can do this and elegantly and nice so these were the what the the changes that occurred uh for um pattern matching and switch only for 21 but this is just for the processing of the color I said we need more operations so if I am to think again to my requirements for processing the request and returning the gifts based on what the system was requiring a possible modeling of the system is to have an exhaustive switch that treats all the cases for my system yeah there're a lot but this you what you get um and you need to check of course for some edge cases when maybe somebody says Okay I want everything for free um and you need to say hey if you want for free you're just getting a postcard you know you don't trick the system um but for the other cases where you're wanting to send a postcard and a gift or or another surprise to someone you can just merge the two and happen there so this is the first date of the switch a lot of options and I think some of you already see some way to optimize this and that's very good um so let's optimize this if you want to know more for the moment about pattern matching I can recommend you to of course read the Jeb but moreover there are some uh nice writing from Brian GS about uh pattern matching in the Java object model and pattern patterns exhausted inconditional and reminder and of course the state of pattern matching from 2022 of Brian's so we want to let's say improve the last Improvement to give to our code because there were some things that were overlapping but that's very um very easy to do thanks to another uh preview in jdk 21 but becoming Final in jdk 22 and name and name patterns and variables to improve code mainten ability somebody these already might suggest that to you by the way this um Fe Fe comes in the context for the fact that is common for programs to just need only some of the components for further processing like we saw earlier right so for example the code we had earlier if we are to look at it can become this the reason being is because on the right side of the each case we don't always use everything that's on the left in the case so for example in case case of the gifts we can easily replace with underscore the ports that we're not using further right so this switch earlier gets simplified to less cases to treat and everything gets put together in this short sentence um so it's great that's finalizing jdk 22 but I suggest we give it a try starting with 21 as it would be F to give some time for others as well I would like to tell you to stay tuned for more to go and check the articles on dev. Java and to invite you to contribute to dev. Java as we welcome Community contributions from you to in writing better articles to help developers improve themselves uh I welcome you to also check inside Java where we publish it's like an aggregate of news what's going on with the latest Chups that are published for each new jdk release but also can be like examples or research by some J uh by some jdk engineers and all is aggregated by date author and tag tag is very useful because for example if you need any everything that has ever been aggregated there in garbage collection you get a lot of learning on garbage collection so and of course as a trusted source as those people that are contributing there they really TR try to do their best to to write uh helpful content but not only that but they are the experts in Java that right there and last but not least the youtube.com Java if you enjoy videos and uh yeah there we're trying to make a video format of the written part that is most of the time on dev. Java or inside. Java that was it from my side I want to welcome some questions these are the slides and there's the code so enjoy The Code by the way has a little bit more complexity but because it has some other cases treated but you're going to see most of it matching the what I showed you in the slides here so if any our questions are there welcoming them all clear okay thank [Applause] you