Meta tags:
description= Detailed Java 12 guide: migration, versions; switch expressions, teeing collectors, indenting/transforming strings (and more); default CDS, Shenandoah, G1.;
keywords= Java 12;
Headings (most frequently used words):
to, and, java, 12, features, api, definitive, guide, migrating, language, updates, jvm, improvements, removed, deprecated, summary, want, migrate, version, requirements, preview, switch, expressions, teeing, collectors, more, versatile, error, recovery, with, completablefuture, indenting, transforming, strings, compact, number, format, mismatching, files, default, cds, archives, garbage, collection, constants, security, enhancements, shenandoah, g1,
Text of the page (most frequently used words):
the (131), java (99), and (69), you (61), that (46), with (44), for (37), new (24), this (22), from (21), stream (21), project (21), string (17), #switch (17), can (16), want (16), jvm (15), jdk (15), two (15), preview (15), code (15), but (15), your (15), cds (15), collectors (14), are (14), which (14), default (13), class (13), more (13), features (13), range (13), shenandoah (12), only (12), there (12), expressions (12), collector (11), classes (11), get (11), teeing (11), garbage (10), files (10), method (10), indentation (10), all (10), version (10), when (9), out (9), methods (9), transform (9), number (9), here (9), return (9), what (9), has (9), need (9), improvements (8), use (8), security (8), api (8), before (8), just (8), have (7), data (7), completablefuture (7), jep (7), memory (7), them (7), requirements (7), will (7), first (7), archives (7), may (7), once (7), source (7), don (7), about (7), integer (7), next (7), case (7), plugin (7), support (7), release (7), post (7), format (6), call (6), feature (6), migration (6), like (6), uses (6), not (6), archive (6), compiler (6), language (6), followers (6), optional (6), know (6), function (6), body (6), max (6), min (6), enable (6), guide (6), maven (6), update (6), license (5), into (5), constants (5), times (5), sharing (5), error (5), recovery (5), way (5), lts (5), bytecode (5), removed (5), enhancements (5), some (5), application (5), heap (5), pause (5), time (5), could (5), even (5), though (5), long (5), apis (5), they (5), collect (5), map (5), int (5), tools (5), dependencies (5), 2019 (5), junit (5), numberformat (4), line (4), make (4), summary (4), deprecated (4), blog (4), well (4), read (4), yourself (4), because (4), many (4), most (4), system (4), same (4), whether (4), work (4), collection (4), without (4), each (4), result (4), where (4), compact (4), maybeaddfilter (4), public (4), why (4), indenting (4), transforming (4), strings (4), things (4), empty (4), better (4), newmin (4), newmax (4), math (4), publish (4), definitive (4), configuration (4), artifactid (4), going (4), last (4), migrate (4), community (4), something (3), boot (3), byte (3), mismatch (3), getcompactnumberinstance (3), compactnumberformat (3), indent (3), exceptionallycompose (3), their (3), results (3), moving (3), free (3), ships (3), libraries (3), got (3), unused (3), collections (3), during (3), concurrent (3), cycle (3), existing (3), other (3), run (3), cloud (3), returns (3), now (3), does (3), doing (3), running (3), loaded (3), loader (3), path (3), been (3), launching (3), off (3), command (3), internal (3), course (3), mismatchindex (3), match (3), println (3), specified (3), mismatching (3), comes (3), instance (3), would (3), tolist (3), example (3), raw (3), simply (3), html (3), setindentationtodepth (3), header (3), createhtml (3), assume (3), after (3), change (3), space (3), executor (3), exceptionally (3), async (3), few (3), one (3), various (3), already (3), versatile (3), much (3), was (3), _range (3), _range1 (3), _range2 (3), false (3), true (3), details (3), keep (3), move (3), updates (3), set (3), works (3), compilerargs (3), performance (3), its (3), such (3), major (3), save (3), check (3), migrating (3), turn (3), follow (3), share (3), pioneer (3), demos (3), streams (3), core (3), clean (3), contact (2), privacy (2), rss (2), stackoverflow (2), github (2), twitch (2), youtube (2), discord (2), mastodon (2), bluesky (2), fun (2), show (2), thanks (2), improved (2), chain (2), expression (2), likely (2), unlikely (2), level (2), these (2), finalize (2), oracle (2), his (2), apparently (2), manipulation (2), over (2), promptly (2), little (2), activity (2), thus (2), functionality (2), available (2), quickly (2), interesting (2), applications (2), avoid (2), full (2), either (2), also (2), low (2), 200 (2), value (2), short (2), threads (2), working (2), launch (2), needs (2), bootstrap (2), were (2), message (2), openjdk (2), supported (2), benefit (2), doesn (2), helloworld (2), xshare (2), additional (2), options (2), turning (2), single (2), file (2), regular (2), always (2), structure (2), called (2), then (2), future (2), again (2), index (2), else (2), boolean (2), utility (2), 5412 (2), ugh (2), how (2), pipeline (2), given (2), means (2), didn (2), literals (2), question (2), recommend (2), passes (2), character (2), decided (2), spaces (2), specify (2), target (2), wouldn (2), completionstage (2), flatmap (2), throwable (2), add (2), computation (2), task (2), suffix (2), argument (2), actions (2), thenapply (2), bearing (2), akin (2), gets (2), mostly (2), create (2), wrote (2), static (2), compareto (2), maxby (2), minby (2), element (2), thing (2), both (2), combining (2), see (2), put (2), aside (2), statement (2), computed (2), intellij (2), module (2), gradle (2), surefire (2), argline (2), incubator (2), easy (2), mechanisms (2), changes (2), past (2), compiling (2), spring (2), hibernate (2), mockito (2), etc (2), eclipse (2), pick (2), newest (2), ask (2), stephen (2), runtime (2), upgrade (2), point (2), start (2), azul (2), term (2), active (2), platforms (2), watch (2), notified (2), content (2), play (2), around (2), repository (2), contains (2), snippets (2), shown (2), permissive (2), reuse (2), projects (2), showcasing (2), important (2), demo (2), src (2), talks (2), libfx (2), record (2), args (2), var (2), year (2), techniques (2), serialization (2), reflection (2), records (2), valhalla (2), panama (2), loom (2), leyden (2), amber (2), patterns (2), pattern (2), matching (2), ramp (2), meta (2), lambda (2), javafx (2), basics (2), j_ms (2), generics (2), dop (2), documentation (2), deprecation (2), libs (2), architecture (2), tags (2), javascript (2), imprint, delve, myself, shorter, compare, numbers, processing, manage, multi, varied, exceptionallyasync, exceptionallycomposeasync, combine, implies, commercial, sparse, known, technical, challenges, except, increased, executive, considerations, marked, removal, weakcompareandsetobjectrelease, weakcompareandsetobjectplain, weakcompareandsetobjectacquire, weakcompareandsetobject, compareandexchangeobjectrelease, compareandexchangeobjectacquire, compareandexchangeobject, compareandsetobject, getandsetobjectrelease, getandsetobjectacquire, getandsetobject, putobjectrelease, putobjectopaque, putobjectvolatile, putobject, getobjectopaque, getobjectacquire, getobjectvolatile, getobject, notable, deprecations, found, unsafe, sun, misc, overrides, privilegedactionexception, undeclaredthrowableexception, exceptionininitializererror, classnotfoundexception, getcause, zipfile, deflater, inflater, fileoutputstream, fileinputstream, sean, mullan, discusses, 334, improvement, helps, parsers, compilers, offline, transformers, head, jlink, 346, committed, 344, abortable, mixed, phases, shows, trigger, continue, using, processes, particularly, enables, elastic, resource, becoming, cheaper, since, tries, hard, completely, gcs, triggers, based, occupancy, allocation, cases, unless, forced, externally, sees, continuous, notably, operating, talk, javazone, aleksey, shipilev, 189, experimental, independent, size, meaning, consistent, appropriate, algorithm, responsiveness, predictable, pauses, reduces, evacuation, concurrently, red, hat, dubbed, 341, improve, essentially, cache, invalidate, different, than, taken, tampered, archived, originally, guaranteed, correct, sticks, tells, bit, server, warning, classpath, appended, nothing, notice, related, program, ide, 120ms, 170ms, ton, easily, observe, effect, laptop, simple, hello, world, takes, 100ms, while, unlocked, appcds, used, process, repeated, every, relaunched, unchanged, leads, removes, redundancy, storing, mapping, launches, kinds, pulls, verifies, loads, bytes, looks, jar, load, besides, seen, path1, path2, difference, compares, differ, formatting, prints, setmaximumfractiondigits, style, locale, easier, constructor, getting, upvotes, subscribers, becomes, social, media, compliant, manner, pretty, awesome, condition, filter, tidbit, places, apply, modify, fluent, inspired, 8140283, brief, complicated, history, noticed, still, good, scour, mailing, lists, did, highly, article, topic, dustin, marx, neat, expects, compared, clumsy, thankfully, our, tab, discovering, lines, abusing, crazy, prefer, instead, stable, under, refactoring, eight, rid, levels, imply, four, imagine, had, handy, fix, alas, 8210971, gives, recover, break, overloads, arguments, pass, produces, supertype, ticks, boxes, matrix, previously, relate, ons, pre, recovers, failed, exception, normal, five, unspecified, thread, explicitly, specific, complete, thencombine, applytoeither, immense, larger, reason, big, place, combinatorial, explosion, orthogonal, thorough, introduction, 8209685, merges, right, non, illegalstateexception, orelsethrow, ofoptional, factory, creates, produce, passing, forwards, exhausted, combines, third, solve, problem, follows, unix, tee, annoying, ranges, needed, end, parallel, min_value, max_value, order, initial, parameters, reduce, sometimes, pieces, information, wasn, exactly, comfortable, determine, smallest, greatest, remember, aware, releases, until, stabilized, bet, too, never, experiments, commands, video, 325, checks, exhaustiveness, branch, fall, through, multiple, labels, greatly, anticipated, bool, ternarybool, filenotfoundexception, ridiculous, uncheckedioexception, throw, file_not_found, https, thedailywtf, com, articles, what_is_truth_0x3f_, numerous, down, longer, being, directs, goes, itself, conditional, ternary, operator, main, assign, variable, written, updated, bumped, exchange, setting, cut, compilejava, test, jvmargs, finalized, stick, latter, activate, flag, below, reactive, http, client, prevent, accidental, requiring, flags, compilation, execution, allow, experimentation, possibly, unstable, similar, modules, recent, gained, expose, stone, mind, rely, explicit, seems, compatible, generally, speaking, fixed, failsafe, bug, straight, built, officially, 2018, idea, ready, minimum, common, ides, build, although, advise, safe, discourage, rewarding, respects, higher, productivity, steep, cliff, least, constantly, explore, saying, consider, carefully, take, close, holistic, look, basis, own, benefits, worth, suffices, address, concerns, lot, pop, actually, against, hear, grueling, explorations, wrong, compile, chances, freely, considering, three, years, careful, anywhere, between, breeze, breaker, occasionally, sheds, implementations, aggressively, reworks, implementation, starting, upgrading, changed, lacking, providers, gaming, terms, gotta, flawless, die, retroactively, buy, points, remaining, option, fixes, currently, downgrade, stuck, unsupported, months, paid, come, far, company, namely, offering, any, pains, say, risk, widely, colebourne, gist, entire, migrated, base, should, help, definitely, hit, milestone, further, remains, answer, bump, released, days, everything, usage, due, present, discuss, importantly, toc, table, contents, lic, artist, dev, detailed, slides, upcoming, schedule, recordings, virtual, vector, structured, concurrency, lilliput, galahad, babylon, conversation, book, club, videos, jms, newsletter, posts, testing, rant, jigsaw, jdeps, impulse, lang, review, comments, words, site, enabled, nipafx,
Text of the page (random words):
va 9 16 features it contains many of the snippets shown in this blog post it has a permissive license so you can reuse the code for your projects table of contents migrating to java 12 want to migrate version requirements preview features language features and api updates switch expressions teeing collectors more versatile error recovery with completablefuture indenting and transforming strings compact number format mismatching files jvm improvements default cds archives garbage collection shenandoah g1 constants api security enhancements removed and deprecated summary share follow share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content src want to play around with the code yourself check out the repository java x demo a project showcasing all important java 9 16 features it contains many of the snippets shown in this blog post it has a permissive license so you can reuse the code for your projects toc migrating to java 12 want to migrate version requirements preview features language features and api updates switch expressions teeing collectors more versatile error recovery with completablefuture indenting and transforming strings compact number format mismatching files jvm improvements default cds archives garbage collection shenandoah g1 constants api security enhancements removed and deprecated summary s f share this post with your community i m active on various platforms watch this space or follow me there to get notified when i publish new content java 12 will be released in a few days and here s everything you need to know about it be it switch expressions the teeing collector improved start time thanks to the default cds archive or better memory usage due to garbage collection improvements i ll present each feature in turn before we get to that we need to discuss migration though most importantly whether you even want to do that why wouldn t you you ask well read on migrating to java 12 i assume that you already migrated your code base to java 11 before going to java 12 if not you should do that first check out this guide for help you definitely want to hit that milestone before going further if you re on 11 all that remains is to answer the question whether you want to bump to 12 migrate to java 11 first want to migrate because here s the thing and it pains me to say that but you put your project at some risk when you move from the widely long term supported java 11 to java 12 stephen colebourne wrote an entire post on that but the gist is you re unlikely to get free long term support lts for java 12 to 16 and even paid lts is not easy to come by as far as i know azul is the only company offering support for any such version namely 13 and 15 without lts a release gets its last security update about 5 months after its release if you can t upgrade to the next major release you re stuck with an unsupported runtime then the only remaining option to get security fixes is to downgrade to the newest major release you can get support for currently that would be java 11 in gaming terms java 11 is the last save point before you make it to 17 and it s gotta be a flawless run because if you die on the way you ll have to start over or retroactively buy save points for 13 and 15 from azul java 11 is the last save point before you make it to 17 what may keep you from upgrading to the next major release though mostly changed removed apis and lacking support by cloud providers starting with java 9 the jdk occasionally sheds deprecated classes and methods e g some finalize implementations in java 12 and more aggressively reworks implementation details if your code uses such apis you need to change it if your dependencies use such apis you need to update them either way that can be anywhere between a breeze and a project breaker if you re running your project in the cloud or even if you re just considering doing that some time in the next two to three years be careful with an upgrade chances are you can t freely pick the java runtime and that s just for running on a new release which suffices to address security concerns a lot more things pop up of you actually want to compile against the new version i m not going to go into that here though read stephen s post if you want to hear more grueling explorations of what may go wrong i don t want to discourage you though moving with each java release is rewarding in many respects you get to benefit from higher productivity better performance and avoid the steep cliff of going from 11 to 17 last but not least working with java will simply be more fun if you can constantly explore a few new things here and there all i m saying is consider the update carefully take a close holistic look at your project and ask yourself whether you can update all the things on a regular basis which has its own benefits of course if you can go for it it will be worth it version requirements so you re ready to move to java 12 here are the minimum version requirements for the most common ides and build tools although i advise to always pick the newest available version just to be safe intellij idea officially 2018 3 but i recommend 2019 1 eclipse 2019 03 that s 4 11 with java 12 support for eclipse 2019 03 or go straight to 2019 06 4 12 which has built in java 12 support maven generally speaking 3 5 0 but e g this bug was only fixed in 3 6 1 compiler plugin 3 8 0 surefire and failsafe 2 22 0 gradle 5 0 is compatible with java 11 and seems to work with java 12 but i know of no explicit statement of support when compiling to java 12 update dependencies like spring hibernate mockito etc when it comes to compiling to java 12 bytecode keep in mind that you will likely have to update all dependencies that rely on bytecode manipulation e g spring hibernate mockito etc preview features in the recent past the jdk gained two mechanisms to expose new functionality before it is set in stone incubator modules for apis preview features for language and jvm changes both mechanisms work in a similar way they allow easy experimentation with new and possibly unstable features they prevent accidental dependencies on them by requiring command line flags during compilation and execution the only incubator module was the reactive http 2 client in java 9 and 10 got finalized in 11 and the only preview feature are switch expressions in java 12 see below which means we can stick to the latter to activate preview features you need to use the flag enable preview this is how to do it in maven plugin artifactid maven compiler plugin artifactid configuration compilerargs enable preview compilerargs configuration plugin plugin artifactid maven surefire plugin artifactid configuration argline enable preview argline configuration plugin and in gradle compilejava options compilerargs enable preview test jvmargs enable preview in intellij set the language level to 12 preview switch expressions for me this only works if i set for the module i e setting it just for the project doesn t cut it language features and api updates you ve decided to move to java 12 updated your tools and dependencies and bumped the compiler s source and target version here s what you get in exchange switch expressions so much has already been written about switch expressions that i don t want to keep you for long the numerous details aside it comes down to switch no longer just being a statement which directs where computation goes like if but an expression which is itself computed to a result like the conditional ternary operator the main use case will be to assign the computed value to a variable https thedailywtf com articles what_is_truth_0x3f_ boolean bool switch ternarybool case true true case false false case file_not_found throw new uncheckedioexception this is ridiculous new filenotfoundexception some of the details i just put aside are greatly anticipated improvements multiple case labels e g case true false no fall through from one case to the next compiler checks exhaustiveness that s why there s no default branch in the example definitive guide to switch expressions in java 12 first contact with switch expressions in java 12 video jep 325 switch expressions remember that this is a preview feature so be aware that it may change in future releases until it s stabilized don t bet too much of your internal code on it and never publish code that uses it to use it in experiments add enable preview to compiler and jvm commands don t publish code that uses switch expressions teeing collectors sometimes you need to collect two pieces of information from a stream pipeline but doing that before java 12 wasn t exactly comfortable see this example where i want to determine a stream s smallest and greatest element so i can use them to create a range range integer range stream of 1 8 2 5 reduce the initial range parameters are min and max in that order so this range is empty range of integer max_value integer min_value combining an existing range with the next number from the stream _range number int newmin math min number _range min int newmax math max number _range max return range of newmin newmax combining two ranges needed at the end of a parallel stream _range1 _range2 int newmin math min _range1 min _range2 min int newmax math max _range1 max _range2 max return range of newmin newmax the annoying thing is that there are collectors minby and maxby which could do most of the work for me if only i could use both of them from java 12 on we can do just that by passing them to the teeing collector static method collectors teeing just like unix tee command it forwards each element the stream passes to it to the two specified collectors once the stream is exhausted it combines the two results into a single instance with the third argument you specify a function with the teeing collector i can solve the problem as follows range string range stream of 1 8 2 5 collect collectors teeing the collectors produce optional integer collectors minby integer compareto collectors maxby integer compareto i wrote a static factory method that creates a range from two optional integer range ofoptional orelsethrow new illegalstateexception non empty stream was empty much better right teeing collector in java 12 jdk 8209685 create collector which merges results of two other collectors more versatile error recovery with completablefuture the api of completablefuture is already immense here s a thorough introduction and in java 12 it gets a little larger the reason why it s so big in the first place is the combinatorial explosion of mostly orthogonal requirements various actions e g thenapply for result bearing methods akin to stream map or completablefuture bearing methods akin to stream flatmap after one one of two or two of two actions complete e g thenapply applytoeither and thencombine in an unspecified thread explicitly as a new task with async suffix or as a new task with a specific executor with async suffix and executor argument java 12 ticks a few boxes on the feature matrix that were previously empty they relate to error recovery and are add ons to the pre existing method exceptionally function throwable t which recovers from a failed computation by turning the exception into a normal result there are five new methods exceptionallycompose function throwable completionstage t is to exceptionally like stream flatmap is to stream map you can pass a function that produces a completionstage supertype of completablefuture new async overloads for exceptionally and exceptionallycompose once with the same arguments once with an additional executor this gives us more tools to recover from all the things that can break out there jdk 8210971 indenting and transforming strings imagine we had raw string literals in java 12 alas we don t wouldn t it be handy to quickly fix their indentation public string createhtml assume four space indentation return html body h1 header h1 body two levels of indentation imply eight spaces to get rid of indent 8 what if after discovering the new line character for new lines you decided to go all in on not abusing spaces and use an indentation character for indentation crazy i know or what if you prefer to specify the target indentation instead of the change of indentation which would be stable under refactoring given a method that does what you want you could of course simply call it with the string public string createhtml assume tab indentation we want no indentation so we call our method string setindentationtodepth string int with the raw string and 0 return html setindentationtodepth body h1 header h1 body 0 ugh i know compared to indent that s clumsy thankfully there s a way out public string createhtml same as before return html body h1 header h1 body but here we call transform which expects a function string t transform s setindentationtodepth s 0 the method transform simply passes the instance that it s called on to the specified function neat now as you may have noticed we didn t get raw string literals in java 12 so why are these methods still in there good question for once i didn t scour the mailing lists but dustin marx did and i highly recommend you read his article on the topic if you want to know more about string transform the brief but complicated history of jdk 12 s string transform method an interesting tidbit is that transform may show up in other places for example on stream or on optional which means you could apply your methods that modify a stream or optional in a fluent call chain inspired by jdk 8140283 given this method public stream t maybeaddfilter stream t s if condition return s filter else return s and this pipeline source stream map collect tolist this is how to call maybeaddfilter maybeaddfilter source stream map collect tolist once again ugh what about transform source stream map transform this maybeaddfilter collect tolist that would be pretty awesome compact number format need to format upvotes subscribers or followers in a social media compliant manner where 5412 becomes 5 4 k the new compactnumberformat is there for you compactnumberformat has a constructor but getting an instance from numberformat getcompactnumberinstance is easier numberformat followers numberformat getcompactnumberinstance new locale en us style short followers setmaximumfractiondigits 1 prints 5 4k followers system out println followers format 5412 followers compact number formatting comes to jdk 12 mismatching files the utility class files got a new utility the method mismatch path path compares the two specified files and returns the index of the first byte where they differ or 1 if they don t long mismatchindex files mismatch path1 path2 boolean match mismatchindex 1 if match system out println files match else system out println files first difference is at index mismatchindex jvm improvements besides the language and apis the jvm has of course also...
|