Java New Features - Java 9, Java 10, Java 11, Java 12, Java13, Java 14, Java 15

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back do you know there is a new java release that is coming out every six months this has been a recent development and because of this you'd see that there are two java releases every year in this section let's focus on understanding the different java versions how versioning is done and also let's learn some of the important features in these java versions let's get started the initial version of java was called jdk 1.0 and this was released way back right this is almost two and a half decades back january 1996. there were subsequent releases of jdk 2 3 and 4 and after that the major release was j2sc 5.0 this was done in september 2004 as you can see in here there are five releases in almost eight years so from 1996 to 2004 there were there were five releases only the next major release as far as i'm concerned is java sc8 this was released in march 2014 and as far as i'm concerned this is the most important java release because this is the release which brought in functional programming in an earlier section we looked at functional programming before java 8 there was no functional programming in java java 8 brought in functional programming constructs like lambda streams method references and all that wonderful stuff java sc9 was released in september 2017 and again you can see that there are four releases in 13 years from 2004 to 2017 we moved from five to nine so there are about four releases in almost 13 years and starting java sc10 which was released in march 2018 java switched to time-based release versioning instead of trying to decide these are the features which would go in and release the version after the features are ready java has decided that we would do a release every six months and whatever features are ready that would be part of that specific release so starting java sc10 is when you would see a number of java releases coming in so javasc 11 came in september 2018 java sc2l march 2019 and so on and so forth one of the latest releases right now is java sc 16 which was released in march 2021 you can see that from march 2018 to march 2021 we have already went through six different releases one of the important things to remember is that as we switch to buy yearly releases the innovation in each of the releases will be very less if you looked earlier there are huge differences between each of the java releases however if you look at javasc 10 verses 11 verses 12 versus 13 the number of new features that are being introduced are far less because these releases are coming in every 6 months one another thing java is doing is to release long term support versions every three years so java sc 11 is the last long term support release the next long term support release would be java sc 17 oracle generally recommends you to use the long term support releases for production because they'll have support for a very long time the idea behind this step is to give you a 10 000 feet overview over java versioning as you can see there are a number of new java versions that are being released off late i just wanted to give you a little bit of background into what's happening with java versioning i'll see you in the next step where we'd be talking about some of the important new features that were introduced in different java versions i'll see you in the next step welcome back instead let's look at some of the important java new features which were introduced across different java releases the idea is to give you an overview of some of the older releases and the newer releases of java as well one of the first releases of java that was very important is j2ac 5.0 this is where enhanced for loop generics enums and autoboxing were introduced into java language we looked at all these in earlier sections and all these features were introduced in java 5 or j2ac 5.0 the next important release for java is java 8. as far as i am concerned this is one of the most important java releases because this is where functional programming was introduced before java 8 there was no functional programming in java you cannot do lambdas or streams i was waiting for functional programming construct in java for a long time and finally in march 2014 with java 8 we got all the functional programming features lambda streams static references and all that wonderful stuff i love functional programming and i really loved the java 8 release java 82 is also brought in static methods in interface in addition to having method definitions for those methods that you want to expose from an interface you can also have static method definitions in an interface java sc9 was one another important java release where modularization of java was introduced the entire jdk is now modularized into multiple modules not only that you can also build your applications in a modularized way we will look at modularization in depth in a later video the next important feature which was introduced in java sc10 is local variable type inference whenever we write any program there are a lot of local variables which we create inside our methods some of the times the variable declaration the type declaration for a specific variable can get really complex with java 10 java can simplify that for you java can automatically infer the type of a local variable we would look at the details behind local variable type inference a little later the next important feature is switch expressions we looked at switch statements in an earlier section in addition to using switch as a statement starting java 14 you can also use switch as an expression what do you think would be the advantages of doing that we'll discuss that in a later video the next important feature is text blocks if i would want to write a complex piece of text having multiple lines having double quotes single quotes and special characters in java is very very difficult text blocks makes it very simple this is introduced in java 15. the next important feature is the record classes we have created a number of java beans during the last few sections for example the customer bean over here in the customer bean we would want to create three member variables we would want to create a constructor we want to have data method setter methods tostring and we might want to implement hashcode and equals method as well you want to do this in most of the bins in java 16 a new feature called record classes is introduced which makes it very very easy to create classes like this you can create the entire class like this with three lines of code we would look at record classes a little later as well in addition to these improvements in almost every java versions there are new methods which have introduced new apis which are introduced and almost every version of java has performance improvements especially focusing on garbage collection we create a number of objects in our program and when the references for those objects are no longer valid java would automatically garbage collect them java uses a number of algorithms to do this garbage collection activities across java releases there are a number of improvements to have garbage collection is done so you'd see that most of the java releases have some or other changes with respect to garbage collection and performance the idea behind this step was to give you a high level overview of the important new features which were released in a few older releases and the newer releases of java i'm sure you're having an interesting time and i'll see you in the next step welcome back starting this step let's look at some of the new apis which are introduced into java during the latest releases of java before we get started let's create a new project so i'll say new java project and i would want to create this with the name java new api features make sure that you choose the latest execution environment and click next let's not worry about java modules i'll uncheck create module info.java file and i'll say finish once the project is created let's go ahead and create a new file i'll call this i want to create a new class and i would want to call this you want to play with a few copy of methods i'll call this copy of api run and in the package i would enter com dot in 28 minutes dot api dot a this first api that we are exploring that's why i put a in here and let's add in a main method also and let's say finish so we have the setup ready to start playing with copy of what is copy of if you look at list or set or map interfaces there is a new copy of method which is introduced so if i actually do a list dot copy of and let's import in list and go to the list class and if you do a control o in here and search for copy of you'd see a method called copy off and you can see that this copy of method is present since 10 so since java 10 we have this specific method and this returns an unmodifiable list containing elements of the given collection it is important that whenever you pass a collection to the specific method the collection must be null and it should not contain any null elements similar methods also exist in set interface as well so if you go to the set interface java util set in java 16 that's what i'm looking for and you would look for copy off so there is a copy of method in the set interface and you would also see a corresponding method in the map interface so i'm choosing java 16 java.util.map and you would see that even here there is a copy of so if you select any java version which is 10 or higher you should see copy of methods in list set and map what is use of these methods let's say i have a array list so let's say i have a list of string let's say its names is equal to new arraylist of string so this is how we typically define a list of strings and let's do organize imports to import arraylist i'll say names dot add ranga and let's have a few list of elements in here ravi let's say john now if there is a method and i pass the names to that specific method let's say i would want to create a method do not change and i would pass in names to it and if you go ahead and create the method create do not change names what would happen if names actually makes a change to it let's say this is a method you don't have control over it's some other method and what would happen if somebody goes in and says names.add and should not be allowed and you go in and say this out and say names what would happen what do you think will happen will the value be added or not you can see that it's added if you don't want others to make changes what you can do is you can create a unmodifiable list how do we create an unmodifiable list usually one of the options is if you have a static list if you know the exact values if i would want to actually create a list with specific values hard coded values then you can go with the list.off this dot off is introduced in java 9 and it would actually create you and it would actually give you a unmodifiable list containing a specific set of elements however in our example this list can be anything this list of values might be added from anywhere you might be actually getting the list of values from the database and before you send to this particular method you'd want to ensure that nobody will be able to change this list and that's where you can say list dot copy of names instead of sending names directly i can send copy of names so i can say list of string i'll copy this and say copy of names and i can send the copy of names to the other method and what would happen enough if i tried to run this java application you'd see that there is an error it says unsupported operation error it says immutable collection and you are trying to add and that's not allowed so whenever you want to create a immutable list of values from a existing list you can use the list dot copy of method similar methods also exist in set so if you want to create a immutable set from an existing set you can use copy off and if you want to create a immutable map from an existing map you can use copy of again the great thing about these copy of methods is if you pass in an immutable map if the map you are passing in is already immutable it's unmodifiable then copy off will not create a copy it will return the exact map back because it knows that it's in the unmodifiable we looked at copy of methods which were introduced in java 10. i'll see you in the next step welcome back in this step let's look at a few file related methods which were introduced in jdk 11. these are write string and read string let's see what's the use of them in this specific step i'll create a new class and i would create it in the package api dot b and i'll call this file read write runner and let's say let's create a main method and say finish now let's create a simple file so that we'll be able to read it so i'll create in resources right click new folder and i'll create resources and i'll create a new text file so i'll say new untitled text file and let's sample line two line three it's just a sample file that we are creating and i would store it to java new api features resources and i'll give the name as sample dot text now i want to read the sample dot text from here the first thing that we need to do is to create a path variable so i'll say path path is equal to parts dot get where is the file present in resources so the way we can specify that is dot slash resources slash sample dot text let's do an organize imports so this would we have the path ready with the new method in files it's very very easy all that you need to do is files dot read string and pass the path in and if i do a control one and import files i'll be able to do a control one and add throws declaration so that we actually through the i o exception there is a chance that there is an exception that happens when reading the file and therefore we are defining main method to throw the i o exception and now again i press ctrl 1 and i'll assign it to a new variable and this is file content so you can see that we can easily read file content i can do assist out and say file content and if i run it as a java application you'd see that i'm now able to print the content of the file if you want you can even write it to another file easily so let's say i have the file content right now what i would do is i'll say string new file content is equal to file content dot replace and i would want to replace let's pick up some text from here line and i would want to replace it with let's say lines and we have the new file content and i would want to write it to the a different path i can say path new file path is equal to path.get resources sample.sample.new.txt and now i would want to write a file so i can say files dot write string and i can say new file path if you look at the right string method you would see that the first is path and the second one is a sequence of characters so i can send this string out so i can say new file content and let's do a launch of this java application and you'd see that when i launch it up and go over to the resources and right click refresh it you should see a sample new dot file in here and you'd see that it contains lines instead of line so you can see that with this specific new methods read string and write string reading and writing from files becomes even more easier so if you want to read a string from a file and to write a string to a file it becomes very very easy with these read and write string methods so if you look at the read string documentation you'd see that it's introduced in java 11 and you can see that by default it would make use of utf-8 as the encoding in this step we looked at a couple of utility methods which were introduced in jdk 11. welcome back in the step let's look at one more important method which was introduced in jdk 11 which is predicate not what is that when do you use it let's look at that right now so i'll go ahead and create a new package as usual or actually new class so let's go ahead and create a new class and i'll put it in api dot c and i would name it as predicate not runner what is use of predicate earlier we were doing functional programming and at that particular point in time we were using predicates let's have a main method in here and over here i would want to create a simple list so list of let's just say integers list of integer numbers is equal to list of let's just create a few numbers at random so we have a list let's organize inputs java util list and let's say i would want to define a to filter even numbers how do i define a predicate i can say predicate of integer even number filter or even number predicate is equal to number we want to check if number mod 2 is equal to is equal to 0 if number mod 2 is equal to is equal to 0 then it's even number so let's do organize inputs java util function predicate let's do that so we have even number predicate defined now i can use this predicate and filter only the even numbers so i can say numbers dot stream dot filter we can filter using this predicate and in the remaining ones i would want to actually do a system.out.println system.out colon colon println we are making use of the method reference and we are printing it off let's run this alt shift x run java application oops looks like there is a error yeah this one should be stream and let's run this right now run java application cool now you can see that it's printing 4 and 88 so 4 and 88 are the two even numbers in here now let's say i would want to actually use the not of this so i don't want to print the even numbers i want to print the odd numbers numbers which are not even if i'm using this way of defining the predicate the way i can actually do that is by actually saying dot even number dot predicate dot negate so there is a negate method which is present on the predicate itself which can be used to negate the predicate and let's see what it would print run as java application you see that it prints the odd numbers this is one option to do the filtering where negate is very very easy however let's say i'm using a method reference instead of using a predicate let's just define a simple method public static boolean is even and let's say number is a parameter so let's just say it's integer number and this returns this logic this returns if the number mod 2 is equal to is equal to 0. in this kind of situation how do you use this to do the filtering so i don't want to do this but i would want to actually do numbers dot stream dot filter and over here i can pass in a method reference so i can say predicate not runner colon colon is even and then i can say for each this would also print even numbers so you can see 4 and 88 however when you're doing this you cannot do a dot negate so there is not there is no negate method which is present in here and that's the reason why predicate dot not method is introduced so let's try and use that predicate dot not and to the not you can pass in the method reference actually it's a small n not a capital n let's format so that you can see the entire lines of code and let's say save and let's run this right now and now you can see that odd numbers are printed so if you have a method reference and if you want a negation of that you can use predicate.not if i go to the documentation of this control click this this returns a predicate that is negation of this supplied predicate and you can see that this is introduced in java 11. in this step we looked at an interesting method which was introduced in java 11 which is predicate.not i'm sure you're having an interesting time and i'll see you in the next step welcome back in the last few steps we were looking at a number of new api features that were introduced into java in this step let's focus on number of api features which are introduced into this string api string is one of the apis which keeps changing across java releases there are new methods added in very very frequently let's look at some of the important changes in the last few java versions let's create a new class so i'll create a new class i'll call this string new api runner and i'll call this api dot d and say finish and let's create a main how did i forget creating the main method so let's have the main method in here and let's look at some of the api one of the typical things that you would do always let's type in sysout one of the typical things that you would do always whenever you have a string is to check whether it's a blank so i can do dot blank actually the method name is is blank if you go to the east blank method you would see that this is introduced in jdk 11. this returns if a string is empty or contains only white space characters so even if you actually have a space in here and run this you'll see that this returns true the each blank method is to check if there are any non-white space characters which are present in this string other interesting methods are strip methods which were introduced so is oops dot strip and let's put some string in here this is left this is right and we have a space on both sides and in addition to this we also have a strip leading and strip trailing let's see what would be the output which should be printed by these three methods and java application you can see that this one both sides these spaces are truncated and here the right side space is left and over here the left side space is left right side space is removed and to easily see that what i would do is actually after calling this i would say replace space with at you can see where these spaces were you'd see that when there is a strip call both leading and trailing are removed you can see that over here there are no spaces at all over here there is space at the right side but the leading space is removed and over here there is space on the left side that's the leading space but the training spaces are removed so you can see the corresponding methods in here slip strip leading and strip trailing you'd see that if i have spaces in between they will remain as is they'll not be removed out so run as java application you can see that the space in between will remain and it is replaced by the act called in here if you go over to that specific thing you would see that this also was introduced in java 11 and if you look at the documentation this is a strip method and it says returns a string whose value is this ring with all leading and trailing white space characters removed let me copy this again and the next interesting method is to split a string into multiple lines so in earlier versions of java if i would want to split this into multiple lines line one slash n line two slash n line three slash n line four split becomes much more easier i can do a rana's java application here and you can see that this refers and you can see that the list returns a stream so what we can do is instead of doing a system.out.println on this this is written in a stream so i'll say for each system dot out println and run as your application you can see that it is split into multiple lines so if you have a string and you want to break it into a stream and you want to break it into a stream of multiple lines you can use the lines method you can go to the lines method and see the documentation related to this it's introduced in java 11 again and if you look at the documentation it says returns a stream of lines extracted from this string separated by line terminators one more interesting string method was introduced in java 12 and let's look at it right now it's instead of dot is blank i'd want to do a transform so i want to take a specific set of string and i would want to do a transformation on this so let's say this is upper and you'd want to use a function to do a transformation you want to use a lambda function to do the transformation transform method helps you to do that so let's say you can define a lambda in here take the string and let's say let's take that string and let's do a substring and let's truncate the first two characters and return a substring after that so i'm doing a sub screen too you can do any other operation as well this can be a function which does any kind of logic on the string and you can use that to transform a specific string and if you do a rana java application right now what you would see ber so this first two characters are cut off another interesting method was introduced in jdk 13 which is formatted so i can take a string and i can say formatted so let's just say my name is percentage yes and i can pass in my name ranga i can have a number of other things as well my age is and let's a percentage d and i can say ranga kama ranga is very young 25 run as java application so you can see that you can easily format a string using values so percentages is a string which is replaced by ranga my age is percentage percentage t is a number which is replaced by 25. the order is very very important so percentages matches against ranga and percentage d matches against 25. until we looked at some of the string methods which were introduced in recent java versions now there is one interesting thing which was added in jdk 14 which is helpful null pointer exceptions what is this let's just say i want to create a null pointer exception so i'll say string str is equal to null and i'll do a str.esplank what would happen try and guess what would happen null pointer exception right you can see that this message comes up exception in thread main null pointer exception cannot invoke string dot is blank because str is null in earlier versions of java it did not contain this much detail with jdk 14 additional context was added into the message you can see that this is much more clearer you can see that it's saying i cannot invoke string dot is blank method because str is null now as soon as you see that the error was thrown on 15 in this particular case you are sure that str is the one which is null however this might not always be the case let's say i have a class like let's say class sample class and let's say this contains a string so let's just define a string str is equal to null and let's say over here instead of sti dot is blank you are actually using a str from here so what you have in here is sample class sample is equal to new sample class and over here you are actually making use of sample dot str dot is blank oops i'm not able to create an instance because this class should be outside the public class so this is where it should be yep now i'll be able to create an instance of it and now let's run this now this is the message which is printed let's copy it in and let's format it a little bit now you can see that it's saying sample.str is null earlier in the before versions of java you don't know whether sample is null or sample.str is null and if you have such a chain of calls which are going on you don't know which one of them is null with java 14 this is made much clearer to us sometimes in production we have errors and on a specific line there might be a lot of complex things that are being done and any of them could be null and earlier it was little difficult to solve those problems and now because of this clear error message it would be easier along with the null pointer exception you'd get a hint as to what was null and that would help you to solve the problem much more easily in this step we looked at a few new string apis and also looked at the enhanced null pointer exception message i'm sure you're having an interesting time and i'll see you in the next step welcome back in this step let's look at another important feature which was introduced in java 10 which is type inference type inference is one major feature which makes the life of a programmer very very easy let's see the advantages that type inferences provide us with let's create a java class and i would call this type inferences runner and i'll create it in the package com dot in 28 minutes dot api dot e and say finish let's add a main method and let's go full screen some of the times you might have complex definitions for your variables let's take a simple example let's say i have a list of string and let's say names is equal to list dot off let's have a few names and you might have another list of strings as well so let's create another list of strings let's call this names one and let's call this names2 and let's have john and adam in here let's organize imports java util list you have two lists and from this list i would want to create another list so list of list of string names is equal to list dot off names one comma names two now if i would want to make it even more complex i can create another list with list of list of list of string the thing you can see in here is the left hand side declaration is becoming complex to make it simple in java 10 type inference is introduced instead of actually trying to define everything which is present in here you can say var so instead of defining the type of the variable you can say java i'm lazy i don't want to figure out what is the type you figure out what is the type and make sure that the right methods are available the interesting thing is all the methods that you can do on names are still available so if you want to do a names dot stream and you want to print all the items which are present in there for each system dot out colon colon println so i'm printing all the lists which are present in here you'd see that this would work run java application it's printing both the lists and if you make an error in the method name stream one there is no method called stream one which is defined on this specific list it gives you a compilation error it says the method stream one is undefined for type list list string now it's saying the type in here you have defined it as var where is the type coming in from java looks at this list dot off names one comma names to what is it written that type is taken as the type of the val so instead of you specifying it java identifies that from the return value of this particular statement this is what is called type inference type inference is one of the new features where java compiler infers the type of the variable at compile time important thing it's at compile time so directly at the time of compilation java would infer the type of the variable so instead of saying list of string numbers you can say var numbers and java would know that this numbers is of type list of string because it can look at what is present on the right hand side this is introduced in java 10 you can also create final variables if you don't want anybody to be able to modify this you can say final but by default this is not final so if you just say var names names is not final however if you don't you can make it final by adding a final in here you can also use var in loops so typically we would be doing something of this kind for int i is equal to 1 i less than equal to 10 i plus plus do a let's say system.out.println this out i over here instead of int you can also say var not a lot of characters that are being saved but it's an option you can do this as well or because you have a list in here i can loop around the list so i can say typically i would say string name in names1 and i would print name so this is the enhanced for loop and even here instead of string i can say var so you can use var in loops as well a few important things to remember about vars are you cannot assign null to it you cannot go and say where abc is equal to null this is not allowed you can see there is a compilation error which is coming in it says cannot infer type for local variable initialize to null only if some kind of an object is present on the right hand side java would be able to infer the type otherwise nope it's not allowed another important thing to remember is var is not a keyword why is it important it means that i can have a variable with the name as well you can even create a method with the name as var as well so vr is not a keyword important thing to remember is vr can make things simple but it can also make things complex if you don't use it the right way so make sure that your variable names are good so whenever you're using where make sure that your variable names are really good they are very very expressive because because somebody who looks at the type will not be able to see the type in here you just see a var similar things can be done here as well so when you do something of this kind who is looking at this code will not know that this is a list of string so the naming of variables becomes really really important when you do something of this kind and it's very very important to have small scopes for your variables if you define a wire somewhere and use it somewhere it it becomes difficult to understand the program one of the places where you can use maps is if you want to improve the readability for chained expressions some of the times you have a huge list of method calls which are being done you might have list of of rangaravi dot stream dot filter s and i would want to filter those with length less than 20 or less than phi dot for each system dot out colon colon println so you can see that you can write code like this oops this should be scn length you can see that you can write complex code like this this is a huge chain of commands in this kind of situations what you can do is you can break it down by using a var let's run this and see what it would print you would see that it would only print ravi because ravi is the only one which has length less than phi instead of having the entire code on one line what you can do is actually probably extract a variable for one of these so i can say i can do all shift l i can say refactor extract local variable and i can call this filter and you can see that it's saying stream of string filter i can say var so i can create an intermediate var variable and make the code more readable in this step we looked at a new feature in java 10. local variable type in friends i'll see you in the next step welcome back instead let's look at switch expression in earlier sections we looked at switch statement what is switch expression and why do we need it that's what we'd be looking at in this specific step switch expression allows you to create expressions using switch statement this is released in jdk 14. we talked about the fact that new releases of java are made every 6 months and with these new releases sometimes java also releases features in preview so this switch expression feature was released as preview in jdk 12 and 13 and was made finalized in jdk 14. in jdk 12 and 13 also you could have tested the switch expression feature by enabling a preview flag however from jdk 14 it has been made a standard feature let's look at a couple of examples for switch expressions let's look at examples for switch expression let's go in as usual let's create a new class and i'll call this switch expression runner and let's use the package dot f let's create a main method and say finish and let's go ahead and play with it here let's create a simple method public static let's just written a string back let's say find day of the week and let's say the value which is passed in is an integer so you are passing a value of between 0 and 6 and you have to determine the day of the week let's say int day and one of the easiest ways to implement this is to go for a switch right switch on day and you can actually create a local variable saying string day is equal day of week is equal to empty or null at the start you can say case 0 let's say it's sunday you can say day of week is equal to sunday and you can say break and similar to that you have other statements also so case one case two case three sunday monday tuesday in this day and so on and so forth you can write the cases for the other ones i can say day of week is equal to error or invalid or you can even throw an exception from here i can say through new illegal argument exception invalid plus the invalid option plus day and at the end what we can do is we can say return day of week this is typically the way we make use of this switch statement you can see that there is a lot of ceremony involved when we are doing a switch statement because if i don't have a break what would happen there is a fall through the code would fall through to monday tuesday and so on and so forth that's the reason why we need a break with java 14 you have an option to simplify this further so i'll this i'll copy the same method again i'll call this file day of the week with switch expression and over here what you can do is instead of actually assigning it to a local variable i can say k is 0 and this is similar to a lambda expression so hyphen greater than so this very very similar to a lambda expression however when you are doing this you don't really need to use the break and you can see that case one is giving you an error right now so i'll go ahead and fix that so i'll do the same change over here and let's remove the break as well and over here as well i can do that and you can do the same over here as well and over here i would change the default as well to do the same thing and over here instead of doing this ascending to empty and that i would actually directly assign it like this at the end of the switch statement i would need to introduce a semicolon so that the code would compile so you can see that this code is much much more simpler so over here what we are saying is if case is zero day of week is sunday case is one day of week is monday so what we have done is we have converted the switch statement to a switch expression and this expression returns a value what is the value which is written by this expression it is the day of the week sometimes you might have complex things to do so in here if you just don't want to do sunday directly you might have some logic before you find it out that it's sunday in that kind of situations you can put it within braces once you put braces the syntax changes a little let's do that right now so enough we have complex logic in here so what we'll do in here is let's say do a complex logic system.out.println and a lot of other things whatever you want to do in here you can do that do some complex logic here and after that you want to return sunday back how can you return the sunday back over here you cannot do return the way you can do that is yield so the statement to return the final calculated value is yield and over here when you're using braces you don't need the semicolon at the end so now i can save this i want this switch expression to evaluate to this value if case is zero that kind of situation i can use yield and return this value back one of the important things to remember in the case of a switch expression is that there is no fall through in the case of switch you need a break because the logic falls through so if this case meets if k0 is success if you don't have a break what would happen it would fall through to case one if case one does not have a break it would fall through to case two however in the case of switch expression there is no fall through at all if you are writing monday as the value of the expression it would directly be returned it would not go to the next case statement the same is the case with yield as well as soon as you yield a value the value will be written back as the value of the expression in this step we looked at switch expressions i'm sure having an interesting time and i'll see you in the next step welcome back let's look at another new feature in java which is text blocks look at this line of code is it easy to read what it's going to print you can see that we have the slashes in here to escape double quotes and we have slash in to indicate that it's newline and this kind of code is difficult to read and that's the reason why java introduced text blocks where it makes it very very easy to look at the string and identify what would be printed so you can see that this is a text block by three double quotes over here and three double quotes over here and within these three double quotes whatever is present is printed as is so this is used to simplify complex text strings this is released in jdk 15. this was a preview feature in jdk 13 and 14. let's quickly look at an example let's follow the tradition of creating a new class and i'll call this text blocks runner let's add a main method and let's use the package api.g and say finish and over here let's create a text block so string str is equal to these three and followed by your text that you want to print so line one line two line three and let's do a sys out of str let's see what would be printed let's not do a println let's do just a print as java application you can see line one line two line three which are printed and there is a new line at the end of line three as well because that's how we have it in here if you don't want a new line at the end of line three you can actually remove this you can actually put the three quotes right after line three and then you would see right click run as java application over here at the end of line three there is no new line character so if you see this that's the last character three is the last character and after that there is no new character so if you wanted new line at the end you can put it this way if you don't want a new line you can put it this way the other interesting thing is have you started you cannot start it this way so if i have a three cores right after that i cannot put line one this would give you a compilation error after three quotes the character which should be present is newline and only after that you should have your text so three quotes followed by newline character followed by your text and that's what is printed out and over here you can have formatting individual formatting for these lines if you look at this thing there is formatting in here and because there is same formatting for all the three lines this formatting is ignored however if you have specific formatting for a specific line you would see that that would be retained so if i am entering three spaces before line two you would see that that formatting would be retained because that's specific to line two so you can see that these three spaces instead in front of line two are printed as is so if i have another line in here line three and line four let's add another one actually line four let's tab let's have one two three four spaces so line 5 you'd see that all this formatting would be retained so if i make this line 6 and print right click run as java application you can see that the entire formatting that you have in here is retained and it's very interesting that you can use double quotes as well so if you have double quotes in a string you have to escape them you have to put a slash in front of them and it makes it very very complex but when you're using text blocks no worries you can directly add double quotes and you would see that double quotes would be part of your string important thing to remember is that in the first line of your text block there should be three codes followed by a line terminator or a new line character you cannot have the entire text block on a single line you cannot have a text block of this kind a cr1 is equal to all three codes here and let's say i would want to have my string and three quotes in here this is not valid you need to always start the value on the next line so this is where you need to start the value and you can choose to end it on the same line as the text however following the first three quotes there should not be any text there should be a new line character that's how the compiler identifies that this is a text block we talked about automatic alignment already we saw that if there is some space be before the entire string which is common across multiple lines it's ignored however if there is specific formatting for a specific line which is more than the earlier lines then that formatting is retained so there is automatic alignment which is done and training white space is also stripped so if you have any white space in here in any of the lines all that is stripped off so if i save this and run as java application if you take the cursor at the end you would see that divide space is not printed so if you have any training white space it's stripped off and you can use text blocks wherever you can use a string and all the operations that you can perform on a string you can also perform them on text box now my recommended approach of using text blocks would be something like this so the best way to define a text block would be string text block is equal to something like this and you can have all the text in consecutive lines right beneath it so line one line two line three line four have a ending code starting the same character as the line phone so this is easily identifiable as a text block so this is how i would recommend you to have text blocks formatted the interesting thing is you can also use things like formatted so if i have something of this kind of line one colon percentage s colon percentage s you can add percentages for the other ones you can do a dot formatted and you can pass the values which you'd want to format so let's say i want to put line one and some value and some other value so formatting of this kind so if you have an email template in here so this is a template and you can replace the values in the template in here and you can try and print the text block over here so system.print block what would it print in a java application you can see that it's printing this string this template with the values replaced so using text blocks you can easily create templates for emails or things like that in this step we played around with text blocks i'm sure you're having an interesting time and i'll see you in the next step welcome back in this step let's play around with records what is the record let's look at a few classes that we created in advanced object oriented programming you would see that most of these classes have the member variables the constructor tostring and a lot of other methods which are defined let's pick up another classroom inheritance let's pick up the employee class you can see that you have a number of member variables you have a constructor you have getter setters tostring and a lot of other methods you might also want to define equals hash code and other methods as well and this is the exact same thing that we would do across multiple beans how do we eliminate that verbosity how do we eliminate the need to define constructors to d to define getters data methods to define two string methods equals hash code methods that's where this feature called records was introduced into java when you're using records the public accessor methods constructor equals hash code and two string methods are automatically created for you you can also add custom implementations for these methods if you want let's take a quick look i'll close all the files which we have opened and let's go over to the java new api features project that we created these are not really api features but let's make use of the same project for this as well so let's go ahead and say new class and i would say api efg h and over here we would want to play with records records runner let's add a main method in and say finish now over here let's create a record record person string name let's say i would want to have email and let's say i would want to have a phone number string phone number followed by open brace and a closing brace that's all you would need to create a record once you have record you can actually create a new instance of the record so once we created a record of this kind then i can say person person is equal to new person and over here i need to pass in the name email so let's say ranga and let's say ranga at in 28 minutes.com comma one two three four five six seven eight nine zero so a person is automatically created and you can even do a sys out of person let's see what would happen so instead of writing all that code to create a constructor to create a tostring and all that this makes it very very simple if you run this right now you can see person name is equal to ranga email is equal to this phone number is equal to this a person object is created with these as member variables name email and phone number and you can also compare so i can say person is one let's say person two person two has different values let's say ranga 1 in that kind of situation you can say person dot equals automatic equals method is also created oops i would want to say person dot equals and person dot equals person 2. and if i run this right now you would see that the first one would be true the second one is false if all the elements have equal values then these two objects are considered to be equal so there is a predefined equals method which is defined similar to that a hashcode method is also defined similar to that even accessor methods are defined so you can say system.out.println person dot name it would get the name of this specific person i would remove the e out comment out the equals parts of it so i'm not really worried about this so let's move these down let's focus on person dot name let's see what it would print what would it print you can see that it prints stronger so you can get name or email or phone number so person dot email person dot phone number are the accessor methods which are present in here these are called public accessor methods so a constructor is provided accessor methods are provided hash code and equal methods are provided and two string methods are also provided for you by default when you create a record you can also create custom implementations if you want if you want to alert creation now over here i can also define an explicit constructor if i would want so if i'm not happy with the default constructor which is provided i can go in here and say i want to create my own constructor so i can say person this and i can go ahead and define the constructor and over here i can say this dot name is equal to name this dot email is equal to email and this dot phone number is equal to phone number and you can add validations if you want so if this dot name is equal to null through illegal argument exception name is null so something of this kind is also possible let's do an organize inputs actually it should be new illegal argument exception so let's and this should be name is equal to equal to null so if the name which is passed in is null you want to throw a illegal argument exception and name is null so you want to add validation we can define a custom constructor like this and you can use that to create your object you can also override other methods which are present in here one of the things that you are observing in here is when we would want to define our own custom constructor there is again the boilerplate which is coming in so i have to write all this kind of code and there is a simpler way of actually doing this this simpler way is called a compact constructor how do i create a compact constructor i can remove the arguments from here completely and i can remove these three lines of code this is what is called a compact constructor so you don't really need to say this dot name is equal to name this dot email is called email this dot phone number is called phone number you just need to say person and you can define your validations in here so whatever validations you want to be done you can do them and after that the object would be automatically created for you let's look at a few things that you need to remember number one is compact constructors are only allowed in records number one is that compact constructors are only allowed in records so only inside records you can use compact constructors in normal java classes you cannot use a compact constructor you can add static fields static initializers and static methods inside your records however you'll not be able to add instance variables or instance centralizers so you cannot go into a thing in here and say i want to add a number this is an instance variable and that's not allowed non-static fields are not allowed however you can create static fields so if you make it static that is allowed one of the non-static things that is allowed is to create instance methods if the name method is not sufficient if you want to add additional logic you can say name and you can go ahead and define your own name method so return name and before that you can add your own logic so you can test this out and do something in here before you would actually return the name back and whenever you are giving a definition for an accessor method this should be public so public string name do your logic and return the value you would want to return so you can add static field static initializers and static methods but you cannot add instance variables or instantializers however you can add instance methods we looked at an example of an instance method that we added in just nerve in this step we looked at records
Info
Channel: in28minutes Cloud, DevOps and Microservices
Views: 27,582
Rating: 4.9205675 out of 5
Keywords: AWS Tutorial, AWS Certification
Id: 8hA47LxykPo
Channel Id: undefined
Length: 68min 59sec (4139 seconds)
Published: Wed Mar 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.