78

Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Embed Size (px)

Citation preview

Page 1: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •
Page 2: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

WhatiscoolinJava8andnewin9

AurelioGarcia-RibeyroDirectorofProductManagementJavaPlatformGroupMarch2017

Page 3: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

SafeHarborStatementThefollowingisintendedtooutlineourgeneralproductdirection.Itisintendedforinformationpurposesonly,andmaynotbeincorporatedintoanycontract.Itisnotacommitmenttodeliveranymaterial,code,orfunctionality,andshouldnotberelieduponinmakingpurchasingdecisions.Thedevelopment,release,andtimingofanyfeaturesorfunctionalitydescribedforOracle’sproductsremainsatthesolediscretionofOracle.

3

Page 4: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Introduction

– DirectorProductManagement,JavaPlatformGroup,Oracle– InchargeofmanagingtheproductrequirementsforOracle’sJDKsincejoiningOraclethroughtheSunacquisitionin2010– BeforejoiningOracle,workedatSunMicrosystemsfortheJavaProductManagementteam–MBAfromMITSloanandBachelordegreeinSystemsEngineeringfromUniversidaddeLima

AurelioGarcia-Ribeyro

Page 5: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Java8 “Oneofthebiggestupdatesevertoamajorlanguage”AndrewBinstock

FormerEditorinChief,Dr.Dobbs ,nowwithJavaMagazine

Page 6: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Abridged Content List for JDK 8

§ LambdaExpressions§ DefaultMethods§ MethodReferences§ DateTimeAPIs- JSR310

Page 7: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AbstractingoverBehaviorCollection<Person> people = ...;

for (Person p : people){if (p.getAge() > 18)

?? How do we remove this person ???}

Page 8: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AbstractingoverBehaviorCollection<Person> people = ...;

Iterator<Person> it = people.iterator();while (it.hasNext()) {

Person p = it.next();if (p.getAge() > 18)

it.remove();}

Page 9: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AbstractingoverBehaviorinterface Predicate<T> {

boolean test(T t);}

class Collections {public static<T>

void removeIf(Collection<T> coll, Predicate<T> pred) {

...}

}

TheAPIDesignerCouldcreatemethodsinthecollection

Page 10: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Collection<Person> people = ...;

Collections.removeIf(people, new Predicate<Person>() {

public boolean test(Person p) {return p.getAge() > 18;

}}

});

AbstractingoverBehavior Butthecodetousethenewmethodwouldbebloated

Page 11: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Collection<Person> people = ...;

Collections.removeIf(people, new Predicate<Person>() {

public boolean test(Person p) {return p.getAge() > 18;

}}

});

AbstractingoverBehavior

Page 12: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AbstractingoverBehaviorCollection<Person> people = ...;

Collections.removeIf(people, p -> p.getAge() > 18);

Page 13: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AggregateoperationsCollection<Person> people = ...;

int highestWeight = 0;for (Person p : people) {

if (p.getGender() == MALE) {int weight = p.getWeight();highestWeight = max(highestWeight,weight);

}}

Page 14: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ParallelismCollection<Person> people = ...;

int highestWeight = people.stream()

.filter(p -> p.getGender() == MALE)

.mapToInt(p -> p.getWeight())

.max();

Page 15: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Parallelism

class MaxProblem {

final List<Person> people;final int size;

MaxProblem(List<Person> ps) {this.people = ps;size = ps.size();

}

public int solveSequentially() {int max = 0;for (Person p : people) {

if (p.getGender() == MALE) max = Math.max(max, p.getWeight());

}return max;

}

public MaxProblem subproblem(int start, int end) {return new MaxProblem(people.subList(start, end));

}

}

class MaxFinder extends RecursiveAction {

private final MaxProblem problem;int max;

protected void compute() {if (problem.size < THRESHOLD)

sum = problem.solveSequentially();else {

int m = problem.size / 2;MaxFinder left, right;left = new MaxFinder(problem.subproblem(0, m))right = new MaxFinder(problem.subproblem(m, problem.size));forkJoin(left, right);max = Math.max(left.max, right.max);

}}

}

ForkJoinExecutor pool = new ForkJoinPool(nThreads);MaxFinder finder = new MaxFinder(problem);pool.invoke(finder);

Page 16: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ParallelismCollection<Person> people = ...;

int highestWeight = people.stream()

.filter(p -> p.getGender() == MALE)

.mapToInt(p -> p.getWeight())

.max();

Collection<Person> people = ...;

int highestWeight = people.parallelStream()

.filter(p -> p.getGender() == MALE)

.mapToInt(p -> p.getWeight())

.max();

Page 17: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Abridged Content List

§ LambdaExpressions§ DefaultMethods§ MethodReferences§ DateTimeAPIs- JSR310

Page 18: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

DefaultmethodsCollection<Person> people = ...;

int highestWeight = people.stream()

...

interface Collection<T> { ...default Stream<T> stream() {

...}

}

Page 19: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

StaticMethodsInInterfaces

• Previouslyitwasnotpossibletoincludestaticmethodsinaninterface• Staticmethods,bydefinition,arenotabstract– @FunctionalInterface canhavezeroormorestaticmethods

static <T> Predicate<T> isEqual(Object target) {return (null == target)

? Objects::isNull: object -> target.equals(object);

}

Page 20: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

MethodReferences

list.replaceAll(s -> s.toUpperCase());

list.replaceAll(String::toUpperCase);

list.sort(Comparator.comparing(p -> p.getName()));

list.sort(Comparator.comparing(Person::getName));

Page 21: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Summary

• Lambdasarefunctions• JavaSE8definesnewAPIsusingfunctionalinterfaces• Defaultmethods• Methodreferences

Lambdasmakecodereadmoreliketheproblemstatement.Theresultingcodeisclear,concise,andeasytomaintainandmodify.

Page 22: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ToolsforJavaSE8

• Quicklyconvertanonymousinnerclassestolambdas

LambdaExpressions

Page 23: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ToolsforJavaSE8

• Easilyconvertfromlambdastomethodreferences

MethodReferences

Page 24: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ToolsforJavaSE8

• SpecifyascopeforupgradingtoJava8– All/currentprojects– Specificpackage– Specificclass

• Runconverters• Visuallypreviewproposalsforrefactoring

RefactoringinBatchMode

Page 25: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Abridged Content List

§ LambdaExpressions§ DefaultMethods§ MethodReferences§ DateTimeAPIs- JSR310

Page 26: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JDK8JavaTimeFeatures– JSR310

• Replacesjava.util.Date,Calendar,TimeZone,DateFormat• Fluent,Immutable,ThreadSafe,Easytouse• Strongtypingwithfitforpurposetypes• Easytouseformattingandparsing• Interoperablewithjava.util.Calendar/Date• ExtensibleUnits,Fields,andChronologies• SupportsRegionalCalendars• SupportedbyJDBC,java.sql.Date/Time/Timestamp• TheessentialISO8601Calendarforglobalbusiness

NewImprovedDateTimeAPI

Page 27: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Rangeoftypesl Dateconsistsofyear,monthanddayl Time-of-dayconsistsofhour,minute,second

- LocalDate - adatewithnotimeandnotime-zone- LocalTime - atimewithnodateandnotime-zone- LocalDateTime - combinesLocalDateandLocalTime

l Time-zoneshandledseparately- ZonedDateTime - closestclasstojava.util.Calendar

l Instantaneouspointsintime- Instant - closestclasstojava.util.Date

InJDK8,youhavetochoosethecorrectdate/timetype

whendesigninganapplication!

Page 28: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

LocalDatel Storesyear-month-day

- 12th March2017

l Usecases:birthdays,start/enddates,holidaydates

LocalDate current = LocalDate.now();LocalDate date = LocalDate.of(2013,Month.SEPTEMBER,12);if (current.isAfter(date)) …

String str = date.toString(); // 2013-09-12

boolean leap = date.isLeapYear();int monthLen = date.lengthOfMonth();

Page 29: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

LocalTimel Storeshour-minute-second-nanosecond

- 13:30(1:30pm)

l Usecases:shopopeninghours,alarmclock

LocalTime current = LocalTime.now();LocalTime time = LocalTime.of(13,30);if (current.isAfter(time)) …

String str = time.toString(); // 13:30

time = time.plusHours(4).minusMinutes(1).withNano(0);time = time.truncatedTo(ChronoUnit.SECONDS);

Page 30: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

LocalDate-Timel StoresLocalDate andLocalTime

- 12th September2013at13:30

l Usecase:localdate-timeaflighttakesoff

dt1 = LocalDateTime.now();dt2 = LocalDateTime.of(2013,SEPTEMBER,12, 13,30);

dt1 = dt1.plusDays(2).minusHours(1);dt1 = dt1.with(next(TUESDAY));

dt2.toString(); // 2013-09-12T13:30

dt2 = dt2.truncatedTo(MINUTES);

http://www.flickr.com/photos/estevesm/473866656/

Page 31: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Instantl Storesnanosecondsfrom1970-01-01Zl Closestequivalenttojava.util.Datel Usecase:timestampinlogging

instant1 = Instant.now();instant2 = Instant.now();

if (instant1.isAfter(instant2)) { … }

Page 32: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Timezonesl Worldisdividedintovarioustimezonesl Timeinalocationdefinedbypolitical rulesl Ruleschangefrequentlyandincomplexways

Ifyoucanavoidtime-zonesyourapplicationwillbesimpler!

Page 33: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Timezonedesignl Fourclassesmanagetime-zonecomplexity

l ZoneId - "Europe/Paris",asperjava.util.TimeZonel ZoneOffset - "-05:00",offsetfromUTC/Greenwichl ZoneRules - behindthescenesclassdefiningtherulesl ZonedDateTime - maindate/timeclasswithtime-zones

Page 34: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Calendarsystemsl Allmainclassesuse"ISO"calendarsysteml CalendarsystemdefinedinISO-8601

- current'civil'calendarappliedtoalltime- nothistoricallyaccurate

l Othercalendarsystemsalsosupported- notsupportedtothesamedegreeasISO- Hijrah,Japanese,Minguo,ThaiBuddhist intheJDK

l Onlyaffectdates,nottimes

Page 35: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Durationl Time-basedamount

- hours,minutes,secondsandnanoseconds- somesupportfor24-hourdays

l Usecases:sportstopwatch,timeout

duration = Duration.ofHours(6); // PT6Hduration = duration.multipliedBy(3); // PT18Hduration = duration.plusMinutes(30); // PT18H30M

dt = LocalDateTime.now();dt = dt.plus(duration);

Page 36: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Periodl Date-basedamount

- years,monthsanddays

l Usecases:lengthofpregnancy,lengthofholiday

period = Period.ofMonths(9); // P9Mperiod = period.plusDays(6); // P9M6D

dt = LocalDateTime.now();dt = dt.plus(period);

period = Period.between(startDate, endDate);

Page 37: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Summaryl LocalDate 2016-12-03

l LocalTime 11:05:30

l LocalDateTime 2016-12-03T11:05:30

l ZonedDateTime 2016-12-03T11:05:30+01:00 Europe/Paris

l Instant 2576458258.266 seconds after 1970-01-01

l Duration PT30S (30 seconds)

l Period P1Y6M (1 year and 6 months)

Page 38: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JDK8Innovation• LambdaakaClosures• LanguageInterop• Nashorn

CoreLibraries• Paralleloperationsforcore

collectionsAPIs• Improvementsinfunctionality• Improvedtypeinference

GeneralGoodness• JVMenhancements• NoPermGen limitations• Performanceimprovements

JavaforEveryone• Profilesforconstraineddevices• JSR310-Date&TimeAPIs• Non-Gregoriancalendars• Unicode6.2• ResourceBundle• BCP47localematching• Globalization&Accessibility

Tools• JSR308-AnnotationsonJavaType• Nativeappbundling• AppStoreBundlingtools• jdeps

Client• Deploymentenhancements• JavaFX8• PublicUIControlAPI• JavaSEEmbeddedsupport• EnhancedHTML5support• 3Dshapesandattributes• Printing

Security• LimiteddoPrivilege• NSASuiteBalgorithmsupport• SNIServerSidesupport• DSAupdatedtoFIPS186-3• AEADJSSECipherSuites

Enterprise• MissionControl• FlightRecorder• UsageTracker• AdvancedManagementConsole• MSIEnterpriseJREInstaller

38

Page 39: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Wheredidmostofthisinformationcomefrom

• JavaSE8—LanguageandLibraryFeatures,BrianGoetz• IntroductiontoLambdaExpressions,StuartMarks• ANewDateandTimeAPI—JSR-310,StephenColebourne

Java8LaunchEvent

https://www.oracle.com/java8launch

Searchfor:Java 8 Launch

Page 40: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

CominginJDK9

40

Page 41: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Agenda

• Behindthescenesimprovements• Newfeaturesandfunctionality• Adoptingnewstandards• Housekeeping• Gone,gone,gone!

MoreinformationonanyJEP:http://openjdk.java.net/jeps/{JEP#}

Searchfor:OpenJDK 9

Page 42: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

BehindthescenesimprovementsGoodnessyougetforfreejustbyupdatingtoJDK9Noneedforusertochangeanythingtobenefitfromthese

Page 43: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP250:StoreInternedStringsinCDSArchives

• Storeinternedstringsinclass-datasharing(CDS)archives

• ReducememoryconsumptionbysharingtheStringobjectsandunderlyingchararrayobjectsamongstdifferentJVMprocesses• OnlysupportsharedstringsfortheG1GC.Sharedstringsrequireapinnedregion,andG1istheonlyHotSpot GCthatsupportspinning• Onlysupport64-bitplatformswithcompressedobjectandclasspointers

hotspot / runtime

Page 44: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP254:CompactStrings

• Adoptamorespace-efficientinternalrepresentationforstrings

• Lessmemoryusedforstoringstrings• Stringclassstorescharactersinachararray,usingtwobytes(sixteenbits)foreachcharacter• ChangetheinternalrepresentationoftheStringclasstoabytearrayplusanencoding-flagfield

core-libs / java.lang

Page 45: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP225:JavadocSearch

• AddasearchboxtogeneratedAPIdocumentationthatcanbeusedtosearchforprogramelementsandtaggedwordsandphraseswithinthedocumentation

tools / javadoc(tool)

Page 46: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

NewfeaturesandfunctionalityNewtoolsandcapabilitieslikelytobeusefultomostdevelopersWillhavetochoosetousethese

Page 47: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

ProjectJigsaw

• JEP261:ModuleSystem• JEP200:TheModularJDK• JEP201:ModularSourceCode• JEP220:ModularRun-TimeImages• Plus– JEP260:EncapsulateMostInternalAPIs– JEP282:jlink:TheJavaLinker

ModularizetheJavaPlatform

Page 48: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP282:jlink:TheJavaLinker

• Createatoolthatcanassembleandoptimizeasetofmodulesandtheirdependenciesintoacustomrun-timeimageasdefinedinJEP220.Defineapluginmechanismfortransformationandoptimizationduringtheassemblyprocess,andforthegenerationofalternativeimageformats• Createacustomruntimeoptimizedforasingleprogram• JEP261defineslinktime asanoptionalphasebetweenthephasesofcompiletimeandruntime.Linktimerequiresalinkingtoolthatwillassembleandoptimizeasetofmodulesandtheirtransitivedependenciestocreatearun-timeimageorexecutable

tools / jlink

Page 49: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP277:EnhancedDeprecation

• Revampthedeprecationannotation,andprovidetoolstostrengthentheAPIlifecycle• ProvidebetterinformationaboutthestatusandintendeddispositionofAPIsinthespecification• Provideatooltoanalyzeanapplication'sstaticusageofdeprecatedAPIs• Provideatooltodetectanapplication'sdynamicusageofofdeprecatedAPIsinordertoemitwarningsatruntime

core-libs / java.lang

@Deprecated(since=9,condemned=true)

Page 50: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP269:ConvenienceFactoryMethodsforCollections

• DefinelibraryAPIstomakeitconvenienttocreateinstancesofcollectionsandmapswithsmallnumbersofelements,soastoeasethepainofnothavingcollectionliteralsintheJavaprogramminglanguage

• Decreasetheamountofcodeneededforcreatingsmallcollectionsandmaps

core-libs / java.util:collections

Set<String> alphabet = Set.of("a", "b", "c");

Page 51: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP222:jshell:TheJavaShell(Read-Eval-PrintLoop)

• Provideaninteractivetooltoevaluatedeclarations,statements,andexpressionsoftheJavaprogramminglanguage,togetherwithanAPIsothatotherapplicationscanleveragethisfunctionality• ARead-Eval-PrintLoop(REPL)isaninteractiveprogrammingtoolwhichloops,continuallyreadinguserinput,evaluatingtheinput,andprintingthevalueoftheinputoradescriptionofthestatechangetheinputcaused.Scala,Ruby,JavaScript,Haskell,Clojure,andPythonallhaveREPLsandallallowsmallinitialprograms.JShell addsREPLfunctionalitytotheJavaplatform

tools / jshell

Page 52: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP238:Multi-ReleaseJARFiles

• ExtendtheJARfileformattoallowmultiple,Java-release-specificversionsofclassfilestocoexistinasinglearchive

• WriteJDK-version-specificvariantsofthesamecodeintoasinglejarfile

tools / jar

Page 53: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

AdoptingnewstandardsJDK9keepingupwithimprovementsintheindustry

Page 54: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP267:Unicode 8.0

• UpgradeexistingplatformAPIstosupportversion8.0oftheUnicodeStandard

core-libs / java.lang

Ahom

AnatolianHieroglyphs

HatranMultaniOldHungarianSuttonSignWriting

Page 55: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP226:UTF-8PropertyFiles

• DefineameansforapplicationstospecifypropertyfilesencodedinUTF-8,andextendtheResourceBundle APItoloadthem• Theplatformhasaproperties-fileformatbasedonISO-8859-1andanescapemechanismforcharactersthatcannotberepresentedinthatencoding

core-libs / java.util:i18n

Page 56: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP249:OCSPStaplingforTLS

• ImplementOCSPstaplingviatheTLSCertificateStatusRequestextensionandtheMultipleCertificateStatusRequestExtension• CertificatestatuscheckingusingOCSPtypicallyinvolvesanetworkrequestforeachcertificatebeingchecked.Becauseoftheadditionalnetworkrequests,enablingOCSPcheckingforTLSontheclientsidecanhaveasignificantimpactonperformance.• OCSPstaplingallowsthepresenterofacertificate,ratherthantheissuingCertificateAuthority(CA),tobeartheresourcecostofprovidingOCSPresponses

security-libs / javax.net.ssl

Page 57: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP287:SHA-3HashAlgorithms

• ImplementtheSHA-3cryptographichashfunctions(BYTE-only)specifiedinNISTFIPS202

security-libs / java.security

Page 58: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP224:HTML5Javadoc

• Enhancethejavadoc tooltoallowgeneratingHTML5markup.

tools / javadoc(tool)

Page 59: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP110:HTTP/2Client

• DefineanewHTTPclientAPIthatimplementsHTTP/2andWebSocket,andcanreplacethelegacyHttpURLConnection API• ForJDK9thisAPIwillbeontheincubatormodules(JEP11)withthegoalofaddingthemtothestandardonalaterrelease.

core-libs / java.net

Page 60: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

HousekeepingSettingupforfutureimprovementsandreducingcomplexity

Page 61: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP260:EncapsulateMostInternalAPIs

• MakemostoftheJDK'sinternalAPIsinaccessiblebydefaultbutleaveafewcritical,widely-usedinternalAPIsaccessible,untilsupportedreplacementsexistforallormostoftheirfunctionality– InordertokeepcriticalAPIswithoutareplacementaccessiblebydefaultsun.miscandsun.reflect willnotbehiddenandafewAPIskept“public”• sun.misc.Unsafe• sun.misc.{Signal,SignalHandler}• sun.reflect.Reflection::getCallerClass• sun.reflect.ReflectionFactory

– AllotherAPIsinthesepackages(e.g.sun.misc.Base64)willberemoved

Page 62: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP275:ModularJavaApplicationPackaging

• IntegratefeaturesfromProjectJigsawintotheJavaPackager,includingmoduleawarenessandcustomruntimecreation

• Leveragejlink inourpackagertocreatesmallerpackages

• ThepackagerwillonlycreateapplicationsthatusetheJDK9runtime.

deploy / packager

Page 63: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP223:NewVersion-StringScheme

• RevisetheJDK'sversion-stringschemesothatitiseasiertodistinguishmajor,minor,andsecurity-updatereleases

• Alignwithcurrentindustrypractices,inparticularSemanticVersioning• ProvideasimpleAPIforversion-stringparsing,validation,andcomparison

Page 64: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP295:Ahead-of-TimeCompilation

• CompileJavaclassestonativecodepriortolaunchingthevirtualmachine.• Improvethestart-uptimeofbothsmallandlargeJavaapplications,withatmostalimitedimpactonpeakperformance.• AOTcompilationisdonebyanewtool,jaotc• Fortheinitialrelease,theonlysupportedmoduleisjava.base.• AOTcompilationofanyotherJDKmodule,orofusercode,isexperimental

hotspot / compiler

Page 65: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP280:Indify StringConcatenation

• ChangethestaticString-concatenationbytecodesequencegeneratedbyjavac touseinvokedynamic callstoJDKlibraryfunctions.ThiswillenablefutureoptimizationsofStringconcatenationwithoutrequiringfurtherchangestothebytecodeemittedbyjavac

tools / javac

Page 66: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP271:UnifiedGCLogging

• Reimplement GCloggingusingtheunifiedJVMloggingframeworkintroducedinJEP158

hotspot / gc

Page 67: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP248:MakeG1theDefaultGarbageCollector

• MakeG1thedefaultgarbagecollectoron32- and64-bitserverconfigurations

• LimitingGCpausetimesis,ingeneral,moreimportantthanmaximizingthroughput.Switchingtoalow-pausecollectorsuchasG1shouldprovideabetteroverallexperience,formostusers,thanathroughput-orientedcollectorsuchastheParallelGC,whichiscurrentlythedefault

hotspot / gc

Page 68: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP213:MillingProjectCoin

• AddresstheroughedgesofthechangesincludedinProjectCoin/JSR334aspartofJDK7/JavaSE7

1. Allow@SafeVargsonprivateinstancemethods

2. Alloweffectively-finalvariablestobeusedasresourcesinthetry-with-resourcesstatement

3. Allowdiamondwithanonymousclassesiftheargumenttypeoftheinferredtypeisdenotable

4. Completetheremoval,beguninJavaSE8,ofunderscorefromthesetoflegalidentifiernames

5. Supportforprivateinterfacemethods

tools / javac

Page 69: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP290:FilterIncomingSerializationData

• Allowincomingstreamsofobject-serializationdatatobefilteredinordertoimprovebothsecurityandrobustness

• Allowsdeveloperstolockoutsomeserializeddata

core-libs / java.io:serialization

Page 70: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

JEP214:RemoveGCCombinationsDeprecatedinJDK8hotspot / gc

• JEP173deprecatedsomeGCcombinationswithJDK8.• UnsupportedanduntestedsinceJDK8• IncrementalCMS(iCMS)removed

Page 71: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

GeneralRule:LookOutforUnrecognizedVMOptions• LaunchingJREwithunrecognizedVMoptionsfails.• UsingdeprecatedoptionsinJDK8triggerswarningmessages:

$java-XX:MaxPermSize=1G-versionJavaHotSpot(TM)64-BitServerVMwarning:ignoringoptionMaxPermSize;supportwasremovedin8.0

• Previouslydeprecatedoptions,removedinJDK9,willfailtostart.– PermgenerationwasremovedinJDK8(JEP122).– Expectmanyprogramstorunintothisproblem.

Page 72: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

• StoreInternedStringsinCDSArchives• ImproveContendedLocking• CompactStrings• ImproveSecureApplicationPerformance• LeverageCPUInstructionsforGHASHandRSA• TieredAttributionforjavac• JavadocSearch• MarlinGraphicsRenderer• HiDPI GraphicsonWindowsandLinux• EnableGTK3onLinux• UpdateJavaFX/MediatoNewerVersionof

GStreamer

• EnhancedDeprecation• Stack-WalkingAPI• ConvenienceFactoryMethodsforCollections• PlatformLoggingAPIandService• jshell:TheJavaShell(Read-Eval-PrintLoop)• CompileforOlderPlatformVersions• Multi-ReleaseJARFiles• Platform-SpecificDesktopFeatures• TIFFImageI/O\• Multi-ResolutionImages

• ProcessAPIUpdates• VariableHandles• Spin-WaitHints• DynamicLinkingofLanguage-

DefinedObjectModels• EnhancedMethodHandles• MoreConcurrencyUpdates• CompilerControl

• Jigsaw– ModularizeJDK

Behindthescenes

Newfunctionality

Specialized

• ParserAPIforNashorn• PrepareJavaFXUIControls&CSSAPIsfor

Modularization• ModularJavaApplicationPackaging• NewVersion-StringScheme• ReservedStackAreasforCriticalSections• SegmentedCodeCache• Ahead-of-TimeCompilation• Indify StringConcatenation• UnifiedJVMLogging• UnifiedGCLogging• MakeG1theDefaultGarbageCollector• UseCLDRLocaleDatabyDefault• ValidateJVMCommand-LineFlagArguments• Java-LevelJVMCompilerInterface• DisableSHA-1Certificates• SimplifiedDoclet API• DeprecatetheAppletAPI• ProcessImportStatementsCorrectly• AnnotationsPipeline2.0• ElideDeprecationWarningsonImport

Statements• MillingProjectCoin• FilterIncomingSerializationData• RemoveGCCombinationsDeprecatedin

JDK8• RemoveLaunch-TimeJREVersionSelection• RemovetheJVMTIhprof Agent• Removethejhat Tool

Housekeeping

Gone

• HTTP2Client• Unicode8.0• UTF-8PropertyFiles• ImplementSelectedECMAScript6

FeaturesinNashorn• DatagramTransportLayerSecurity

(DTLS)• OCSPStaplingforTLS• TLSApplication-LayerProtocol

NegotiationExtension• SHA-3HashAlgorithms• DRBG-BasedSecureRandom

Implementations• CreatePKCS12Keystores byDefault• MergeSelectedXerces2.11.0Updates

intoJAXP• XMLCatalogs• HarfBuzz Font-LayoutEngine• HTML5Javadoc

Newstandards

Page 73: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

DownloadJDK9EA• EarlyaccessbuildsofJDK9

availablefortesting:– https://jdk9.java.net/download/

• Thereareperiodicupdates,socheckfrequentlyfornewerbuilds.

– See“Summaryofchanges”oneachbuild.

Page 74: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Jointheconversation• ThereareOpenJDKaliasesforall

questions• http://mail.openjdk.java.net/mailman/listinfo

• EveryJEPlistsitsalias• Lookfor“Discussion”

Page 75: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

DownloadNetBeansDev• EarlyaccessofNetBeanswith

supportforJDK9availableat:– http://wiki.netbeans.org/JDK9Support

Page 76: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

IdentifyProblematicDependencies

• AvailablesinceJDK8,BestresultsfromtheversioninJDK9EA• Optiontofindinternaldependencies

UseJavaDependencyAnalysisTool(jdeps)

https://wiki.openjdk.java.net/display/JDK8/Java+Dependency+Analysis+Tool

tzupdater-2.0.3-2015b $ jdeps tzupdater.jartzupdater.jar -> java.base

com.sun.tools.tzupdater -> com.sun.tools.tzupdater.utils tzupdater.jar(...)

com.sun.tools.tzupdater -> java.util.regex java.basecom.sun.tools.tzupdater -> java.util.zip java.basecom.sun.tools.tzupdater -> sun.util.calendar JDK internal API (java.base)com.sun.tools.tzupdater -> tools.javazic tzupdater.jar

(...)com.sun.tools.tzupdater.utils -> java.util java.basecom.sun.tools.tzupdater.utils -> sun.util.calendar JDK internal API (java.base)tools.javazic -> java.io java.base

(...)

Page 77: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •

Copyright©2017, Oracleand/oritsaffiliates.Allrightsreserved.

Stopgap:ExposeInternalAPIs

• SamplecommandforearlierdevversionofNetbeans

Butcomebackandfix!

$bin/netbeans --jdkhome ~/jdk9ea--add-exportsjava.desktop/sun.awt=ALL-UNNAMED--add-exportsjava.base/jdk.internal.jrtfs=ALL-UNNAMED--add-exportsjava.desktop/java.awt.peer=ALL-UNNAMED--add-exportsjava.desktop/com.sun.beans.editors=ALL-UNNAMED--add-exportsjava.desktop/sun.awt.im=ALL-UNNAMED--add-exportsjava.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED--add-exportsjava.management/sun.management=ALL-UNNAMED,

Page 78: Cool in Java 8, and new in Java 9 - RainFocus · PDF file• Store Interned Strings in CDS Archives • Improve Contended Locking • Compact Strings •