Exploring Java 9: The Key Parts by Venkat Subramaniam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right let's get started thanks for coming we're gonna talk about Java 9 we're gonna talk about a few key parts Java 9 itself is very one of the most powerful improvements to the language we'll talk a little bit about that and then we'll focus on what we gonna talk about in this presentation well looking back in time over the past few decades Java has had been very stagnant and not very many improvements over the years and suddenly all that changed with Java 8 if you really look at Java 8 itself you could say Java 8 was somewhat of an opt-in to a certain extent as a programmer you had the choice of saying I want to adopt these things are I want to wait out and maybe adopt later on while Java 8 was extremely significant from the programmer point of view Java 9 is significant from every single person point of view it doesn't matter what part of Java ecosystem you are dealing with there's going to be an impact related to it and that is one of the really interesting things about Java 9 as it is probably we could claim the the most significant change to Java ever even more than Java 8 itself in that regard so I want to talk about Java 9 and there's you know if we can come up with a laundry list of all the features in there and a lot of times there are two problems with such an approach one is the list is very big and the second problem is we don't remember any of those when we just look at a laundry list of features so I step back and ask the question what do I want to really talk about in Java 9 and I realized that there is actually three different groups we can look into when it comes to Java 9 so I decided to classify what we're gonna look at into three different groups the first one I want to put them into a category called improvements and changes what I mean by improvement and changes is these are things you already do in Java 7 or Java 8 but you can do it a little bit better and that is always nice because hey whatever I'm doing before I can do it a little bit better that makes me off reductive that's awesome so we got to look at a few things that I would like to call as improvements and changes the second category is what I would like to call as additions additions are things that did not exist before but we can use them now and as a result benefit from some of those things that are available so I'll call them as additions and and the third category I was kind of wondering what to call it so very politely I decided to call it as advances meaning disruptions it's gonna hurt you gonna kick and you can scream and then eventually we come out I had you know enjoying it so that's what I want to talk about these three groups in the in the next about three hours or so we have together I want to talk about these different three different things um so let's get started with the first category here improvements and changes let's talk about interfaces well in Java of course we've had interfaces from day one which is one of the things that really excited me I came through the background of programming in C++ years ago and when it comes to C++ we had abstract classes we could have you know abstract a classes with pure virtual methods but the concept of interface has never sunk into me and I remember the day I got you know I looked at Java's interfaces and interface inheritance that's an unforgettable moment in my life I still remember that after these many decades because that really changed the way I look at the language and on how can use it so interfaces have a special heart in my in my mind so we could call an interface here called util let's say and in this case the interface let's say has a map that I want to write in this case we'll say it's called get let's say number of course for up for example well I want to know how the compiler deals with this so I'm gonna output right here just the word okay and you can see that the compiler is pretty happy with what I wrote so no big deal it seems to be hey you're happy writing Java I'm in good terms with you but if I go here and try to implement this method maybe I say return for for example immediately Java will give me an error obviously saying what are you trying to do you know Java it says well obviously we cannot put implementations into interface or at least that's the way it used to be so my definition of inter face in Java is or was it was an interface and where an interface is something that told a lot about what it'll do but it never does anything kind of like my boss I always say that so so that's basically what it was but this has improved quite a bit thankfully now we can write implementations but we can put a default method in it in Java 7 sorry Java 8 of course so we can create these as default methods that's it that's really nice but let's step back for a second let's talk about classes well when it comes to classes you normally write public methods and classes but occasionally you will realize quickly that multiple public methods require the same they know implementations and rather than duplicating that inter implementation because we know that if we start duplicating things we're in violation of the DRI principle so rather than duplicating that implementation we say we want to really reuse that function so what do you normally do in a case of a class well he will take that common implementation and roll it into a private method within classes the same concerns appear here as well with interfaces it's quite possible that multiple methods default methods in an interface may have common code and if there's a common code where would you put it well you could roll them into another default method that's one option but if you roll it with the front default method that method you rolled into from other sub methods become available for inheritance by classes become available for use and maybe you're saying well I want to roll these methods together but I don't want you to really use it from the outside it's for me for my internal purpose we call them as helper methods the same exact idea it is available for interfaces too so for example if I wanted to really call this as a helper and whatever the helper is gonna return I'm gonna return from here I can say private int helper in this case let's just return that for right here which I wanted to really maybe reuse in other methods I can definitely do that as well but because this is a private method it is hidden within this particular interface not available not visible and and so that is there's one of the things now of course you know that you can only put a static method in interface you can alter a default method an interface you can also put static methods also so for example I have you know some method foo here that is going to be a static method well in this example of course you may wonder if I have static methods and you know default methods what's next do I have private methods well they say be careful what you wish for so you actually do have that now you can have private methods but what if I want to really share this method with the default method versus a static method how would I actually use it well if you have a method which is going to be used within a static method we'll say in this case helper do well obviously I want to use helper to within the static method so I could say private in let's say in helper 2 and let's say in this case I want to return the value of 5 but unfortunately though in this case we don't know what that means so we get a little compilation error saying I cannot reference the helper 2 from a static scope well obviously in this case you would say static right here and then that would be calling that well that that's the question do I really need to say default over here because after all this is for a default method versus this is for a static method and the answer is no you cannot so if you want to really write a private method which is for access by default then just write as a private method if you want a private method that's useful for static context then do sprinkle the word static in along with it as well and so as a result you can write private methods and interfaces too and essentially these are for helper methods that you're going to write and then tuck them away and so you can reuse them across multiple methods as you may choose to so that's one nice little improvement in the language where you can have these reusable methods you can provide really nicely and that's basically what you're you have available one other little improvement goes back to Java 7 feature so for example if you have a class called resource let's say and I have a constructor for resource in this case I'm going to just output let's say created right now and then of course I want to implement let's say a method will call this ass up one and all that this OP one method is going to do simply is let's say output op one well that's my little resource class but I want meant a fairly good cleanup of this resource so what you could do in this cases in the class of course you can write a public void closed and the close method simply is going to say in this in this implementation all I'm going to do is clean up well okay so that's a cleanup method I have implemented but I want to use this resource class and make sure that it is going to perform a proper cleanup operation how do I go about doing this well rather than having to spend the time and effort doing this we could simply say implements for example and you could say our toe closeable and other plausible of course expects a public closed method to be available but the benefit that gives you is you can put a try block and say resource and then once you create a resource or object right here you can say equal to new resource you can go ahead and call the methods you want to call for example in this case you could call resource dot up one but when you nicely leave the block of code right here the close is going to happen for you automatically without you having to put any effort we call this the try with resource or a RM or automatic resource management well this worked really well there was one small edge case if you will this really required that this object that you are putting around as a resource constraint is being born right there in this call well well that is really a good idea most of the time there may be a time when you acquire this object from somewhere else or even even different what if this object is passed to you in your function call well you may ask why would you really bother about it well remember that what two letter word we all occasionally think about di dependency in inversion or injection so if you have a dependency injection you want to be able to receive that being given to you rather than you calling a getter to get to it so what how do you deal with dependency injection well in the case of dependence action maybe I have a function you're passing this as a parameter to my function in which case this object is not being created right here so it's going to be available outside of this maybe it is you know coming from somewhere else like a parameter to a function of course I'm creating this right here but silly me but it could be coming in as a parameter for somewhere but you don't want to put a constraint around this resource and say you know what you created you're giving it to me but I'm the owner of this object well that restriction of the try with resource as been removed now you can put that outside so it could be given to you um I don't want to mention that I'm always wondering whether this is a feature or a flaw because this could be you know really misused as well so I would use it with a grain of salt if you really have a good reason to do this then use it if you're not very convinced is a good idea usually I ask for help I go to a colleague and say I have this evil thoughts in my mind can you review my code and usually they set me right so the thing is that don't assume this is actually a good thing it might be good in some special cases a lot of other times this might not be a really good thing one of the reasons we have to be careful about this is while the object is being cleaned up you notice that it is calling the cleanup right there and I'm gonna say done over here notice that done came after the cleanup but the object is still available to us which can really be problematic if you're not very careful about it so use that with a grain of salt don't don't just assume that because you can do it that you should actually do it so that is just a feature and it's I think it's useful like I said I can think of a few different reasons why this is good di is one of the reasons I can think of and and butt out about it but again you know use your judgment whether whether you should use this or not well moving a little forward let's talk a little bit about what a few other languages normally do well if you look at languages like GRU we are Kotlin in these languages when you have a parameter coming in as a lambdas parameter you can receive the lambda and you can give a name like E and you can print the value of E like this but programmers sometimes do something some other things like they say k and then print line K and then somebody else says you know L and then print line L and I've come to the point where I realize these variable names are absolutely random and one day I was working with the code and I was trying to predict what the next variable name is going to be and I totally failed at it it was that random and then I figured out finally the way these variable names are given is programmers don't actually type this they are coding away they get distracted and they look around their character Italy walks on the keyboard and whatever key they step on become the variable name and they love their cats so much they oh wonderful you gave me a good variable name now go play while I cut into code and they just scored away well if you're gonna come up with absolutely ridiculous random names I would rather get rid of the ridiculous names and let the language take over so in these languages what we can do really is rather than writing these ridiculous names we can simply get rid of it and we could say it for example and in this of course the H stands for you know the first and the only parameter that's coming in on the other hand if you look at language like Scala you would similarly write a lambda but in this case you would you may potentially get multiple parameters but you use an underscore for that in fact in the case of Scala you could even use underscore multiple times and in this case you could even do something like like that if that makes sense in the context and this would be where you're receiving one but not not one but two arguments to the language or to the lambda I have to say the jury is still out on it I've only seen two kinds of JA Scala programmers those who love this and those who hate this there is nobody in between I think so and and it turns out the people who hate it are more than the people who love it also so this is a little hard for programmers to get used to it but Java is still not sure Java in the case of Java the jury is still out I think they're sitting there and debating a group of people saying this is awesome and the other group saying are you out of your mind so they want to take the time to decide well they take the time to decide whether they want to do something like this in Java or not they wanted to take a dips on that variable so in Java eight the underscore was made as a warning if you had a code where you used underscore you'll get a warning in Java 8 but now that turns into an error in Java 9 so for example if I said equal to 4 right here and then if I wanted to put put the output now why in the first place I used the underscore for defining 4 because we cannot we can all agree that makes the code so much more readable isn't it so as a result we want to use these kinds of beautiful syntax so but when I run this code you notice it gives you an error it says a release 9 underscore is a keyword and may not be used as an identifier and so they don't want to use it so what is the answer the other day I was talking to a programmer and he was quite content with that he said hey that's not a problem if they will not allow me to use underscore I'll use two of them No so this is not a good idea and the next thing you know there are programmers doing this and all guys like me have a really hard trouble because now I have to start counting the number of underscores I mean if your goal is to make your code unreadable I mean there are several ways to do it still don't use this means so the point really is this is not a good idea and clearly in this case you can see that the language doesn't really bother if you do this but other stuff I should really do that and what did it put console dot log bad things happen when you do that ok so you can see that it's the real output right there and I'm gonna even try to put that in there and see that's ok so don't go there definitely so the point really is that underscore is no longer available and so what is the answer the answer is give it a respectable name for it whatever that meant give a good name for it and then use that name and and be happy with it no need to make the code really cryptic after all so no underscore well that's great so far but let's talk about Java eight one more time well in Java 8 I absolutely have to tell you that I love streams you probably know this already I just enjoy using streams but when it comes to streams let's say we have a collection of values to work with so in this case I'm going to say integer will call it as numbers is equal to arrays start as lists and in this case we'll say I have 11 I have a 32 let's say a 12 may be a 54 may be a 67 and 81 and then sixteen and then let's say you know what twelve alright a fifteen well maybe what I want to do is I'm gonna say print only values greater than let's say 55 well how would I print only values greater than 55 I can say numbers start stream and then I could say filter given an element element greater than 55 and then I could say for each and system dot out and I can simply ask to print that value well well that's really easy to do what it really happens in the case is the filter operation it gave us the 67 it gave us the 81 it did not give us anything afterwards because those are not really greater than 55 but it picked up every single value and decided whether to take it or not so here's a way you can think of a filter think of a filter as a flap which we'll be doing this back and forth back and forth so a flap may open and close and open and close it may stay open for a little bit closed for a little bit I can go back and forth so a filter is like a flap so when an object comes in it can either let it go or when object comes in it may block that object and drop it and then maybe let the next value go through our block the next one it can go back and forth this has been around in Java eight which is really nice but there's one deficiency in Java eight however well what is the deficiency let's step back for a second in the case of this one we had a method called limit and the limit will tell you how many values go through so I can say a limit to for example if I said limit do it only allows me to let two values go through this is already in Java eight similarly there's a method called the Skip and the Skip method will remove two values and let the other values go through notice it started with 12 because we skipped two things again that's available in Java 8 so Java 8 gave us methods like filter and then on the other hand methods like you know limit and the methods like skip while if you look at this there's a disparity here if you looked at the filter what is the filter do the further took a predicate and and that's basically how the filter worked it took a predicate on the other hand unfortunately limit took a count and and the skipper took account of so a number well wait a minute though what if I want to limit not on a fixed number but what I want to limit based on a condition so in other words if I want to say go ahead and keep bringing values for me but the minute you see a value that doesn't meet your condition don't off the switch and stay turned off so the way this limit can work is it'll stay open but I've been a condition is met it closes and never to open again that would be really cool isn't it well one way to have done that is we could have said limit a limit while but if they called it limit while they would have been consistent with limit and in software development we should never be consistent so they said we call it take wild because that's so much better so we'll call it take well and in this case we will say given an element will say element is less than 55 well notice in this case we're going to take less than 55 so this is going to be less than 55 less than 55 we keep going but 67 is not less than 55 turn off the switch close the gate never letting anything go after this so the last member is going to be 54 and nothing after would ever show up because you close the ditch at that close the gates at the door and and went away so that is take one similarly remember you have a skip over here would let's say value of four this will drop the four values or skip the four values and then give you all the other values starting from sixty-seven likewise you have what is called a skip while but they could not call it skip while because they'll be punished for being consistent so they called it drop while that's really really nice so in this case you say given a value e.e less than fifty five will drop everything less than fifty five and bring everything else after that so that is basically the take while and drop while which are the cousins of limit and skip except that they are condition based rather than a fixed value based I don't have to tell you that I'm really happy for these functions because there are a lot of times I would be writing code and streams and I would suddenly realize oh my gosh I want to break out of my loop but there's no real good way to break out of the loop and that becomes really hard this gives a nice way to break out of your loop are to continue at the proper point so in other words take well and drop while are really you were continue and break a equivalence in imperative style of programming where take while is equivalent to break and skip is skip Boyle or drop while is equivalent to continue so these are extremely important functions that make our life a lot easier without these it was really hard to program with the functional style and I'm really glad that that's been added to the language so definitely a really good welcome addition to the language so this is another improvement we talked about now the next thing I want to talk about here is when I talk to people who are doing improv the style of programming I often encourage them to move towards the more functional style of programming so this is Java 8 again one more time Tom and I introduced a qilin and skipper drop while but in this case going back to Java 8 I can do the following I can say far in I equal to zero I less than 5 I plus plus and then I can simply output I right here well in this case as you can see that's a traditional for loop we wrote well the equivalent of this in Java 8's I can take this one and say I want to use a stream in this case we would say in the stream into stream dart range from 0 through 5 and then I could say over here for each and in the each of course will simply say system dot out and we will go ahead and print out this particular value from our expression well the output is exactly the same as the other one well this is a much more civilized way of writing this this is in Java 8 I'll try but what if I had a less than are equal to in the case of a traditional for loop if you had a less than or equal to this will be range closed rather than being range close range itself so arranged closed will include the last value whereas range will not so if you put a range it stops one shy if you put a range closed it also includes that value maybe you are helping a colleague of viewers who writes program in imperative style and eats imperative style for breakfast and you're trying to convince your colleague to start writing code in the style but you know how sometimes people are not really that convinced and your colleague comes to you and says oh yeah so how do you do the following I equal to I plus two and you're gonna say you win and you're gonna walk away because in Java eight this is not really easy to do and even if you try to do this after you are done you will still have to face your colleagues that's not going to be really fun at all so you kind of say yeah you're right go be go be happy write in a parody style well thankfully that has changed in job and nine before I talk about Java nine I want to just quickly remind you stream that Ito rate is available already in Java eight stream iterate in Java eight will take an initial value and then after that it will take a lambda which will keep on producing the next value in the chain whatever that you want to do so this Internet is already available in Java Java 8 but in Java nine you have something even better so in Java nine you can say in the stream and and before I talk about this iterate method I want to emphasize one thing I'm a huge fan of api's that are intuitive I I really love ApS are intuitive because when ap is intuitive I quietly calmly sit down and program when ap is not intuitive I'm forced to read the documentation and a lot of times the documentation only irritates me even further I want really things to be absolutely clear when I look at it so let's talk about how clear and simple this could be when you look at this this an initial value that we call as a seed value so you put a zero now take a look at this closely for a second what are we doing given a value I you are telling whether you have hit the limit or you have not hit the limit in other words that result of the highlighted piece of code is a true or a false Tara we know exactly what that is in functional style in the function package that's called a predicate well there you go you figured out what you really need that's a predicate so given a variable I tell me I is less than or equal to five that becomes your predicate and as a result you turn that little boolean expression into a predicate lambda expression in your iterate method then you take a look at this part right here what does that look like given a value you return yet another value and you know exactly what that is that's called a function functional interface tada that's exactly what we're gonna use a function so you simply say given an i I wanted to turn an i plus two so you replace the equals with the dash and you got what you really want and then you say dot for each and then of course in this case system system dot out and then print line I really appreciate what they have done here is they've made the code so easy to understand and intuitive so this is an example of how you can do a looping with the iterate and that makes a life lot easier well and and while we're on this topic there is one small improvement you can also do you know that this part is extremely critical sometimes you may increment sometimes you may decrement sometimes you may step over by seven sometimes may decrement by four whatever you do it's perfectly fine you can do a plus three if you wanted to or a - you know but - and so on you can do that however you also notice that in this case this actually is already done in Java and you don't have to put it right here so what you could do is you can remove this so in other words the ether rate is overloaded so the iterate is overloaded where you specify a seed value all the time then you specify either a predicate and a function or just a function if you want to only give a function then replace this with the limit and in this case of course you can put a limit on it or you can also replace this with the other cousin of limit which is take wild or you can use a skip or a drop while you can put all those combinations of things depending on what you want to do and so drop while and so on so you can use those flavors as well so either use iterate with three arguments where the first argument is a seed the second is a predicate a third is a function or use iterate with two arguments where the seed and the function and then use something to limit or put bounce on your infinite stream like a limit or a take while or a skip or a drop while and that way you can control the looping in in the way you want it so that gives you a nice little flavor as to what you want to do so that is basically an improvement to the in-stream you can use it without bounds or you can use it with bounds as well and as you saw here the bounce is optional oh talking about optional let's talk about this other optional that's available in Java well what is optional let's step back to Java eight again well remember effective Java what is effective Java tell us about this if you want to return a collection but you have nothing to return then well return an empty collection and not know so null is a smell we want to avoid null as much as we can and you know you don't want to return a null pointer null pointers poor people's they don't really have fun programming with it so you don't want to just return a null just return an empty collection that's what effective Java said and I think most of us have been following this really nicely but what is effective Java say if you want to return an object but you have nothing really to return then return you know what no well unfortunately that's what we've been doing right you return a null and that's no fun you get into a null pointer exception what is wrong with null pointer exception if you don't handle it it blows up if you handle it the code begins to really smell bad and so there's no real good answer you get beaten either way you go you're doomed if you do and you're doomed if you don't so would it be nice to have a compile time error to tell you you need to really maybe check for non-existence of value that's exactly what is available in Java 8 with optional so as a result we can take a list of numbers here for example numbers start stream and I'm going to say dart filter give me an element greater than 5 but find the first value for me so what is the find first to say the find first is very defensive the fine first says you know what I may not have a first element period the given collection may be empty or the given collection may not have any number greater than 5 in that case sorry I don't have an answer for you so in this case if you run this notice you got an optional 6 on the other hand if you said 50 because there's no value greater than 50 the first element that's greater than 50 is optional empty as you can see so this only worked in Java 8 which was really nice but unfortunately optional had a very few many rough edges so when you picked up optional there were a few things that you always said yeah these could be really better let's talk about a few rough edges the first question is how do you get the value out of the optional well one way to get the value out of the optional is to use the get method if you use a get method it gives you the value sex but unfortunately if the optional we're empty what is gorg 8 going to do get will blow up on your face and that's no fun at all in fact you look at this and say wow isn't that what I try to avoid in the first place so if you go to the people who wrote this method they will tell you if there a time machine they will go back and remove this and this is one of the most terrible methods in the JDK so I have a best practice recommendation for you the best thing I would like for you would get is forget so don't even bother it never use it not a good idea so well then what Crudup could I do you could use an AR else and then substitute a value for it if you want there ok that was there already we could use that but let's talk about other ways to process and empty was just non empty optional well let's go ahead and save this away so we can easily talk about it so let's say optional integer over here and we'll call this as first is equal to and we'll just save this away into this optional now that I've saved into an optional you could have said first dart over here a first dart is present and then you could do some work with it so if it is present you could do this if it is not present you could maybe say something else about it saying that it's not present so that's one way to deal with it well that worked really well that's still an imperative style of code and you don't want to really do that ok strike that here is another alternative you could have done you could have made this a little bit more functional you could have said if you could have said first dart is present how nice that is and then you can say given a value I want to output the value E so this works really nicely but the problem is what if the value doesn't exist you are kind of left in in a no-man's land well how do I deal with it and you kind of sit there and scratch your head well there is no way to put a not operation on this so it kind of feels a little weird you have to maybe go back to the imperatives Tyl after all that is not very pleasant and maybe one day you sat there and said wouldn't it be so cool to have if present or else well why not so you have an if present or else method you could actually use and you can imagine what this is going to look like this is going to take a part but it's also gonna take an else part and as you would imagine the if part is gonna take a value and do something with it and we know what that is something that takes a value into something with it that's called a consumer but we also know something that doesn't take anything and doesn't give you back anything that is called a runnable so in this case you want to just say I don't want anything and in this case I can output whatever I want output may be a word nope over here to say I don't have anything at all to give you as a response back well in this case it's if if present or else and I can tell him what to do in the absence off it so you can see if present or else takes an if part and a nice little else part as well and you can pass those two parts and deal with it if the value exists the if part kicks in till the value doesn't exist the else part kicks in so that is a very nice little addition or improvement to the language JDK of API that's really nice to him so that is about using the if present or else but on the other hand let's step back again and talk about two different functions that behave a little kind of differently talk about the map function of the optional what does the map do it took me a little while to get a grasp of map what a map say says if an optional contains a value so optional let's say width value is called and then you say in this case this is our pilot value a dart map if you call this with a function let's say F which is going to take a value and transform it what you get out of this is an optional with a value so that's a response you get the type transformation could happen maybe the value of type X maybe the F is going give you an object of why and you may get a value of y out of this but on the other hand if you have an optional empty and you perform a map of X this will give you an optional empty back to you except this is optional empty of type X on the other hand this is optional type of empty y so you do type transformation but you continue with an empty so notice what map did map will so here's a way to summarize what we just saw here a map map transforms optional with value moves does not touch does not touch empty that's what it really is right when I said that's not touch it did a type transformation but it didn't really do anything with it useful that's the way map works on optional on on the empty but on the other hand if you take or else or else is kind of the opposite of this what is our else do our else says if the value exists you know do not touch so just just pass it so if a value exists what is our else do it quietly gives you the value it doesn't do anything with it doesn't mess with it if the optional is empty on the other hand or what is or else do inject inject value so in other words the our else will inject the value if the value doesn't exist it Valley exists simply moves it forward if you noticed notice it to a certain extent these are kind of like the opposites map will transform the value but quietly ignore the empty or else will quietly move the value forward doing nothing with it but inject a new value if it's empty but on the other hand what if I really want to do the following I want to do the following where I say if the value exists move it forward if the value exerted is empty if the optional is there is empty optional is empty what do I want to do you inject a new optional so in other words here's a way to think about it in the case of the map the map is so to say injecting a new optional when the value exists and doesn't do anything if it's empty in the case of this you want kind of the opposite if the value exists just move it forward if the value doesn't exist inject a new optional into my into my hierarchy well how would I actually do that so that is exactly the purpose of our instead of our else so if I say our over here and if I say our say our else for I'll get a compilation error because our else for is a number whereas I'm putting into a optional I cannot do that but in this case I'll say R and I will put a supplier and return optional of let's say 77 so what's going to happen now if the value exists you simply get that value out of this let's output the first right now so you will notice that when I output the first it is the value six it's inside optional if the value does not exist then I get an optional of 77 so R is to inject an optional and then you can continue programming with the optional moving forward in except that our else will kind of give you a primitive or whatever the type is whereas in this case it lets you continue even forward which is really a nice thing to do so we talked about if present or else we talked about our but there's one other beautiful thing you can do with optional moving forward and that is if you take a function will call it as process so I have a public function right here called let's call it as process which is going to take a stream of integer and in this case I'm going to case stream right here and all I'm going to do is take the stream for each and call system dot out and print it so this is really nice and how can I use it well already we can use it as we can say process and then call number starts tree and I can just pass it through and have it printed that was really nice wasn't it how well were what I really want to do is take this collection so I say numbers dart stream dart filter may be given an element element greater than 5 and then dart find first now that I have a value on my hand I want to take this value and pass it to process so that I can iterate over the value now you may look at this and say does this make sense well I think it does and the reason it makes sense this this is saying I'm an iterator how many values can I iterate a iterative process oh gosh an iterator may process zero values one value or any number of values what about an optional and I feel is a special case of it an optional may have zero values or may have one value so what why shouldn't I iterate over zero our one value I should be able to do it but it doesn't matter what you and I think the compiler says you cannot do this because you cannot pass an optional to where a stream as expected so what do we do well you could do something silly like this you could first of all figure out if the value exists here the value exists you could then create a stream of that one value if the value doesn't exist you can create a stream of no value and then you can call process that's going to be looking really ugly why not just simply do dart stream and be done with it and that's exactly the whole idea so you can take a stream sorry take an optional and convert it as a stream and then it becomes a iteration over one value if the value exists what if the value does not exist I run the code and the iteration did work and it didn't print anything because a value doesn't exist so you can see in this case I'm gonna output done and when the value is greater than 5 you can see it printed the value 6 and done if I put a 50 you can simply see done being printed because there's nothing to eat irate over so you can take an optional and convert to a stream very nicely and that's basically yet another thing we got so that's what I'm gonna stop about improvements and changes that are in the language and I would I would say I am really happy for most of these changes I think they are really good they they have improved quite a few things we can do I'm sure there's more improvements to come in the future but I think they have done a fairly decent job in my opinion so if I were to give the grade I would say a plus grade really nice changes to the language I'm pretty happy for what they provided so far in this case so that's our improvements and changes now let's talk about some things that are I would like to call as auditions so what are some of the additions to the language well when it comes to auditions you know one of the things is we always want to get get more fluency there is that's one thing the other thing is we often have programmers who constantly complain are not complained but constantly compare and complain to maybe if it was me and who constantly compared to oh oh you could do this in this other language and and you know when you sit down and code in Java it doesn't really help to keep saying this works really in this other language right and I always have this feeling when you're coding in a language talking about this other language that you used to program in is like talking to your current girlfriend our golf boyfriend about your ex that's not a good thing to do in general it only makes everybody frustrated including you and that other person so in general we shouldn't really salivate like that and say you know what I'll just live with what I have right now and the past is past the future is future in the current is current but having accepted though it still is kind of nice to have this is one thing that gets really programmers excited right you show them a little feature and they're like oh that's so nice to have and I wished my language had it so we always do this so it would always say when will Java have all these other wonderful features from all these other languages I'll tell you first of all the goal of job is never to do what every other language does actually I do have to tell you they took Java and they added every feature from everybody beautiful language that's out there when they finished it they renamed clear Scotland so it's already there right so if you want everything from every language go you Scotland be happy with it but Java says I'm gonna selectively bring some features I'm not gonna be in a hurry to bring those and there are some really good features in other languages but maybe I don't want those features right now because it may make really good sense in those languages maybe it doesn't make sense in Java to have it so they've been really I know I'm not really the word reluctant they've been selective in bringing features but one thing I'm really happy they did bring is a nicety about creating a collection so if I have a collection of 1 to 10 right now and I'm gonna output numbers right here you will notice that it says 1 to 10 that was a collection we created but what kind of collection did we create we had to really create values in place I don't want to use dart add dot add dot add that's kind of boring to do so I don't want to be verbose and boring so what do I do well I can throw these numbers using s list we've had this for a long time in Java but unfortunately though is this immutable oh is it still verbose Oh what kind of object is it creating so if I ask for get class over here you will notice it's actually an ArrayList class that it created it doesn't matter how many elements you have notice that if I had three elements versus 10 elements versus two elements it doesn't really matter I got an ArrayList well thankfully you don't have to do this moving forward in Java 9 you can say list dot off one two three four five six let's say and in this case I have numbers created as lists off notice the type in this case is list and now list n that's a specialized to type but this is also immutable so if you say numbers start add and I want to add a seven to it now when you write this the question is this will not work but the words this will not work is a very loaded word what does it mean well will it give me a compilation error well the answer is no of course not because getting a runtime error is much more fun than getting a compilation error well it cannot give you a compilation error in this case because you you are dealing with the list which is a traditional list the compiler really doesn't have the information to tell you that you are wrong so it'll wait until you run the program to tell you but in this case when I run it it says unsupported operation exception and then it tells you that you cannot do this and gives you an error at this point so that is immutability but even better when you look at the class notice it says list n but I'm going to then go ahead and remove these few values down to 3 that's a still list n bring it down to 2 this time it's a list 2 if you notice not list n bring it down to 1 value as you would imagine it's a list 1 and bring it down to 0 values that becomes a list 0 so there's no list minus 1 ok so the point really is that it really is a very specialized collection that it creates and as a result it can be better in performance as well so these are very customized implementations and of course the question is how many can you implement well you can try to push the limits one two three and you can go all the way up to maybe a value of 10 and I was able to really put these values and run the code you can see that it's a list of n it provided what if I were to take this and add more values to this even further and then even further and then even further at some point you should say dude you are doing this wrong so the limit should be our sanity rather than the compiler so even though people talk about limits you can try to push it but you obviously won't go to a very large value so that shouldn't be a concern in general I say asking those questions is a sign of bad design you don't want to say how many can I have so that's basically the idea is you can create a list like this well this is nice we had a list off in a similar way you can also create a set off as well so for example you can go back here and say a set dart off and you can create let's say in this case let's say John and let's say this is going to be a Sarah and let's go ahead and say we have another object let's say Tom over here so this is a sect obviously and we know that set is unordered and essentially when you notice the output it's not in the same sequence it's an unordered collection you can do a set-off in a similar way you can ask for the get class on this and that's optimization as well so if I don't have a Tom right here and run this code you can see that it's a set - likewise along the same lines you can also create nicely and this is something I really like what they have done map dart off I can say a comma 1 B comma 2 and then maybe C comma 3 and in this case of course as you can see we can create a map with key value pairs given together which is which is really nice but of course what if I give a key but I don't give a value for the very last one this is even more beautiful you get a compilation error ok it really got mad as you can see so you can get accomplished an error at the point and tells you you need to give a value and John will never mind lis tell you right it throws a big error storm I'm sorry I won't do this again so that's the best way to learn so you can actually put that value then you get an error and you can deal with it so the key and value both are required and as long as you give it that's great I also noticed that one of the other things I like is nice little type inference is kicking in right here which is also a nice thing to do what if I said 3.0 right here in this call well then the type is going to be elevated to a type that fits into it so you got to be a little bit careful but hopefully what your sign on the right side will get inferred what your assigned on the left side will help you to infer the type on the side and deal with that so so type inference works in more places in Java now compared to Java 7 so we've been really going in this direction very nicely so that is basically what you do with this nice little off method so um the next thing of course is about completable futures well in the case of completable futures one of the nice things completely futures as has a two different little features when you create when you create a completable future in the past you could create a completable future to do some and if you were in my previous talk I request you to just be a little have a little patience with me so I can cover this for people who may not have had a chance to look at it so I can create over here let's say concurrent in this case I want to bring in a completable future so I'll say completable future and in this let's say integer we'll call it as some kind of a future here let's actually say dart in this case supply async and with this let's go ahead and write a function we'll call it a sample let's call this as you know Factory our method that's going to generate the data well in this of course I can then call this and say then accept and we can specify what to do when the data comes in maybe I just want to print out the value so out print illan you can do that print Lin like that or you can say then run in which case you will just do some action without the data that you don't care to receive that can be very useful with the with the run so in this case of course this is going to wait for the Dare to arrive and then it's going to display the data for you what about the factory method though well in the case of it to scary things okay skur factory method right now is the factory right now this is going to return an integer value eventually but what am I going to output right here well in this example I'm gonna take some time so I'm gonna say sleep dart sleep let's say I want to sleep for about 5 seconds and then I'll return let's say a value of 42 from here so that's gonna return that value and eventually we're gonna print the value out and be happy with it so when I run this code what is going to be the output like well maybe what I'll do here is just give a sleep to wait for it so that it can be displayed and we can take a look at the value as an output well that's my little code to work with this completable future but it turns out what if this value this particular call is never going to complete or it's not going to complete in a very very very long time well if it's not going to complete in a very long time this would end up being a waste waiting for the events to happen maybe you want to know that things are never going to happen there are two things you can actually do about this one is you can save away this one into a completable future of integer right here so I'm gonna put it into a completely future of integer and then let's go ahead and call this as let's say future is equal to and and then we'll come here and say future dot but while I'm waiting for that one I can say future over here so this is a future if you future there we go so given this future what I can do is I can say future dart and in this case we can say complete on time out well this gives a way for you to declare the win and say I'm gonna send a minus one after maybe only about two seconds and this is time unit dart seconds so you can ask it to bail out with that value only after about two second delay if the process finish really fast you will get the result if the process did not finish really fast you will get the minus one and you can move on so that's basically what this is going to do but of course if a value successful or the completable failed already you'll get the failure and not your time out at the point so right there is minus one this is useful to feed some you know premature value to close and move on on the other hand if you really want this to fail rather than continue this way what you can do is you can go back to the completable future you can do this while you build it out afterwards and in this case what you will do is simply say dot our our you can say a time out so you can ask it to time out with up time so you can say to comma our time unit dart seconds so in this case you can ask it to timeout at a certain time now clearly in this case it'll generate a exception you would have to then handle the exception down below using exceptionally and things like that but this allows you to timeout also as an error rather than giving in a dummy data to deal with so that is one thing they provide with a completable future the other thing that's available which is also pretty neat with this is the ability to copy completable futures and what copy allows you to do is you can take a particular completable future and you make a copy of it and then you can execute them at the same time so this can I love you to take certain algorithms and create a branch in the algorithm and execute different parts and see how they behave and of course you can deal with them the parts completing separately versus the originals completing and you can play with it then the next thing that is pretty interesting to have in here is a is a stock not a stock a stack Walker yes stack Walker so what does the stack Walker do well imagine you are dealing with exceptions remember how we always complained that exceptions exception stack is really really huge well there's a good news it's really huge but you can now process it using the streams API so it doesn't feel that bad anymore right so if you want to really go through the call stack and process it you can do filters and maps and reduce and stuff like that you can take a call stack and you can apply a stream operation on it and then you can walk through it very nicely so take a look at stock a stack Walker if you are somebody who has to deal with a stack trace day in and day out there is some kind of relief for you and you can use that one of the features I really like as an addition is J shell so what is J shell well J shell is a pretty amazing tool in my opinion and what I like about J shell is it's a ripple that you can use I'm a huge fan of ripples almost any language that you care to use today has a ripple for example node is a ripple for JavaScript you can say 2 plus 3 right here or console hi and you can see that work right there that's a ripple for JavaScript similarly you have a scar a scholar a ripple and of course in scholar ripple you can say print let's say you know hello and that is Scala working right there so that's Carla's Ripple this is Haskell triple if you want to you can say add a B is equal to a plus B and you can call add one and and three or two you can get the result of three almost any language has a ripple well I really like ripple a lot and the reason I like ripple is I consider ripples to be this is of course stands for read evaluate print loop it reads what you type evaluates it prints it and loops over why should we care about it one of the reasons I really like about repple is it is a facilitates what I like to call as a fine-grained incremental development now I'm a big fan of automatic testing I'm a big fan of writing unit tests I write test write code write test write code write this write code but even then I think that cycle is too slow for me I have a test that's failing I've got to make it pass but when I look at the code I'm like huh wonder what what kind of functions I can put together here well I don't want to really think about it write it and watch the test to fail even that's relatively fast that's still in and order magnitudes really slow but instead what I normally do when a program in these languages is I flip over to the repple try out a little experiment a little code can you imagine where this can be really powerful imagine you're working with the spring application we imagine you're working with the web application and you can inject you ourselves into that environment using a ripple and now you are interacting with your server-side data and you can start really querying for things this is like you are given a power to you know minimize yourselves with a micro-level and then inject your salts and you're able to see the world around you that's awesome power right it's like taking debugging to the next whole level in my opinion so you can sit there and play with the environment I know this quite often when I programming languages that have ripple and I've really done this for several years I was working with a developer not too long ago and and he said how I wonder how would this be and I said weren't you try this in the dipole and he said yeah I should really start using the ripple more often because this is a habit we develop over time so it's a micro incremental development that's really nice it's nice to prototype a little little examples you can play with it it's a great experimentation tool and it's also a really good learning tool when I teach my students of our different languages it gets tiring if you have to set up projects by the time you set a project you forgot what you have been there for well you open up a ripple type in a few examples and I would just you know save that as a script and send them and say go try it on your machine they can open either a pool and play with it all so that becomes really really easy so the first thing I want to show you is how do you start the ripple to start the ripple you will type J shell and in this case that brings up the repple one of the things I learned very quickly is when I get in somewhere I learned how to get out so ctrl D game brings you out that's one way to really exit there is also another way to exit you bring into the shell if you want to exit you can do ctrl D and get out very quickly but maybe it's a very slow day at the office maybe there is nobody and you feel really lonely then you can type exit it will even say goodbye to you which is really nice so you can use either of those more depending on your mood right if I want to really get my work done and going or did ctrl D before and I have a conversation with Jay shell also it slash exit but this also tells you one other thing when I bring up Jay shell you know sometimes you have this right you you have somebody asking you question they are talking on the phone they are asking you question they're talking on phone at some point you're confused you're like are you talking to me or on the phone well that's exactly the problem here when you're typing something are you typing that to Java the JVM or are you typing it to Jay Chou well if I type something here 2 plus 3 that is 2 the JVM if I type slash over here and help that is to the Jay shell so slash is commands for Jay shell without slash you're sending commands to the JVM here's another beautiful thing you can do here if you ever had to teach Java to somebody remember that that fateful day they came to you and say yes Java EC you said Oh Java is simple don't worry about it can you teach me something simple how about teach you hello world and they're taking a piece of paper and say yes I want to learn word you said let me show you hello world and what did you do first you went to the editor for them and the first thing you did was you said public and their hands goes up and says what's public you tell them you don't need to know that right now they take notes public no need to know that right now then you set class they say excuse me what's a class oh you don't need to know that right now then they say class no need to it right now then you said hello world and and there's a glimmer of hope in their eyes they're like finally and then you put a curly bracket and they're like okay what does that mean then you said public they immediately a very smart student they look up their notes public I know that no need to know that right now then you sit starting and they lost they completely lost it and they ask you is static also something that I don't need to know that right now and that is a lot of baggage on our shoulders isn't it well we don't have to do this we can simply say system dot out dot print learn and I'm gonna say hello and isn't that beautiful and for the first time ever Java with no semicolons this is awesome this is amazing isn't it so you can just type that little snippet and you can let it run you kind of even wonder right I mean the other day I was writing code in Java and the compiler said you are missing a semicolon and I had this argument I said I'm not missing it really you see and the point really is I don't want to be doing all the stupid things I don't have to do thread darts sleep 5,000 I'm sure you've had this nightmare right you are you are trying to call stated sleep and the compiler says you will handle the exception and you spend a sleepless night thinking what do I do with this exception now well you don't have to just call the sleep five seconds later you get the response back as you can see that works really well also so this shows you how you can remove some of the sory morning of course if you're gonna write a method you'd have to bring in the semicolons and the necessary try blocks because the methods that you write have to be complete in the syntax and also we can take these methods and bring it over to code and that can be very useful so we went in and tried a little hello over there these are called the snippets of code and we can try different snippets for example I can say 4+3 here to get the value of seven I can deal with that really nicely it even saves it away into a variable which is dollar four so dollar four plus twelve right now is 19 as you can see that's 12 plus 7 which is 19 so you get that value really nicely also so you can use those kinds of expressions and then we talked about exiting also but here comes the charm you can use the up arrow and the down arrow back and forth and you can reevaluate the things you already wrote so you can use the up and down arrows and then similar if you want to go to the beginning of this you can do control a you want to go to the end of it you can do ctrl e if you want to go to the other words you can navigate in other words I simply blindly started typing things I'm already familiar with in the command line and almost everything worked here as well so that's one of the nice things is all those things really work nicely so you can do that and then of course here's a control you to get rid of it and and I'm a person who could like to keep things clear in front of me I have this habit of typing control L all the time to clear my screen and that works also really nicely control L to clear your window that can be really powerful so you can use the up arrow down arrow control a control e and then comes along something really really exciting let's say I create a string greet is equal to let's say hello and of course I want I'd greet right here but remember how some developers tell you they love I des because in the IDE you put a dart and before it could blink your eyes it would oh here you could do that's really interesting isn't it so you can get a list of methods as well well you can just do that very nicely or you can say concat type minimally and hit the temp and then something even more beautiful you type the tab and then you type it one more time it even gives you the Javadoc can you believe it I literally cried that day when I saw this this is like this amazing so I I'm in when I'm on the flight I don't have to go look for stuff I can put a dot and see that right here I don't need to use as you know IDE if I don't want to I can just look at it right here so that's really nice as well and you can take a look at the beautiful capability just double down - a double tab rather this is the documentation for it - which is really nice what if I wanted to repeat a command I just typed so for example if I want to type over here let's say hello well I did type hello but I want to repeat it I remember slash as a command to the J shell 1 I'm asking you to type the command 1 that was whatever the command 1 was and apparently that was 2 plus 3 or I can type this over here and say slash bang and that is saying execute the previous command so this is much like you were UNIX environment where you can put a bang bang and run the previous command in this case a slash bang executes the previous command for you and you can use that so that's a nice way - n is to go execute that many commands further out so - 3 is the third command moving backward from here similarly you can look for your current imports so you can say imports and see what imports are already loaded if you want to really use your own environment obviously you have your own classes you have your own stuff and you want to be able to look at those when you start J shell use - - class - path not class path class - Pat and when you say clams plan star - path you can load those classes in your class path for glacial to be using then you can start querying your own classes import them and use them or you can set up a script which will import them ahead of time you don't do it you don't have to manually type it then well but if you don't do any changes these are the default things available which is good and bad in my opinion what is good they said you will give you util for free which makes sense have you ever written code without using util most of the time we use it nice welcome io mat ok net yeah maybe they think concurrent is needed this is the one that really surprises me they think that we need to have reg X all the time but anyway you can have that and you can deal with it that's available for free you don't have to be really do import that's nicely available and you can use it if that's what you want to do so that's your important you can do a list didn't save which is really nice to find a listing of all the source you wrote so you can say list over here and that's all the code we wrote similarly can do a save and put it into a file name and so after you type all of this and play with that you can dump into a file similarly if you already have things in your file you can just open it and execute it right so you can load it as well which is really nice I want to know what variables I have in my Corp so far we will tell you what your variables are I don't know if you remember but I define greet and that's there and the other internal variables it created you can also do var if you want to do it's the same thing similarly M for all the methods you have similarly T for all the types or classes that you have had you written a class my class over here and now you say T you can see my class is appearing right there so that tells you what types you have interfaces classes and stuff like that but one of the really cool things about this is not only can you add these things but you can also do a couple of other really exciting things so for example I have great over here and I want to change it to high for example I can change it this way but you can also do something a little different you can do a slash edit and then you can put greet right here and it opens up this beautiful little if it you know what we call it a display and you can just go in here and change this to some other value right here and then when you're done with it you can click on accept you can quit on it and then you can ask for what greet is isn't that cute that has that value right there why is this something you should do well if I want to do a not a exit but edit well I can just do I edit without anything as you can see then it actually gives you a lot more broader scope of things as well which is really powerful so if you want to quickly make some changes or put a little bit more multi-line functions it's a little I know hard to do this on the command line you can open up this type multiple lines on it then click a save and it accepts it so this can be very very powerful way to really make those changes so slash edit gives you that little thing to play with as well so you're not confined to just the command-line available to you what are the other real beautiful things I already talked about semicolon try and throw well the drop is a really phenomenal function so V tells you all these things I'm gonna drop dollar eight over here and I drop the variable now if you look at the variables you don't have eight over here you can drop all kinds of stuff so I have a class called my class drop my class and in this case I drop the class no longer present so you can drop them from your environment as well well what if I said void foo and in this case I'm gonna say output a fool called and then of course in this I'm going to call a function called bar semicolons are required and I'm gonna close it well you know very well we don't have a bar so it gives you a little warning it says created method foo however it cannot be invoked until bar is declared you know how programmers are when you say you cannot call it they want to call it so what is gonna happen if you call it it says I told you don't call it right so attempted to call foo which cannot be invoked until method bar is declared so that's what it tells you now I'm going to create a bar method in this case of course I'm gonna output a bar and then of course I'm gonna close it now bar is available now I can call foo and that called the bar but then you come along and say aha I'm gonna drop bar and maybe the jeschill is sitting and shaking its head right now it's like what are you doing but then I call foo it says big here we go again you're not supposed to do this so you can drop stuff you can put them back there's a nice little experimentation environment it's it's it's a playground isn't it this is when Java is actually kind to you and just have a good time playing with it and you say is it ok if I don't use some icon and it says we are just having a good time keep going and then you can play with it so that's basically what this is doing it gives you that ability to just be playful and tries write write some code there are a few limitations of course you have to run into up from time to time you will cut off a run of but don't be too unhappy with it for example J shell doesn't really care about final so if you try to do final let's say in a equal to 4 J shell says I thought we were having a friendly conversation here now you're trying to use legal terms on me don't do that so it doesn't want you to do anything really strict and you know a really formal just kind of play with it be happy so there are a few little restrictions that you have in this case and we started finding variables you can also infer the variable using a really good shortcut key which is called a fixed shortcut key in this case it's kind of like what you do in editors like IntelliJ you define a expression you press that key it'll fill in the pipe and a give an opportunity for you to define a variable name you can try to find variables as well really nicely and then of course you can define it as a different type also because remember this is just an experimentation area you can define methods we saw that you can redefine methods you can define classes you can define interfaces to find the methods find the types you saw me do all of that we did it forward reference also remember I call foo without defining bar so I was able to call call bar in it you can also look for history what should have just been history and list list is a listing of your source code history is every interaction you had with the J shell environment so if I say history over here notice that it's a drop and the bar and everything sitting there list will contain only the code of course so you can look at both of those and look at whatever you want to look at in terms of looking into it like I mentioned earlier I really have to tell you that I love J shell it is one of my favorite tools I like to really get instant feedback and play with things and I'm just experimental in nature that's just the way I'm wired so this is a tool that I really definitely like I've used ripples in a lot of other languages and as a result I like ripple in Java also some really happy that they they have that in Java 9 well that takes us to the end of the second part in here we talked about the improvements and changes we talked about the additions now we want to talk about the last category here which is the advanced as I would like to call it and in that area I'm going to pretty much talk about one big elephant in the room which is modularization now modernization is a very key feature in Java 9 maybe the most disruptive feature and here's my take on modernization modernization is like parenting children never appreciate you when you are parenting them and they eventually come back 4 years and say you're not that bad thank you and that's the way we're gonna feel about it that's my optimistic mind telling me we're gonna kick and scream and complain about modernization now but in the longer and when the dust settles I think we'll be thankful that we have this so we got to talk about marginalization but I think we should do that in the next module if you don't mind so why don't we take a short 10-minute break when we come back we'll continue with modularization after that thank you all right thanks for coming back let's get started with the second part here we're going to talk about modularization let's talk a little bit about the problem we are trying to solve and how modulation can help us with that but before we talk about it let's talk about something we've seen in the past well we've had solutions like OSGi available in the past I have used Royce di I've really liked it os di is extremely ambitious it does a lot of things Java modularization does not do enough of what OASDI does so it's a very small part that motorisation actually solves in that sense Java Nine's motorization is not ambitious it is rather very practical and pragmatic they want to take one problem and find a fairly good solution to that problem or our small set of problems then trying to make something work for a lot of different combinations and possibilities so so the objectives and the goals were extremely different but what are some of the problems we deal with the very first problem is the jar sizes does anyone remember the size of our T dot jar there you go still counting right so that's 68 Meg's in size that's a big big jar raise your hand if you you swing okay very few people right but the rest of us don't worry about it you have it with you and if you ever need it never worry about it right it's there you can reach and that's like saying you got to carry these three bags on your trip what are these three bags don't ask the question just take it with you right that's how it feels you got everything bundled up together whether you want it or not and you just keep lugging it around and that's basically what we have been doing is this manola take big blob you've been carrying with us there is a lack of clarity of dependency what does your code depend on and there's a little gulp ants like well let me see let me find out the other day somebody told me hey don't you think you can find this using maven and it reminded him nobody uses mavin mavin uses you so it's something you have to keep in mind right so it drags you in beats you down and says sit down now and then you start using it well but I also keep in mind whether it's mavin are Gradle or any tools that you use a gain of greater self falls back on a lot of mainland repositories to get the details but the point really is it doesn't matter what tools that you would use all of those are compile-time dependencies what about run time dependency well the short answer is there is not a whole lot to deal with that information we are pretty much lacking in that area the other problem is public is too open you write something and say hey don't use this please and then people start using it it's like I told you not to use it but you said please so I kind of took the liberty well but public is to open what if we want to create something but very much clearly dictate permissions us to use it or not that would be really nice and the last thing is how does the classpath behave in the classpath you have two options you have a fail late or fail early which one you think is better fail later fail fail early fail early is better right fail fast is better well imagine you leave the room without the keys it would be nice if you can know that you left without the key by the time you leave the room then walk you know three kilometers and then find out you forgot the key back in the office that would be really painful well in the same way let's say you deploy your application it runs for five days and that on that fateful afternoon the user hits a feature and that's when the classpath bunks on you and and I always feel like Java when you compile Java and start running it Java looks at your class path and says oh this is going to be fun and it's just waiting for you to fail eventually right that's not really fun at all because you would rather know that this is now going to work when you start then to be known later on that this is not going to work so fail fast is very important we're gonna see this all in a little bit and see how this can work so this brings us to the so-called modularization technique but one of the really beautiful things about this is packages now go into modules but what's really cool about this is that the JDK itself has been modular rights how many modules do you have in the JDK well let's find out so I'm gonna go back over here to the command prompt right here and in the command prompt I'm gonna ask the question let's kind of break bring this out so in the command front I'm gonna ask the question Java and I'm gonna say list modules list modules tells me all the modules that's available for you in Java here we go boom and that's all the modules how many are there well let's take a look at it that's how many there are that one little orchid our jar well one big our tada jar we saw has been broken down into all of these things let's see what are the some of the things we have we have Java dot activation we have Java dart base Java dot base is one of the most important things this is a while where your java lines and stuff like that are contained then there is java dot compiler and there's java dot core it never goes away okay so you can see that all these things are sitting there and you can see all these modules and how many are there well you can see if you notice wc- l my gosh 94 modules in this case so keep that in mind 94 module sitting in there that's how our JDK has been broken down into all those modules well some rules the first rule is no cycles at compile time the every single module depends on one module which is Java dot base except Java broad base itself doesn't depend on Java copies so every ma other module depends on Java base and as a result you have this hierarchy built up in front of you and you have clear dependency being specified and you have all these things with no cycles at compile time so what's a module a module is a collection of code packages you know source code while compiling to bytecode obviously data and then of course you have resources and stuff like that a module has a name a name is simply a reverse DNS name so make it as unique as you can so that's a name and then it has two other things it has usually a requires and you have exports requires tells you what you need exports tells you what you are giving well a couple of things to keep in mind about exports and requires and modules so the very first thing to remember is when it comes to requires the requires only modules you always require modules you never require packages and then when it comes to exports exports only packages so you will require modules and you will export packages it may appear a little strange in the beginning but once you apply logic to it it becomes really easy what your your module contains packages so you will export packages you would say I have seven packages I want to give you three the other four are my business I'm not going to give it to you so that's why you export packages but why not require packages the reason is that's based on this on the release reuse equal MC principle the release reuse equivalency principle says there everything you use is what you should be releasing as one bundle not part of it that would become reusable so you would not want to really request for parts of a module you get the entire module so you give modules you give packages but you get modules on the other side so that's just where you are going to build this and you're gonna have these kinds of facilities available now given this of course we are gonna build around these modules let's take a look at example of how to build it now before we do that couple of small important things the first thing to keep in mind here is that when it comes to dealing with let's say Java 8 Java 8 was all about how you modify your code isn't it well that's the way Java 8 was built it's all about how you might you know change your code how do you design your code that's whatever it was all about well that whole game changes when it comes to Java 9 Java 9 has modularization has very minimum code change it's almost all about how you actually work with code so to make my life a little easier I've got a few scripts here from the command line no belts and results no other tools purely Java Java C and Java are we gonna use to build this stuff so let's get started with a little example so the first thing I want to do is to build a module so to build a module let's first take a look at the code we're going to create so for this I have a directory called one underscore creating modules I'm gonna open up all the Java files in here and you will notice that I have a class called Fibonacci well where is this located it's located in a package called calm down agile developer dot util so that's the name of the package I create comm dot dot util and then of course it imports this my helper from this other package and Fibonacci says excuse me you will notice that it says well it's purely Java code nothing really exciting close that out then comes along helper interface that's also part of util great then comes along my helper my helper is part of impl and it kind of gives us a wind you will not want other people to get access to your impl package directly so we won't export this well great so i have this my helper sitting right here and my helper also has a hidden if you notice we'll talk about all these and see what we can do with it later on so that's basically what we have so far now comes along one interesting file called the module info dot java now what is module info dot Java notice the file name module - info dot Java if that name looks weird to you it does definitely YZ weird so there's only one other file that's as weird as this which is package info dot Java package info Java was created or was introduced a few versions ago in Java and it contained by far the most useless information a little package love documentation model info is not like that it actually contains some use something useful what are you put within the module info the first thing you put in module info is module first of all a wait a second stop right there module wait a minute well does that mean module is a keyword so step back to Java Phi for just a minute what they really wanted to create was for each and then string name in names would that that would have been so beautiful isn't it but they couldn't do that why because the problem was what if people are using for each already they actually found out a lot of people are using in as a keyword so this was kind of like gosh what do we do about it so they figured out let's put a stupid colon in here and as a result that's not a keyword we can use it every time I type this I go into a corner and cry because this is not as elegant in fact I was on the flight yesterday writing some code and I had inadvertently typed in that's my deeper inner likings and I say in and Java says I don't know your syntax of course you don't know and and I start coding away after that right so that's really awkward but you had to do that that's because the keywords are significant in languages and as sort of so they couldn't use it fast forward to Java Java 8 what did they want to really do in Java 8 they wanted to say interface and you have an interface called util you want to write a method called foo but the method foo had to be implemented so you want to say that you are implementing this method not abstract but you are implementing you could have said implemented right well you cannot do this because I'm sure somebody has a variable called implement at some place in the world so what did they do they were very clever they said default there's a reason why they called it default because default is already a keyword in the sewage statement so they hijack did and I'm so happy they use default rather than colon one more time so I'm happy for that right this is really good but the point really is we are back to square one aren't we every time the language needs a keyword you're like gosh I'm gonna compete with the world of developers using the keyword now enters module now what now well thankfully module is a very interesting variable module is a keyword only within module info dot Java if you're using module anywhere else in the code whatever that means to you it doesn't mean this module and that's one of the nice things so it's a very contextual keyword rather than a very broad keyword in your language so in this case module and I'm gonna give a name for my module and I called it as comm dart agile developer call it whatever you want to call it with a proper you know the end reverse DNS oh by the way you cannot put a number so don't don't play to put a number into it it doesn't work with numbers just be little civilized and give a good name and be happy with it don't get tricky on it so this is a module name that's what you're providing so there are thread there's the module and then you said calm down idle developer in this case I'm gonna make some mistakes as I work on this some of the mistakes are intentional some of my mistakes are not intentional I won't tell you which one is which so in this case I'm gonna just leave this as a module for now and notice what I'm missing I don't have any requires I don't have any exports well what I could do here is I could have said requires I could say Java dot base this is totally unnecessary because every single module automatically requires Java dot base so you never ever have to say this and remember have you ever written a program where you did not include let's say Java dot util mostly we use it and yet you have to always say import well thankfully you don't have to do that for things like job at our playing days because things like Java dot Lang or so you know useful you use that almost all the time well so absolutely it's going to be available for you all the time so you never have to require so in this case that's all I created what is this gonna do for us let's go ahead and compile this code so up to this point what has been different only those two lines are different right everything else was Java eight code are Java 7 code or earlier just these two lines are the only different why does this module info dot Java go well back here I have a directory called comm dot adult developers under which I have comm as well developer under which in pull and you told but if you notice right at this level under the comm agile developer I have the module info dot Java so in short the module info sets are the same level as the beginning of your package hierarchy so if your package is called a dot b dot c wherever that a directory is there put the module info in that directory so that's where it sits well with all that said let's now take a look at how this is going to work so i'm gonna blow away my output directory I'm gonna create a EM Lib directory under output then what am I going to do then I am going to ask it to perform the following I'm gonna create a directory classes compiled Java C minus D notice no magic all I'm doing is compiling every single Java file in my directory including module info dot Java so all that is just to run Java C but when you run Java C Java now knows that special named file called module - judge a module dot info - info and I put a jar compile it into util dot jar again jarring doesn't appear really that difficult at all different at all occasionally you can give a module number to a jar file let me just get that out of the way by talking about it right now module info has no module number has no meaning whatsoever so module info information will contain the module version number the version number is a glorified timestamp nobody cares about it nobody uses it so so if you want to put a module number be happy and put it maybe in the distant future we may have use for module numbers right now we don't so that's basically what we did we just compiled it well once we compile it let's actually compile it make sure it compiles and see if there are any errors hey good news there's no errors let's look at the output directory M Lib there's our util dot jar sitting a module is just a jar file but it's not just a jar file it's a modual file well how do we look at it how do I know what's in here well to do this let's take a look at the jar and I would say in this case minus D minus D will give me the description of this jar so I can find out what's in this jar file so minus D is a useful command so I'm gonna run the minus D on this and it tells me my jar is containing a module called comm dart agile developer it only tells me that the module contains that but it also contains a mandated Java base it also contains an in pull as and the Narrows contains a util so it tells me what packages I am holding on in my module that it tells all so well but I didn't export anything keep that in mind for a few minutes so we created this we requires and exports we quite didn't do that don't come back to that later now I want to use this on the other side how would I go about using this in order to use this let's go ahead and go to this second project and play with it this is going to be using this code so if you notice right here i have something called first user the first user is part of the comm dot agile developer dot user and in this case what is the benefit we guard out of this well let's talk about this real quickly when it comes to class path if I have a package in my in my class in my jar and I have a package of you know internal class somebody could come along and say I got a great idea they could create a jar with my packages in it and and then of course they get a backdoor to talk to my classes and what does Java do when it sees multiple jars which have the same packages in it job alicetin says well these are very interesting people and that's all it says right it doesn't really complain about it well that changes now when you have modules a module cannot exist in two jar files in the same directory so if you have two jar files in the same directory and if both are saying the same module name John will give you an error if you have two different module path directories but you have jars with the same modules with the same name in both it'll take the first one ignore the second one so you will never have a conflict in there and a collusion where you can probably bring in inappropriate classes and start interfering with this that that safety is there now you have a first user main I bring in Fibonacci with an import code looks exactly the way we write code in the previous versions of Java and then of course I call the Feb up in tada I have a use helper what am i doing in the use helper I'm asking for the Fibonacci's helper which is the helper interface and I'm printing it out let's leave it at that we'll come back and see what this is going to do so now this is called comm dart agile developer dart user but what do I want to do I want to create a module for this so I'm gonna say module and then I'll say come agile developer dart first is the name I gave for this module again no requires no exports at this time well maybe I could do an exports who cares so exports and what am I going to export right here I'm gonna export the you tillage that I have with me this guy this guy saw a user package I have with me I'm gonna export the dart user ok so that's all I did let's go ahead and compile this and see what it's going to do so if I compile this now I do a Java C I provide the minus B that is for the module path not your class path so going forward we use module path and we will not use class path well I'll change that statement a little bit later during the plate of transition we may use a class path but in general we'll use module path then I say where the destination is compiled all the files in the underscore to underscore using modules directory and once you compile jarred them all up using first our jar and the main class I want to use here as the first user so I put the main class when I compile the code and then of course I'm gonna blow away all the all the classes this will not compile unfortunately why wouldn't this compile well the reason it will not compile us in the way this actually works is think about this as a handshake I can I may extend my hand that doesn't mean a handshake happens you have text in your hand as well for a handshake to happen so both of us have text in the hand for handshake to happen so this has to export this has to require if I did not export or if you did not require then there is no handshake well in this example I did not export my package and the first Pam module did not require my module either so there's no hands involved at all so there's no handshake so it's gonna fail so when I run this let's see what it tells us I got an error let's look at the error it says package comm a dil-don't for you tell is declared in the module comm a july / but module comm agile developer first does not read it I have to admit this to you the error messaging is beautiful it actually is something can I finally understand usually when you get an error you call a team of people to ask you to read it for you and everyone has an opinion this one kind of spells it out really nicely right so very well-written messages I really like what they have done here so what am I going to do to look at this well the very first thing I'm gonna do here is I'm gonna go to the agile developer module and I'm gonna say exports and I'm going to export calm down agile developer dot util I'm gonna export it notice I did not export the calm Dajjal dot impl impl is mine to keep I'm not going to give it out right now but I will export the util for other people to use so as author of a package I have exported one package I have with the other package as internal to me and that's what I'm keeping at this point well that's great so far now on the other side what I'm going to do here is I am going well this is not a nerve right so when I go to compile this core I still get a compilation error why because you're not reading it still so what am I gonna do now I say read well requires and what am I going to require in this case comm dot what wrong place pardon me I'm gonna go to the other module which is going to be the one we are going to use in the two so this is gonna be module info and this is from the users and what am I going to do with this module info well with this module info I'm gonna say requires so requires and this is gonna be com dart agile developer that's what I'm gonna require remember you always require modules you exports packages so in this case the first is requiring comm agile developer and the comm adele de l'opéra is exporting util and of course my class wants to use Yuto so everybody is in harmony so when I go back and compile this code in this case notice that it did not complain anymore well that's great no complaint anymore so let's go ahead and get rid of this information so we don't see the output but I want to run the code now so what's gonna happen when I run it here are two ways to run it Java minus P give me the module path minus M which is for module name and tell me the module name or tell me the module name slash and the name of the class you want to run as the main class so if you don't give me the main class it uses the main class already kind of compiled into otherwise you can change the main class by so notice that in this case you don't space a the main class alone you say the module name slash main class to run it so let's see if that works so if I go back and run this code you will notice that it calls the Fibonacci and it did right there is the output it also was able to get the helper and print it out notice it says my helper because a real class is my helper even though the interfaces helper and then of course you can see that it's a hidden method I was able to find out it has a hidden method we'll come back to those and talk about it I just read ran it second time and you saw that appear both the times when we ran it so those are the two ways to run it I'll remove the one way to run it so it doesn't appear so noisy so we can look at the output when we run this and it displays the Fibonacci value this plays the helper object and also tells us what the my helper contains those are the information we saw so far so what did we do up to this point we looked at readability at this point we also did exports public is not the same anymore in in this case notice that I have my in this in the in the code I have so far I have a class called my helper remember my helper is public my helper is public but it is part of the imple package remember I never exported the IMP imple package I never gave it the imple package so what's gonna happen now because I did not give it the input package if I go to the first user I got the helper here as you can see but if I say over here my helper dart helper in fact I'm not even gonna do anything more just put it equal to null as innocent as status all I'm doing is I'm mentioning the name of this particular class right what's gonna happen when I mention the name it immediately says you shall not mention such names right so it's it's unmentionable because it is completely hidden it is you cannot see it so you cannot talk to it that's what you just saw here so you cannot really talk to it because talk about it because it's supposed to be things you don't see you are completely hidden hidden from you we'll talk about that a little bit more public plus exports is visible now well notice how we had our Bonacci class it's public but it's part of the package which is you told that was exported so we are able to access fibonacci public with no export is not visible and this is true at compile time and it's true at run time as well and that one of the beautiful things about it notice I go here to this code I am not accessing my helper now but what I'm going to do is I'm gonna quietly use that using reflection so notice I went to the helper and I said a helper do me a favor please you are an interface could you tell me what your class is yeah yeah I'll tell you my class is my helper but you're not supposed to know that it's a string you cannot do much with it then you say hey you get me your method called the hidden it says okay I'll fine I'll give you a method called hidden you got the method called hidden well notice when I have the output shown up here you can see the method hidden being displayed right here and now you're like oh this is awesome let me get this done before anybody looks at me and then you're gonna say method dot invoke and Java says I've been watching you right and it says hey I this is this is how far I'm gonna let you go not anymore and slap and back to your desk now right so that's basically where you can't do that job so when you run this now what's gonna happen well at this point bloom error first user cannot access class my helper in the module because module agile developer does not export impl to the module first that couldn't be any more descriptive right really nicely done and they tell you you cannot access it because you cannot touch it it's not visible to you you didn't ask for it they didn't give it to you well okay so all right let's comment that out for a minute but before we go any further I want to show you one more thing which is extremely valuable and that is let's go back and run this and make sure it's all working fine and notice it worked just fine great but let's go back here and only grab the Java command right here put it on the command prompt directly and that is working not a problem at all but I quietly sneak around and I go to the output directory and I believe eat util dot jar so I got rid of fetal dot jar in the world of class path what will happen in the world of class path you'll start your program and Java is just sitting there and chuckling there's going to be fun let's just wait and watch well and then it's gonna crash and burn later on this is all about fail fast now notice I got rid of the you till now I go and run the program with the first an error during initialization of boot layer find exception module agile developer not found it is required by first this is because the dependency graph is available at the beginning of your program every module specifies what it depends on dependency is so Java is able to quickly build the difference dependency graph and verify that all the members of this graph are available if any one of them is not available it can immediately tell you sorry can take off this is really broken we don't want to fail mid hair right that's what it's saying really so so that is already built in as you can see I'll just go ahead and rebuild it to regenerate the util class and things are working again as you can see so that's what we saw so far one small detail I want to quickly talk about is a slight variation if you go back to your module info and this module info where is this this one here here's something you could do notice how I said exports calmed art a jalopy dot util well maybe you say exports calm down agile developer dart I am PL but I don't want this to be available to the world of programmers I could say to my cousin right my cousin only gets this not everyone else if you want it you gotta be my cousin too well the point really is I can specify my module that I am exporting it to this feature also has another name it is also fondly known as bad idea so you know it's usually not a bright thing to do so don't do this too often this could be something you do very rarely if this reminds you of friend in C++ that also tells you why this is a bad idea friends generally are not not a good idea I mean I'm talking about programming voice so keep that separate keep it clean why is this such not a good idea the reason it's not a good idea is all of a sudden we are now introducing coupling to other module names that can make our things brittle right so then as you start expanding it this can become really messy so in general don't do this if you did this this my cousin module will get access to this I am piell's public stuff but no other module will get it but in general don't do that so so let's get rid of that so that is about export too that's why I avoided that it's better to not do that but let's talk now about employed readability so to understand employed readability let's go back to the problem we have at our hand and understand what's going on now notice in the first user the first user says I am using Fibonacci well yeah sure you can because the agile developer exported it you are importing it awesome not a problem but when you look at this over here this guy has a method called hand over what does hand over do hand over says I'll create a Fibonacci and give it to the caller let's understand the consequence of this let's say we have a Bob over here and Bob has a module we call it as Bob module and Bob module is going to export the packages well we here have Sara module and what a Sara module do Sara module you know requires Bob's module so that's working really well now bob has a class called b sarah has a class called x now what Sara does is sarah has a function or a method in class X that says a get B well look what just happened sarah says I am generously taking this object B and giving it to anybody who asks for well now I have a let's say in this case Jane module what does Jane module do james module says I am going to require in this case and I'm gonna require Sarah's module well good news Jane can use X right so X is available to Jane because Jane and Sarah are shaking their hands now at this point independent of anything right nothing to do with X if I say newbie this is an error right why is this an error because I did not shake Jane did not shake hands with Bob so you cannot get Bob's class because you don't require it well so you cannot do this great but I am going to then say the following instead of this I'm gonna say well X well B object B equals let's say new X dot get B if I do this now what just happened well I am accessing B over here but I'm accessing be using the object X which is allowed which is legal but when I get B it returns an object if I said object B equals new B this is fine this is not a problem at all Java will not stop you Java simply says this guy gave you some object you're holding on to it play with it that's fine right on the other hand if he changes to be no we have a problem because you don't have access to be directly because you never request it for it now this can be a bit painful isn't it let's see what we can do about this pain so this is the hang handover method so to understand this let's go to this third implied readability let's open up all the Java files in here here is my second user second user say I'm gonna use first user hey no problem this is like the Jane and and the Sara's class I get the first user how do I use this stuff very simple my package is called another so I go to the module and say module come angel' dart ii so ii is a my package and i'm going to say exports and what am i going to export from here i'm gonna export this another package i got a do a requires requires calm agile and this is gonna be you the first that's the module I'm gonna bring in so all this is fine so far right we brought that in we're using first user everybody should be happy so far so let's go back over here compile all this stuff together and run the code so far now when I run this code you can see it is going to compile all the code in the three underscore employee readability bundle up to do a sec a second jar and then just call it so when I run this hopefully it should all work just fine with no problems at all so let's go ahead and say build darn Sh and see what it does well there you go you got the first user no problem however now I go back to the code and say hey hey you go ahead and second user go ahead and ask for Fibonacci from the first users hand over this is exactly what we are doing right this here wants to reach over to this but through this in other words we are doing this what's gonna happen now I got the Fibonacci and then I want to call the method on it the short answer is no you cannot do this because you're not really doing a handshake you have no direct dependency on it so when I compile this code you get a compilation error and the compilation error says module a jold over second it cannot be used in this case right here's the error and it's saying sorry ii does not read it there are two ways to solve the problem one way to solve the problem not a very elegant way is to go to the second and say requires come agile developer this is a boring way to do it if I go back and run the code this time because I did a requires that is perfectly fine it worked but that's kind of boring instead of doing it this way why is this such a not a good idea the reason is I didn't really need it directly I'm using it because first is giving it to me and I'm using it through first so rather than having to take this pain there's another way to solve the problem and that is let's go to the module info but this time we go to the module info of first and here you say I require agile developer but I'm not going to require it for me I'm going to require it for me and everyone that requires me so I'm being generous here right I'm not only gonna take it from you but I'm gonna take it from you and give it to everybody that is taking stuff from me to and that you do by providing before I go further I want to make sure I removed the other part I thought I did yes I did let's make sure the code fails before I fix that and there it failed okay now let's go back here and say requires but this becomes transitive right so you're making it transitive transitive means I require this but if any one requires me it's as if they require that one so by making it transitive you have now drawn a relationship from here all the way up to here automatically for this so making this transitive draws this bridge if you will right so now it's as if this guy has made a require on this particular object and so as a result that ties it down to that right now so that dependency comes in let's see if that works so go back and run this now and you can see in this case oops I did I save it so so that's got to be transitive oh you know what wrong place isn't it it should be in the first not in the second I was sleeping at the wheel God God hold up the wrong module file so let's fix it so I go to the other module file first there because that's where I want it to be so that's going to be transitive so transitive and now that we made it let's go back and run the code and that's what we are doing is going to bring the access to it okay so we saw how to make this implied readability right so this implied readability simply gives a nice way to take your reads and make it available for the the modules that require you let's talk about reflection and encapsulation and cancellation says you cannot use what's public you can only use what is public and export it either export it to everybody or exporter to the cousin right to the specific module so you ask where it's being exported to that's basically what you are asking for so to do this to understand this let's go back to our code here in the fibonacci class the Fibonacci class returns get helper returns helper but what is helper helper is my helper well my helper is is public but my X my helper belongs to you pull and input has not been exported so for all practical purposes my helper is hidden you're not supposed to get to it if you look at my helper I have a method called hidden you and you call it it says hidden method called that's all we have right now as a result when I go to the first user in the first user I am going to get the helper and then I'm going to print the method then I'm gonna invoke it and you saw this earlier it gives you an error so it says sorry you cannot do this so there are two things to keep in mind the first thing to keep in mind is when a class is hidden in this case my helper is hidden you cannot touch it at compile time period sorry end of story go home you cannot touch it right so if I want to use my helper you cannot use my helper why because my helper is hidden it's public but the package has not been exported so if I try to cast help her - my helper we'll get an error let's not even go here to the invoke yet this is a compilation error you fail at compile-time right it says no you cannot do that it failed at compile time let's go ahead and go ahead and comment it out real quick and compile the code one more time to make sure it compiles and you can see that it compiles now so model the story you cannot touch it at compile time but what about run time hey hey wouldn't this be cool I'm going to say over here a class dart for name and I'm gonna provide the name of the class to be able to get to it right so I'm going to ask you to get me the class well what is it gonna give you it's gonna give you a class over here and this is gonna be class that you get out of this well but notice this is class not you were my helper so you even really tight this to my helper still this will still work but I cannot still use it by the name well if I cannot use it by the name how do I call the methods on it right well hmm here's an idea I can go here and say I have this thing called helper so why don't I say helper Dart hidden well not so fast if you know this helper the interface helper has an ADD method it is implemented by my helper I can call add because add is visible through the interface the implementation is going to run it but hidden is not visible through the interface the code won't even compile now right because a code doesn't know what hidden s on the helper the only way can call hidden is to cast it to my helper timeout I cannot cast it because I cannot touch it in the code compile time hmm all right because it cannot touch it using the call compote compile time why not just sneak around use reflection well I cannot use reflection either because it's encapsulated so when I run this code the code compiles but it fails during runtime we saw this earlier so that didn't work also hmm where do we go from here well let's step back and understand this if you want access at compile time exports the package with me if you export the package what's going to happen then you get if you exports you get access at compile time and as a bonus at runtime you may argue what's the point in getting access at compile time if you cannot access it the runtime right so make sense however I don't want access at compile time but I want access at runtime right it's ok I don't touch it during compile time but I want access during one time can you give me that yes and the way you do this is you go back to the module info there are two things you can do in the module in for a moment the input was part of this is part of this I can say Oh pen this will open every single package in your module for reflection access that what this does if you put open up there don't do this too easily right let's see if that works so go back and run the build and when you do did you notice hidden method called because you opened up access to it I still cannot touch it during compile time right because I didn't export it so during compile time my hands are tied but you're in runtime I can sneak in and get it all right one thing no but I don't want to do that why not because I have seven packages as an example and I don't want to export all the second packages what do i do then well then opens and I'm gonna say calm agile developer dart I am PL so I'm opening just that one package for access only not everything else a quick note please notice the word opens versus the word word Oh pen right little subtle difference you're all pulling the module the module opens this package that's why it's called opens right so just a little s in the end this one is a little bit better than putting open at the module level much better is not to use it at all right so that's basically what you can see now that I removed all of that what is going to happen in this case of course if I don't open it then we're going to get into a compa one-time error because that's no longer visible and as a result you get the error so what did we learn in that part what we learned in this part is the reflection is closed but you can get access to reflection by either opening a module or opens the package so that's a little way to break through that so far so good but I got a lot more to talk about let's talk about service API service Lord API what is this about well okay so this all work really well but we want to really work with interfaces that are implemented by multiple different implementations if you want to do that well how are we going to support this kind of operation so to understand this let's go to a slightly different example here the example we want to go to I'm gonna make sure this is compiling now okay the example I want to go into is going to be this in this example I got a series of files I want to take a look at and understand the first thing I want to show you here is something called rates let's take a look at rates rates is going to contain a module info which is empty rates also contains over here it also contains a class called vendor an interface called bender J as a java vendor dot java is a get name and a get rate that's what I have here okay that's an interface this interface is in a separate module what is the name of the module I'm gonna call it as calm a developer rates so I'm gonna say Marge you come agile dole operates this exports come a developer rates right so I just kept the name same not a big deal so rates module is gonna export rates which means vendor is exported awesome so far so we'll keep it that way the next thing I want to do here is I have rates find the rate finder so let's go ahead and look at the rate finder and the rate finder also has a module info we'll come back to that in a minute but the rate finder also has the class called rate finder dot Java the rate finder dot Java says I'm a rate finder so I'm gonna take grab that put it as a module name rate finder and what am I going to export exports rate finder dot that one now within the way it finder I have a main function it says query vendors and I don't know about specific vendors I don't want to depend on the specific vendors it's a plug-in architecture tomorrow you can throw in a new vendor or you can get rid of the old vendor right I don't want to change any module and couple to it so I'm gonna find out how to get those vendors in just a few minutes we'll just start with this baby steps so but one of the other things I need to do here is I'm gonna say requires and calm agile developer dart rates the module that I'm going to bring in that way I can talk to vendors notice I need access to vendors after all here is that this guy so I'm gonna bring in the require I don't have the specific modules yet so we'll worry about it little later on let's go ahead and do the build on this and see if it takes us anywhere anywhere at all when I do the build built on it line number two is having a trouble on module in foreign rates let's go ahead and fix it so this is module info in rates well let's go ahead and fix it - info and this is under rates there we go so line number two is XO exports there we go so let's go ahead and try this again so this is going to tell us well I have a thing called vendor one and vendor two and I've not really required those it's toking up that's ok well sit in just a minute by just removing the build and we'll come back and add the build a little later on so what I'm going to do is let's just go ahead and comment out all of these will slowly build this one step at a time so what's a very first step the first step is I want to compile my code for the rates the second step I want to do is I want to compile all the way down here I want to compile my rate finder so let's go ahead and compile that also so hopefully those two parts will work properly and produced no no error there you go so but I don't have any vendors yet hmm how am I gonna bring a vendor well the way I'm gonna bring a vendor is let's go to vendor one and in the vendor one here is my vendor one dot java vendor one says I'm implementing vendor I'm having a default constructor and my default constructor simply says vendor one created so your constructor with no arguments then get name and get rate from the vendor you know obviously to make this work I need to require vendor module with the vendor so for that I'm gonna say this is my first vendor so I'm gonna go to module info module vendor one that's the name I give for it and I'm going to say requires and requires what requires comm agile dart rates so that way I can get the vendor let's see if that works so I'm gonna go to the build file here is my vendor one I want a compiled vendor one quietly so when a compiled vendor one now that I have included the build for it it should be fairly happy I would think let's make sure it is actually true and it requires it actually helps me to type this properly let's do that so here's my module info for the vendor one and requires there we go so try that again so we compiled vendor one nothing really special in vendor one yet we'll change that in a minute similarly we go to vendor to vendor do is a little different just to show you an illustration of a difference this is going to be module and this is going to be calm agile vendor two and this requires also the race however let's look at vendor to vendor to does not have a default constructor vendor to says I got a boolean preferred but notice I have a static method called provider so what java is gonna look for is either a constructor which takes no arguments if a constructor doesn't exist like that it looks for a method called a provider that's it you don't have to implement any interfaces for that it just looks for the word provider it's a static method everything else is the same so let's make sure this compiles too so I go back now to my a build file and I'm going to compile this also but when I run this code it is going to be happy with all these dependencies so compilation should pass but how do I actually use this well to understand how to use this we need a few more details the very first thing we need is a set of you know contracts we're gonna use inversion of control your code says I need a vendor somebody save able to give it to you service loader has been around for a while in Java that's been enhanced in Java 9 and so the service loader a.p I say is you first of all use this X what does that mean uses X well uses X simply says if you go to the rates finder in the rates finder you have a module info in here you say uses come agile developer rates dart vendor you tell me what interface you are using so what does Java do Java will come around and say hey I saw your dependency description and you said use this vendor that means you are a consumer of vendor so when I find vendors I'll give it to you right so that's why you said uses great now the second thing I do is I go to the Edit vendor 1 module info and here I'm gonna say provides calm agile developer a vendor one what is the name I gave for the vendor one right here vendor 1 dot vendor 1 width provides that with come agile developer rates dart vendor so I'm sorry the other way around so this is providing this interface vendor with the class with what class is it providing with the vendor one so it provides that with that class so this again tells Java hey I have an implementation in my module you should know and I'm implementing the vendor interface so the wiring can happen now so this is on this on the site similarly I'm going to go ahead and add it in here vendor 2 dot module info and I'm gonna say right here provides but this is going to become vendor 2 right this is going to become where's that file so this becomes vendor 2 so let's okay so this becomes provides this is vendor with vendor 2 and this is vendor 2 almost there almost there let's make sure all this is working actually together but we need to still know how to bring it in but we'll do that in just a few minutes so compile it it's still happy now let's do some fun things this is purely configuration so what are the things we did we said uses we said provide user interface provide interface with that class implementation why now I want to go to the service load in the service loader I am going to now say let's look at this guy this is the beauty let's use the service loader to query all vendors but before this I say service loader vendor vendors 1 is equal to service loaded art load vendor dark class here comes the charm this does not create any objects it is lazy evaluation so at this point notice I just said load it did not expend any effort to create these objects so when you look at the output it still say is querying for all vendors then I say far vendor vendor coming from vendors 1 and what am I going to do here output and output what are we gonna output at this point vendor dart get name and similarly I can call vendor not get price or get rate not a big deal so when I run this now what does it do now notice in this example it printed this and only then it created vendor 2 printed vendor 2 only then it created vendor 1 printed vendor 1 that's how lazy it is right so we can get these things you say no no no no no I don't want all the vendors there are several vendors but I want to cherry-pick the one I want right now vendor with a certain flavor how do I do that well you can do that very easily also to do that let's get rid of this we can say vendors 1 again but this time what I'm going to do is I'm going to call upon vendors wondered stream this gives me a provider so I can say dart filter given a provider I can say provider dot type this gives you a class the Big C class once you get a class you can query whatever you want to query on the class you can query for name query for patterns query for annotations you can do any kind of query on it so if you know your own annotations you have created you can see if that vendor are that particular implementer implements that particular annotation I'm not going to work with annotation here so instead I'm gonna say contains and I'm gonna look for the word too so in this case I'm gonna say if you have a do then I want to talk to you not otherwise so then what am I going to dot map I take the provider given to me and return provider get this is going to create an object then I say for each this is gonna be my vendor and I'm gonna output output what am I gonna output a vendor dot name so vendor a vendor dot name get name right so you can query for things and you can use the provider and get things also so when I go back and run this now let's see what it's going to tell us in this case oh that's not good so um it gave us a error on type a get name isn't it so type dot get name so because it's a class and then do the other work so let's see if that that works so what am I going to do in this case I get in this case if it has two when they're two created not vendor one because vendor 2 is the one with the word two not mentor one so this gives you an idea about how you can use a service loader API the real load is for you to recreate all your objects so you can say give me a new fresh objects also and you can load it so it's a very slow or not slow but a lazy evaluation which is good I will have three more topics I want to talk about in this area well the next is something really charming which is a linking custom runtime images I want you to remember to remind you one thing one more time when you go back and say Java - - list modules remember that we had ninety four modules keep that in mind well good news they are modularized bad news they are still there and if you if you whether you want korba or not it is just sitting there right and sometimes it annoys you it's the go away I'm never gonna use you right so what can we do well here's an idea let's go back to one more example in in this example this is purely about linking nothing to do with compiling so I've compiled the code use the modules already and they're sitting happily in a directory what can I do well this one here is purely about building I blow away the agile developer directory in this directory I use a program called the jail link jail link where is all the modules for Java where is all my modules and then add modules agile developer and I add modules agile developer dot first and output to agile developer and give me a launch name called agile developer what does this do what does this j-link do let's only run up to this part right here not anymore so when I go back and run this part alone notice what just happened well before I go any further this directly had only the built now I go run that now this directory contains the build and the agile developer going to agile developer there is a bin directory go into bin and beautifully you see Java this is not any Java it is my Java it is my precious right and you can ask this Java Java what do you have list modules word count - l 94 no three I contain only stuff I need right so this creates a very targeted custom image in fact if you ever said I really miss running programs with clearly defined my own names that's your named program welcome to C programming right so you can create dedicated executables to moving forward right and that dedicated executable is just a binary created that's your Java you are yes and as a result you can create a very small customized distribution and imagine you don't have to install the whole boatload of stuff on your systems you can spin up your servers with minimal things you care about and nothing else if you want to put this on devices if you want to put this on whatever that you want to put it on you're just loading essentially what you care about and nothing else at all that's your custom image using j-link as you can see right here so jailing can create the target image and then you're able to run it by either by the application name are able to run it as java also then you can look at the list mods pretty nicely and with that said how do we go from where we are to over there well to go there from where we are we want to talk about transitioning I'm going to talk about a couple of different transitioning techniques the first is I have nothing to do with Java 9 I have Java 8 I'm happily running it I don't want to really do anything with Java 9 right now but I just want to deploy it and see how it runs what you could do is one thing you could take your jars and put them into class path like you do run in class path just like the old way of doing things no mention of module path Java run Java 9 put in the class path what happens it runs like the way it runs and when it does those classes belong to what is called unnamed module remember how when you don't put a package it gets a default package it's the same similar thing when you don't give a module name it becomes an unnamed module when you're an unnamed module everything lives in this unnamed module and they are happily sitting there and working and it works like the old stuff not a big deal so you can still do that that's an initial transition period I can continue to run with Java 9 and move forward but I want to recompile my code want to use modules I want to get ready for this stuff how do I do that well for that we are going to talk about how do we transition from the old to the new and to do this let's say I this is van kits module and when kits module depends on let's say over here is Sara's module well there are couple of problems venket wants to change it to java 9 it's like go for java 9 go for jonathan i want to change it sarah says hold your horses I'm not ready yet why because two reasons Sarah says not now no time or Sarah says you know what I love this change I'm gonna make a disruptive change which is not helping me so these are two options right a disruptive change are no change so we'll talk about both of these one is no no change not now another is disruptive change if Sarah doesn't change anything then I still want to use the old jar right now in my Java 9 if Sarah discipline jizz things I'm like I'm sorry I don't have time to make all the code changes to use your new version Sarah thank you I'm gonna move to Java 9 but I would rather use your old jar sorry I know you're supporting Java 9 but I'm not ready to use your version I don't have time to change to all your disruptions so in both cases I want to use Java 8 of a 7 module we call it as old old old jar right jar not the module this is module how do you deal with this good news you put rank it's module and the Sarah's sorry not your module right jar Sarah's jar both of those into the module path just take the old jar and dump it into module path venket module comes to life and says hello I'm a module here's my module info and Java say is awesome come on over what do you want in my module info what am I gonna do requires Sara right jar I'm gonna specify that and Java says Sara jar let me check it goes to the module path and says oh there it is but this is old stuff it doesn't have a module info no worries I'll use your jar name as your module name that is called automatic module ik automatic module link is your jar name becomes your module name almost if my module is called food dart jar sorry if majority is called food jar what's my module name foo if my module name a jar name is food - all dart jar then what is my name Fuu dot all the - becomes a dot automatically if my module is our name is foo - all dot one dot two point three point seven dot jar what does that become food at all period that numbers are ignored so this is the automatic module or you say hey Sara I know I know you have a new module I'm not really use it but your new module has a completely different name you gave a new name in your Java 9 module you know what and that is not Sara jar if I use Sara jar when I decide to use your job and I'm Mario I gotta go change my code so could you do me a favor you open up here all the jar go to manifest put an entry for automatic naming and use the name you will use in the future and that way I can be safe that when I swap your jar with the module my code will still work so one recommendation is if you're not ready to build for Java 9 yet you might want to at least change your manifest file and give an automatic name if your jar does not have an automatic name in the manifest then Java will give one for you if you give a name in the manifest Java will use that name it's better that you give a name then Java give a name for your my if you for your jar and that's how that's going to work and so finally you can just throw that in there and things will come to life and be able to support you and that is one of the biggest benefits you get out of it so the summer is what we talked about Java 9 I think is incredible in terms of its power by far it is probably the best set of changes in the language the most significant in this talk I divide this into three parts I talked about what I'd like to call as improvements and changes what are the call as additions and as advances and the improvements we saw several things that are making our lives better from Java 7 and Java 8 in additions we saw new things we can do including ripple and list off and a few other things including the callable futures completable futures things in advances we pretty much focused on the modularization itself so it's definitely a lot of fun it's going to be a bigger learning curve and effort as we transition but overall it's also a very powerful change as well that will come back to benefit also and so overall I'm very positive for what's really happening in this area I hope you found that useful and if you want to download the code examples by tomorrow morning I'll post that on my downloads link you can download and play with the examples thanks for coming [Music]
Info
Channel: Devoxx
Views: 56,372
Rating: 4.9476814 out of 5
Keywords: DV17, Devoxx
Id: Yacu1yUktjY
Channel Id: undefined
Length: 154min 10sec (9250 seconds)
Published: Wed Nov 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.