ian lake: i don'tknow about you, but i like writing less code,particularly boilerplate code. alongside api 24,we've included support for a number of java8 language features with the help ofthe jack toolchain.
Android java, to use any ofthese new features, you'll need to enablethe jack toolchain. all it takes is justa few lines of code in your build.gradlefile, and you're all set.
now to cut thatboilerplate code. firstly, you'll be ableto use lambda expressions, a simplified way of expressingfunctional interfaces, interfaces with justa single method. this allows us to rewrite ouronclick listeners from all this down to something alittle easier to look at. you'll note that parametertypes are optional. here we're usingbraces to allow you to have multiple statements,just like a normal method.
but if you only havea single expression, you can leave them out. a sister feature tolambda expressions, method references allowyou to use existing methods as lambda expressions,making those one line lambda expressions even shorter. the best part aboutthese features, they're backward compatibledown to gingerbread. so whether you're building forthe latest version of android
or not, you'll be able to shaveoff some boilerplate code. this works becausethe jack toolchain is able to implementthese new features using anonymous classes,just as if you had written the old versionof the code yourself. that also means you shouldn'tgo overboard with using them in tight loops or anything. they're stillrelatively expensive. however, if you areminsdkversion 24,
you've entered amagical land where you can take advantage of anumber of additional features. for example, lambdaexpressions go perfectly with the stream apis whichallow you to chain together multiple operations, suchas filtering, mapping, or converting intodifferent types before applying aterminating method, such as sum for a streamof ints or for each for applying a lambda expressionto every remaining item.
oh, and it supports running inparallel out of the box-- crazy powerful stuff. and i'd encourageyou to check out the docs for all the details,as there's a lot of them. interfaces also got anupgrade with the ability to add default methodimplementations and static methods. this makes it easy to addnew methods to an interface without having to update everyimplementation of the interface
and allows you to playstatic helper methods related to an interface inthe interface itself. and if you're afan of annotations, and i know someof you are, you'll be able to take advantageof repeating annotations to add the sameannotation more than once. particularly useful if youhave an annotation that accepts
different type elements. so lots of greatnew features, both
in the backward compatible andnon-backward compatible nature. new tools to help youbuild better apps.