java 1.7 features
TRANSCRIPT
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 1/21
Java 7: The Top 8 Features September 2, 2011 VineetManohar
204
132
53
89
Its been a while sine the last ma!or "a#a releaseand e$petations were nat%rall& hi'h (or the%pomin' release…)
It*s been a while sine the last ma!or "a#a release and e$petations were
nat%rall& hi'h (or the %pomin' release) +he "a#a release initiall& inl%ded
man& "S-s with e$itin' (eat%res, like s%pport (or los%res, whih were later
de(erred to "a#a 8 in order to release "S-s that are alread& done) +his
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 2/21
e.eti#el& dil%ted what is now o.ered in "a#a and has le(t some
disappointed)
+he "a#a lan'%a'e has %nder'one ma!or han'es sine I started %sin' it in
1998) Most o( the han'es were dri#en b& the "a#a /omm%nit& roess "/
whih was established in 1998 as a (ormal and transparent proess to let
interested indi#id%als and entities partiipate and in%ene how the
lan'%a'e sho%ld e#ol#e) +his is done thro%'h the s%bmission o( a han'e
re%est, known as "a#a Speiation -e%est "S-, (ollowed b& a re#iew and
a #otin' proess) /han'es or enhanements made to the lan'%a'e an be
%s%all& traked bak to a "S- where the& were ori'inall& p%t (orward (or
re#iew) 6or e$ample, the addition o( 7eneris in "a#a 5 was done #ia "S- 14)
Java Releases
ere*s a %ik snapshot o( the past "a#a release dates table 1) +here are
se#eral small new (eat%res and enhanements in "a#a ) %t o( the 28
(eat%res that I looked at, here are the ones that I (o%nd %se(%l)
New features and enhancements
#1 Strings in switch
In pro'rammin', we o(ten eno%nter sit%ations where we need to do
di.erent thin's based on di.erent #al%es o( a #ariable) 6or :oolean
#ariables, an i(;then;else statement is the per(et wa& o( branhin' ode) 6or
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 3/21
primiti#e #ariable t&pes we %se the swith statement) owe#er, (or Strin'
#ariables, we tend to resort to %sin' m%ltiple i(;then;else branhes as (ollows)
Java 6 and Before
ne workaro%nd (or this is to on#ert the Strin' into an en%m and then
swith on the en%m)
Java 7
"a#a , howe#er, has added a lan'%a'e le#el s%pport (or Strin' in swith)
<ow &o% an rewrite the same ode more ele'antl&=
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 4/21
<ot onl& does this help %s write more readable ode, b%t it also helps the
ompiler 'enerate more e>ient b&te ode as ompared to the i(;then;else
b& at%all& swithin' on the hashode and then doin' an e%als
omparison) lease note that &o% will 'et a <%llointer?$eption i( the
#ariable lan'%a'e in the abo#e e$ample resol#es to n%ll) I like this (eat%re,
b%t %nlike some o( the other enhanements in the past like 7eneri in "a#a
5, I don*t antiipate %sin' this (eat%re a lot) ratiall&, I nd m&sel( %sin' i(;then;else (or one or two #al%es and resort to an ?n%m when the n%mber o(
#al%es are hi'her)
#2 try-with-resources statement
ne o( the most %se(%l additions in "a#a is the a%to losin' o( reso%res like
Inp%tStream whih helps %s red%e boiler plate ode (rom o%r pro'rams)
S%ppose we were writin' a pro'ram whih reads a le and loses the
6ileInp%tStream when it*s done, here is how &o% wo%ld write the pro'ram=
ith Java 6 and Before
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 5/21
I want to point o%t a o%ple o( thin's in this ode) 6irstl&, notie that we
delare the 6ileInp%tStream o%tside the tr& blok !%st so that it an be
aessed in the nall& blok) +he seond obser#ation is that we need to
initiali@e the Inp%tStream to n%ll, so that it is '%aranteed to be initiali@ed
when we aess it in the nall& blok) Last b%t not the least, the is)lose in
the nall& blok ma& throw an ?$eption as well, thereb& hidin' the ori'inal?$eption thrown in the tr& blok ?$eption (rom the aller) Ahat we
probabl& want is to handle the ?$eption thrown (rom is)lose and throw
the ori'inal I?$eption)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 6/21
+he abo#e ode still has a shortomin' that the ?$eption thrown (rom nall&
is s%pressed and not aessible to the allin' ode) I*m not s%re how o(ten
we want to 'et both the ori'inal ?$eption and also the ?$eption thrown
(rom the nall& blok, b%t i( we did want it, we o%ld do alwa&s do somethin'
like this=
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 7/21
S%ppressed?$eption abo#e is a %ser written "a#a bean with a eld named
s%ppressed o( t&pe ?$eption) +he allin' ode an then all
S%pressed?$eption)'et+hreadLoal)'et?$eption to 'et the ?$eption
that was s%pressed in the nall& la%se) 7reat, we sol#ed all the problems
assoiated with the tr&;ath;nall&B <ow we m%st remember to repeat this
e$at se%ene with eah %se o( tr&;ath;nall& when handlin' les or other
reso%res whih need to be losed) ?nter "a#a , and we an do the abo#e
witho%t the boiler plate ode)
ith Java 7
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 8/21
tr& an now ha#e m%ltiple statements in the parenthesis and eah statement
sho%ld reate an ob!et whih implements the new !a#a)lan')C%to/losable
inter(ae) +he C%to/losable inter(ae onsists o( !%st one method)
?ah C%to/losable reso%re reated in the tr& statement will be
a%tomatiall& losedB I( an e$eption is thrown in the tr& blok and another
?$eption is thrown while losin' the reso%re, the rst ?$eption is the one
e#ent%all& thrown to the aller) +he seond ?$eption is a#ailable to the
aller #ia the e$)'etS%pressed method) +hrowable)'etS%pressed is a new
method added on +hrowable in "a#a !%st (or this p%rpose)
!andatory catch "loc when using the try statement
<ote that i( &o% are reatin' C%to/losable reso%res in the tr& blok, &o% are
re%ired to delare a ath blok to ath the onrete e$eption thrown
(rom the at%al C%to/losable) lose method) Clternati#el& &o% need to
throw the ?$eption) +hink o( the lose method as impliitl& bein' alled as
the last line in the tr& blok) So, i( an appliation has its own C%to/losable
lass as (ollows=
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 9/21
+hen, the (ollowin' will a%se a ompile error=
+o $ the abo#e, &o% need to ath or throw the ?$eption (rom the allin'
method)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 10/21
Synta$ for declaring multi%le resources
+he tr& statement an ontain m%ltiple statements separated b& semiolon)
?ah statement m%st res%lt in an C%to/losable ob!et)
It is ille'al to delare an& #ariable whih isn*t an C%to/losable)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 11/21
&ut%ut
?---= tr&;with;reso%res not appliable to #ariable t&pe
re%ired= C%to/loseable
(o%nd= lon'
'uto(losa"le vs (losa"le
+he old /losable inter(ae introd%ed in "a#a 5, whih also has the same
method that now e$tends (rom C%to/losable, impl&in' that all /losable
lasses are a%tomatiall& C%to;/losable) +hose lasses a%tomatiall& beome
reso%res that an be reated in the tr& statement) +he sli'ht di.erene in
C%to/losable and /losable is that %nlike /losable)lose,
C%to/losable)lose is not s%pposed to be idempotent, whih means that
allin' it twie an ha#e side e.ets) +he seond di.erene is that sine
e$eptions thrown (rom C%to/losable) lose are s%ppressed,
C%to/losable)lose sho%ld not throw e$eptions whih an a%se problems
when s%ppressed, like the Interr%pted?$eption)
#) !ore %recise rethrow
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 12/21
+here are o(ten sit%ations when we want to ath m%ltiple e$eptions o(
di.erent t&pes, do Dsomethin'E with them, and rethrow them) Let %s
onsider this e$ample, where we ha#e some ode whih throws I?$eption
and SFL?$eption)
In the abo#e e$ample, we are lo''in' eah t&pe o( e$eption bein' thrown
b& the tr& blok be(ore rethrowin' them) +his res%lts in a d%pliation o( odein the ath bloks) :e(ore "a#a , to 'et aro%nd the ode d%pliation iss%e
we wo%ld ath the base e$eption as (ollows=
owe#er, this re%ires %s to rethrow the base ?$eption t&pe
!a#a)lan')?$eption (rom the allin' method)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 13/21
p%bli stati #oid mainStrin'GH ar's throws ?$eption
ith Java 7
"a#a has added the Dpreise rethrowE (eat%re whih lets &o% ath and
throw the base e$eption while still throwin' the preise e$eption (rom the
allin' method)
<ote the ke&word nal in the abo#e ath la%se) Ahen a parameter is
delared nal, the ompiler statiall& knows to onl& throw those heked
e$eptions that were thrown b& the tr& blok and were not a%'ht in an&
preedin' ath bloks)
#* !ulti-catch
+here is no ele'ant wa& with "a#a J to ath m%ltiple e$eptions o( di.erent
t&pes and handle them in the same wa&)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 14/21
Ko% an alwa&s ath the parent ?$eption in order to a#oid d%pliation o(
ode, b%t it is not alwa&s s%itable espeiall& i( the parent is
!a#a)lan')?$eption) "a#a lets &o% ath m%ltiple ?$eptions in a sin'le
ath blok b& denin' a D%nionE o( ?$eptions to be a%'ht in the ath
la%se)
<ote that the pipe * harater is %sed as the delimiter) +he #ariable e$* in
the abo#e e$ample is statiall& t&ped to the base lass o( ?$1 and ?$2, whih
is !a#a)lan')?$eption in this ase)
#+ Binary integral literals
Aith "a#a , &o% an now reate n%merial literals %sin' binar& notation
%sin' the pre$ D0bE
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 15/21
int n N 0b100000O
S&stem)o%t)printlnDn N E P nO
&ut%ut
n N 32
#6 ,nderscores in numeric literals
Aith "a#a , &o% an inl%de %ndersores in n%meri literals to make them
more readable) +he %ndersore is onl& present in the representation o( the
literal in "a#a ode, and will not show %p when &o% print the #al%e)
ithout underscore
int tenMillion N 10000000O
S&stem)o%t)printlnDCmo%nt is D P tenMillionO
&ut%ut
10000000
ith underscore
int tenMillion:%tMore-eadable N 10Q000Q000O
S&stem)o%t)printlnDCmo%nt is E P tenMillion:%tMore-eadableO
&ut%ut
10000000
!ore rules and e$am%les
1) /onse%ti#e %ndersores is le'al)
int n N 1000QQQQQQ000O
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 16/21
2) Rndersores an be inl%ded in other n%meri t&pes as well)
do%ble d N 1000Q000)0dO
lon' l N 1000Q000lO
int he$ N 0$deadQ0deO
int b&tes N 0$1000Q0000O
3) Rndersore an be inl%ded a(ter the deimal point)
do%ble e$ample8 N 1000Q000)000Q000dO
4) It is ille'al to start a literal with an %ndersore
5) It is ille'al to end a literal with an %ndersore)
J) It is also ille'al to ha#e %ndersore be !%st be(ore or a(ter a deimal point)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 17/21
7 .m%roved ty%e inference for generic instance creation
"a#a 5 introd%ed 'eneris whih enabled de#elopers to write t&pe sa(e
olletions) owe#er, 'eneris an sometimes be too #erbose) /onsider the
(ollowin' e$ample where we are reatin' a Map o( List o( Strin')
ith Java + and 6
MapStrin', ListStrin'TT retVal N new ashMapStrin', ListStrin'TTO
<ote that the (%ll t&pe is speied twie and is there(ore red%ndant)
Rn(ort%natel&, this was a limitation o( "a#a 5 and J)
ith Java 7
"a#a tries to 'et rid o( this red%ndan& b& introd%in' a le(t to ri'ht t&pe
in(erene) Ko% an now rewrite the same statement b& %sin' the T
onstr%t)
MapStrin', ListStrin'TT retVal N new ashMapTO
+his does make the ode a little less #erbose) Ko% an also %se T onstr%t
when ret%rnin' a newl& reated ob!et)
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 18/21
+his, in m& opinion, 'oes onl& hal( wa&) +he (%ll sol%tion wo%ld ha#e been a
ri'ht to le(t (%ll t&pe in(erene)
Map map N new ashMapStrin', Strin'TO
+he abo#e wo%ld ha#e made the ode e#en less #erbose) +ho%'h this
enhanement an still be done in a later #ersion)
#/ !ore new .0& '.s for the Java %latform N.&23
C new set o( IU CIs and (eat%res were introd%ed in "a#a 1)4 %nder the
!a#a)nio paka'e) +his addition was alled the <ew IU CIs, also known as
<I) <amin' somethin' <ew is alwa&s short;si'hted bea%se it will notremain new (ore#er) Ahen the ne$t #ersion omes alon', what sho%ld the
new #ersion be alled, the <?A <?A IU "a#a 1) o.ers a rih set o( new
(eat%res and IU apabilities, alled <I)2 <ew IU #ersion 2) ere are the
ke& hi'hli'hts o( <I)2)
a3 acage
+he most important paka'e to look (or is !a#a)nio)le) +his paka'e ontains
man& pratial le %tilities, new le IU related lasses and inter(aes)
"3 4he 5avanioleath interface
ath is probabl& the new lass that de#elopers will %se most o(ten) +he le
re(erred b& the path does not need to e$ist) +he le re(erred to does not
need to e$ist) 6or all pratial p%rposes, &o% an think o( replain' !a#a)io)6ile
with !a#a) io)ath)
&ld ay
6ile le N new 6ileDhello)t$tEO
S&stem)o%t)printlnD6ile e$ists NN E P le)e$istsO
New ay
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 19/21
ath path N 6ileS&stems)'etWe(a%lt)'etathDhello)t$tEO
S&stem)o%t)printlnDath e$ists NN E P 6iles)e$istspathO
c3 4he 5avanioleiles class
+he 6iles lass o.ers o#er 50 %tilit& methods (or 6ile related operations whih
man& de#elopers wo%ld ha#e wanted to be a part o( earlier "a#a releases)
ere are some methods to 'i#e &o% a sense o( the ran'e o( methods o.ered)
X op& Y op& a le, with options like -?LC/?Q?ZIS+I<7, <6LLAQLI<[S
p%bli stati ath op&ath so%re, ath tar'et, /op&ption\ optionsO
X move3 Y mo#e or rename a le p%bli stati ath mo#eath so%re, athtar'et, /op&ption\ optionsO
X new.n%utStream3 Y reate inp%t stream p%bli stati Inp%tStream
newInp%tStreamath path, penption\ optionsO
X read'llBytes3 Y similar to the Cpahe IRtils)read6ile;+o:&teCrra& p%bli
stati b&teGH readCll:&tesath path throws I?$eptionO
X createSym"olic8in3 Y reates a s&mboli link, i( s%pported b& the le
s&stem p%bli stati ath reateS&mboliLinkath link, ath tar'et,
6ileCttrib%teT\ attrs throws I?$eption
d3 atchService '.
AathSer#ie CI is a new (eat%re introd%ed in "a#a 1)) It pro#ides an CI
that lets &o% DlistenE to a ertain t&pe o( le s&stem e#ents) Ko%r ode 'etsalled a%tomatiall& when those e#ents o%r) ?$amples o( e#ent t&pes are
apt%red b& StandardAath?#ent[inds lass)
X ?<+-KQ/-?C+?=an entr& is reated or renamed in the diretor&
X ?<+-KQW?L?+?=an entr& is reated or renamed o%t o( the diretor&
X ?<+-KQMWI6K=a diretor& entr& is modied
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 20/21
?$ample
ere*s a (%ll e$ample o( how to wath a diretor& and print an& newl&
reated les)
-%n the abo#e pro'ram) +hen reate a le new)t$t* in the diretor& lo's*)
+he pro'ram will print=
7/26/2019 Java 1.7 Features
http://slidepdf.com/reader/full/java-17-features 21/21
lo's= new le reated new)t$t
Note a"out atchService im%lementation
+he implementation will take ad#anta'e o( nati#e s%pport (or le han'e
notiation when s%pported b& the nati#e le s&stem, b%t will resort to
pollin' otherwise)
.9: Su%%ort
"a#a s%pport is now a#ailable in <et:eans and Intelli") ?lipse does not
s%pport "a#a &et) +he %pomin' release o( ?lipse 3) will not ha#e s%pport
(or "a#a either, b%t s%pport will be added in 3) S-1, e$peted September2011 table 2)
(onclusion
"a#a o.ers man& small lan'%a'e enhanements and (eat%res) owe#er, I
did not nd an& (eat%re as ompellin' as -e'e$ enhanement in "a#a 1)4 or
7eneris, C%to;bo$in' or ?n%m enhanement in "a#a 1)5) I nd the tr&;with;
reso%res enhanement parti%larl& %se(%l and am lookin' (orward to %sin'
it) I also look (orward to %sin' new (eat%res (rom the <I)2 librar&) #erall, I
am 'lad that somethin' was released this &ear as opposed to releasin' a
monolith o( han'es ne$t &ear)