Modern Java - Top Features of Java 9 to 17

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Despite the 26 year history java is still being  actively developed and as for language so mature   the number of new features added with each release  is usually quite impressive. Putting in on top of a   six month release cycle - yes, every half year Java  gets a new version - we get a constantly evolving   rapidly changing language. In this episode we'll  go through the most interesting features added   to the language in the last few years between  Java 9 released in 2017 and Java 17 released in   September 2021. The classic switch we have in java  inherited its syntax from c plus and well it's not a very good syntax we've all been using it for a  long long time but it has its flaws here we have   a very simple switch we take a player's name and  depending on the name we print out his clap let   me see what name we put here we put lewandowski  so in this case we should see bayern here in the   terminal yes and we have bayern it seems all  right but the syntax of switch the switch we   have used for so long is not very clear we have  those breaks here and what happens if i remove   the break or i comment out this  one and this one let's run it again   now we have bayern pasje and  manchester united if we put messi here the output will be pedre and manchester united  this is because switch works in a way that it   finds the first matching case it executes the  statement which is associated with this case   and then it executes all the statements below  unless it finds a break instruction so we have to   put these breaks here to make it really work it's  not the best way it's not the best syntax because   it's very easy to make a mistake here  it's also not very clear when we look   at the code and creators of java also were  aware of that and that's why they decided   to change the switch in newer versions of java   and now let me close this and you can see here  that intellij is already suggesting me the change   it says that the switch statement can be  replaced with enhanced switch so let me press alt   enter replace with enhanced switch statement and  here we are here we have a new switch we have   several cases the first case lewandowski miller or  neuer and the instruction that should be executed   so we have an arrow here and then the instruction  we don't have any breaks here it's just a simple   construct we have several cases and if we have  a match we just execute what's associated with   that case we don't have that weird logic that  we execute the associated statement and all the   statements below so a very good change and this  is the switch i really recommend the next thing a   new instance off we have a very simple structure  here i have created this uh abstract file class   which is abstract i have also created two classes  that extend that abstract file this is music file   which has one method to play music and video  file that has a method play video so now we go   to the instance of example here we can see that  i have created a new variable abstract file i   gave it a type of an abstract file but i gave  it an instance of a music file so now here we   can use the instance of operator if abstract  file is actually an instance of a music file how did we do it before so what we usually did  was to cast this abstract file to the music file   and now we can use the method play music  which is a part of a music file class   this looks quite okay but it might be better  inside if we already know that abstract file   is actually a music file so why do we have to cast  in newer versions of java we don't need to cast it   anymore so what we do instead we do like this  so first part is the same we check if abstract   file is an instance of a music file and here we  give a name for a new variable if this is true   let's create a new variable called music file and  now this variable will be present inside our if   so just like this java is a statically typed  language so every time we initialize a new   variable we must declare its type but in java 10  a new feature called type inference was introduced   now instead of writing a type we can write var we  can give a name and we can initialize a variable   and a correct type will be inferred by  a compiler of course it works only when   we initialize a variable it won't work in a case  like this when we first declare a variable and we   want to initialize later it won't work it also  won't work for class fields so doing like this also won't work an interesting thing is that  var is not a new keyword it doesn't work like   the other keywords that are reserved that we can't  use them in a code we can do something like this   and it will still work of course it's  not the smartest way to write a code   but it's technically correct this is because of  the backward compatibility the creators of java   couldn't have added var as another keyword  because it could have masked some code   which already exists the feature itself might not  feel very impressive but of course i mean this uh   these examples are really really simple  there's not much of a difference between   indoor string and var but if we have longer  class names it might make a difference   the next feature records is something we know  from other programming languages under the name   data class a data class as the name suggests is a  class that holds data it doesn't contain any logic   it doesn't calculate anything it just holds the  data before records if you wanted to have a data   class in java you either had to write a lot of  code a lot of boilerplate code or you had to use   some library like lombok let's see how much code  i'm talking about here so let's create a class   call it a person and we'll have two fields here  private final string name and private final in h   of course we need a constructor so we generate  one on windows it's uh alt insert on mac os   it's command n we have this generate window  so i'm choosing constructor and here we have   a constructor but of course we need getters  for our fields so let's generate those name age okay already 19 lines  of code but it's not the end   we usually want to compare objects of such a  class so let's generate equals and hashcode like this okay it's getting bigger   and let's generate to string because we want to  have a good text representation of a of an object okay 42 lines how can we  achieve the same using records creating a new java class person record here  i'm choosing record and i'm giving the names   and types of these two fields string name and  h and that's all four lines of code how can we   use it in our code actually we use it as a  usual class it's it's it's almost the same   first let's create an object of class person my  name my age here person one and now let's use the record and that's it the only difference here is  how we access the fields in this case of   of a class we use our getter   in case of a record we just give the name of the  field and that's all let's compare once again this   is our class a lot of code here we have record  very short very concise piece of code no room for   error here and this is what makes records really  special and i think it's one of the best features   that was introduced in java in the last few years  the next feature handling multi-line strings   and this is how we've been doing this so  far so we have this string and we have a   lot of different characters inside we have those  slashes t slashes and it doesn't look very clear   when we look at the string we actually don't  know what it represents when we want to have an   indentation we have to explicitly say slash t when  we want to have end of a line we have to put slash   n when we want to have a quotation mark inside our  string we have to escape it using a slash but now   we can do everything much easier we can use a  new feature called text block and this is it we   just put our text inside triple quotation marks  and all formatting is preserved so now when i   run this we see that we have a very nicely looking  json if i remove this indentation and run it again   we see that it's gone so our string our text  looks exactly how we wrote it and this of course   leads to a code that is clear and easy to read  now we have a very interesting feature called   sealed classes this new approach gives us more  control over who can extend our classes let's   start with a simple example i will create a  new class code audio file and i will mark it as   an abstract class and now i want to  extend this audio file so i will create   and mp3 file that extends audio file so this  is the classic approach we create a class and   everyone can extend our class but now with sealed  classes we can explicitly tell who can extend   our class or who can implement our interface so  now we can add to the audio file a new modifier   called sealed and now intellij tells us  that there is an error there is a problem   because we need permits clause because we  need to give an information who can extend   our class so permits and i will give this mp3  file but it's not all here we have another problem   because all seals class subclasses must either be  final sealed or non-sealed so we go to mp3 file   and now we have to give it one of those  three modifiers sealed non-sealed or   final if we give sealed we need once again permits  here because we need to have subclasses of this   so i want now to give it a final modifier so  no one else can extend this class now if i gave   non-sealed everyone could extend  mp3 file but let's go back to   final if i wanted at some point create a  class that extends mp3 file like extended mp3 file i would need to extend mp3 file which is not  possible at the moment because this is the final   class so i now have two options i can do either  this non-sealed so now everyone can extend or   i can put sealed and now i can give permits here  and i can tell that extended mp3 file is allowed   to extend this class this mp3 file class but  now of course in extended improve mp3 file   i need to give a modifier and now   let let it be final and the same for interfaces  so let me create an interface called drive and i want it to be a sealed interface but  now of course we need to have sub classes   someone must implement this interface so let  me create a new class hdd drive which extends drive and here we have actually two issues   first is that no inter oh sorry no extent but  implements so first hdd drive is not allowed   in the sealed hierarchy because we need  to give a pyramid here permits hdd drive what else okay all sealed class subclasses must  either be final sealed or non-sealed so we go back   to hdd drive and here we put a modifier so let  it be non-sealed so this feature adds like a new   level of control we can explicitly tell who can  extend a class or who can implement an interface   for a long time initializing a collection in java  wasn't very convenient this is how we had to do it   with set so first we had to create a new set and  then we had to add all elements manually the same   with map also we created a map and then we had  to call put several times the same with lists but   here at least we had this syntax arrays as list  so we could use this static method and initialize   the list in one line this wasn't the best solution  though because we had to know that such method as   sllist exists in arrays class now all of this we  can do much much easier because we have of methods   and these are static factory methods to initialize  a collection so now instead of calling add or put   several times we just have list of set of map of  or map of entries much easier much more clear much   better the last feature for today a very cool  one called meaningful null pointer exceptions   i think everyone coding in java knows that pain  when we have a new pointer exception and we don't   know what it's about so i've created this very  simple class person class it only has two fields   name and age and i created an instance of this  class in our main class here by the way i'm using   this repo id an online compiler because this has  java 11 and java 11 didn't have those meaningful   no pointer exceptions yet so we will we'll see a  difference okay we have an instance of a person   class here and we call get name here and then  we call to lower case on this name in this case   everything will work because we have a name so we  can call to lowercase on the name and we can see   that the name in lowercase was displayed but now i  will comment out this one and i will use this code   so we can see that i don't provide a name here  i provide null so here this get name will return   null and to lower case will return a null pointer  exception it will throw a null pointer exception   so let's run it yes and we see that in line 7 we  have a new pointer exception but what happens if   i comment out this and run this this time person  class the instance of a person class is null so   not this is null but this let's run it and we  see that we have a null pointer exception in line   10 but we don't have any information where  exactly we got this new pointer exception   we don't know if it was here or here and this  is not the case in java anymore let's go back   to intellij we have exactly the same example  here i commented out these two lines i'm running and let's see cannot invoke string to  lower case because the return value of   meaningful new pointer exceptions person class  get name is null so now we know that null pointer   exception came from here from this get name  method and if i comment out this and i run this we see that person 3 is null  so we have this meaningful   null pointer exception now we know where to   look where we can fix the problem that's all for  today thanks for watching and have a good one
Info
Channel: Kamil Brzezinski
Views: 2,124
Rating: undefined out of 5
Keywords:
Id: zNaUasfC84Y
Channel Id: undefined
Length: 20min 18sec (1218 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.