Upload
somesh-suman
View
249
Download
0
Embed Size (px)
Citation preview
7/22/2019 Durga Core Java Book
1/459
1
Language Fundamentals1. Identifiers2. Reserved words3. Data types4. Literals5. Arrays6. Types of variabless7. ar ar! "et#od$. %ain "et#od&. 'o""and line ar!("ents1). *ava +odin! standardsIdentifier: A na"e in ,ava pro!ra" is +alled identifier. It "ay be +lass na"e- "et#od na"e-
variable na"e and label na"e.
Example:
Rules to define java identifiers:
Rule 1:T#e only allowed +#ara+ters in ,ava identifiers are
1/ a to 02/ A to 3/ ) to &4/ 5/
Rule 2:If we are (sin! any ot#er +#ara+ter we will !et +o"pile ti"e error.
Example:
1/ totaln("bervalid2/ Totalinvalid
Rule 3:identifiers are not allowed to starts wit# di!it.
Example:
1/ A'123valid2/ 123A'invalid
Rule 4:,ava identifiers are +ase sensitive (p +o(rse ,ava lan!(a!e itself treated as +ase sensitive
lan!(a!e.
7/22/2019 Durga Core Java Book
2/459
2
Example:
+lass Test
int n("ber81)9int :("ber82)9
int :;%
7/22/2019 Durga Core Java Book
3/459
3
Reserved %ords:
In ,ava so"e identifiers are reserved to asso+iate so"e f(n+tionality or "eanin! s(+#type of reserved identifiers are +alled reserved words.
'iagram:
Reserved %ords for data t(pes:
1/ byte2/ s#ort3/ int4/ lon!5/ float6/ do(ble7/ +#ar$/ boolean
Reserved %ords for flo% $ontrol:
1/ if2/ else3/ swit+#4/ +ase5/ defa(lt6/ for7/ do
7/22/2019 Durga Core Java Book
4/459
4
$/ w#ile&/ brea?1)/+ontin(e11/ret(rn
)e(%ords for modifiers:
1/ p(bli+2/ private3/ prote+ted4/ stati+5/ final6/ abstra+t7/ syn+#roni0ed$/ native&/ stri+tfpB1.2 version/1)/transient11/volatile
)e(%ords for ex$eption #andling:
1/ try2/ +at+#3/ finally4/
t#row
5/ t#rows6/ assertB1.4 version/
*lass related +e(%ords:
1/ +lass2/ pa+?a!e3/ i"port4/ eEtends5/ i"ple"ents6/
interfa+e
!,je$t related +e(%ords:
1/ new2/ instan+eof3/ s(per4/ t#is
-oid return t(pe +e(%ord:
If a "et#od wont ret(rn anyt#in! +o"p(lsory t#at "et#od s#o(ld be de+lared wit# t#evoid ret(rn type in ,ava b(t it is optional in 'FF.
7/22/2019 Durga Core Java Book
5/459
5
1/ void.nused +e(%ords:
goto 'reate several proble"s in old lan!(a!es and #en+e it is banned in ,ava.*onst: ;se final instead of t#is.
y "ista?e if we are (sin! t#ese ?eywords in o(r pro!ra" we will !et +o"pile ti"eerror.
Reserved literals:
1/ tr(e val(es for boolean data type.2/ false3/ n(ll defa(lt val(e for ob,e+t referen+e.
Enum:
T#is ?eyword introd(+ed in 1.5v to define a !ro(p of na"ed +onstantsExample:
en(" eer
GH- R'- G- H9
=
/ote:All reserved words in ,ava +ontain only lower+ase alp#abet sy"bols.
:ew ?eywords are
Ctri+tfp1.2
Assert1.4
7/22/2019 Durga Core Java Book
6/459
6
'ata t(pes:
7/22/2019 Durga Core Java Book
7/459
7
Example:
byte b81)9
byte b2813)9JJ'.
7/22/2019 Durga Core Java Book
8/459
$
4/ float follows sin!le pre+ision. 4/ do(ble follows do(ble pre+ision.,oolean data t(pe:
Ci0e :ot appli+able Bvirt(al "a+#ine dependent/Ran!e :ot appli+able b(t allowed val(es are tr(e or false.
"#i$# of t#e follo%ing ,oolean de$larations are valid&
Example 1:
boolean b8tr(e9
boolean b8Tr(e9JJ'.
7/22/2019 Durga Core Java Book
9/459
&
Do(ble $ bytes 1.7e3)$ to
1.7e3)$
Do(ble ).)
oolean :ot appli+able :otappli+ableBb(t
allowed val(es
tr(eOfalse/
oolean false
'#ar 2 bytes ) to 65535 '#ara+ter )Brepresents
blan? spa+e/
T#e defa(lt val(e for t#e ob,e+t referen+es is Kn(ll.Literals:
Any +onstant val(e w#i+# +an be assi!ned to t#e variable is +alled literal.Example:
Integral Literals: Hor t#e inte!ral data types Bbyte- s#ort- int and lon!/ we +an spe+ify literal
val(e in t#e followin! ways.
1/ 'e$imal literals:Allowed di!its are ) to &.Example: int E81)9
2/ !$tal literals: Allowed di!its are ) to 7. Literal val(e s#o(ld be prefiEed wit# 0ero.Example:int E8)1)9
3/ exa 'e$imal literals: T#e allowed di!its are ) to &- A to . Hor t#e eEtra di!its we +an(se bot# (pper +ase and lower +ase +#ara+ters. T#is is one of very few areas w#ere ,avais not +ase sensitive. Literal val(e s#o(ld be prefiEed wit# oEBor/oP.
Example: int E8)E1)9 T#ese are t#e only possible ways to spe+ify inte!ral literal.
"#i$# of t#e follo%ing are valid de$larations&
1/ int E8)7$69JJ'.
7/22/2019 Durga Core Java Book
10/459
1)
Example:
int E81)9Bvalid/
lon! l81)L9Bvalid/
lon! l81)9Bvalid/
int E81)l9JJ'.
7/22/2019 Durga Core Java Book
11/459
11
Example:
do(ble d81)e29JJ881)U1)2Bvalid/
Cyste".o(t.printlnBd/9JJ1))).)
float f81)e29JJ'.
7/22/2019 Durga Core Java Book
12/459
12
Y Cin!le V(ote
Y Do(ble V(ote
YY a+? spa+e"#i$# of t#e follo%ing $#ar de$larations are valid&
1/ +#ar +#8a9JJ'.
7/22/2019 Durga Core Java Book
13/459
13
Example:
int> a9JJre+o""ended to (se be+a(se na"e is +learly separated fro" t#e type
int >a9int a>9
At t#e ti"e of de+laration we +ant spe+ify t#e si0e ot#erwise we will !et +o"pile ti"eerror.
Example:
int> a9JJvalid
int5> a9JJinvalid
Two dimensional array declaration:
Example:
int[][] a;int [][]a;
int a[][]; All are valid.
int[] []a;
int[] a[];
int []a[];
8#ree dimensional arra( de$laration:
Example:
int>>> a9
int >>>a9
int a>>>9
int> >>a9
int> a>>9 7ll are valid9
int> >a>9
int>> >a9
int>> a>9
int >a>>9
int >>a>9
"#i$# of t#e follo%ing de$larations are valid&
1/ int> a1-b19JJa1-b1Bvalid/2/ int> a2>-b29JJa2-b1Bvalid/3/ int> >a3-b39JJa2-b2Bvalid/4/ int> a->b9JJ'.
7/22/2019 Durga Core Java Book
14/459
14
Example:
7rra( $onstru$tion: a8new int3>9
'iagram:
Hor every array type +orrespondin! +lasses are available b(t t#ese +lasses are part of,ava lan!(a!e and not available to t#e pro!ra""er level.
7rra( 8(pe $orresponding $lass name
int> I
int>> I
do(ble>
.
.
D
.
.
Rule 1:
At t#e ti"e of array +reation +o"p(lsory we s#o(ld spe+ify t#e si0e ot#erwise we will!et +o"pile ti"e error.
Example:
int> a8new int3>9
int> a8new int>9JJ'. a8new int)>9
Cyste".o(t.printlnBa.len!t#/9JJ)
Rule 3:
If we are ta?in! array si0e wit# ve int val(e t#en we will !et r(nti"e eE+eption sayin!:e!ativeArrayCi0e a8new int3>9JJR.
7/22/2019 Durga Core Java Book
15/459
15
byte b81)9
int> a8new intb>9JJBvalid/
s#ort s82)9int> a8new ints>9JJBvalid/
int> a8new int1)l>9JJ'. a8new int1).5>9JJ'..Example:
int> a18new int21474$3647>9Bvalid/
int> a28new int21474$364$>9JJ'.
7/22/2019 Durga Core Java Book
16/459
16
"#i$# of t#e follo%ing de$larations are valid&
1/ int> a8new int>JJ'.> a8new int3>4>9Bvalid/3/ int>> a8new int3>>9Bvalid/4/ int>> a8new int>4>9JJ'.Q eEpe+tedBinvalid/5/ int>>> a8new int3>4>5>9Bvalid/6/ int>>> a8new int3>4>>9Bvalid/7/ int>>> a8new int3>>5>9JJ'.Q eEpe+tedBinvalid/
7rra( initialiation @#enever we are +reatin! an array every ele"ent is initiali0ed wit# defa(lt
val(e a(to"ati+ally.
Example 1:
int> a8new int3>9
Cyste".o(t.printlnBa/9JJI[3e25a5
Cyste".o(t.printlnBa)>/9JJ)
'iagram:
/ote:@#enever we are tryin! to print any ob,e+t referen+e internally toCtrin!B/ "et#od will be
eEe+(ted w#i+# is i"ple"ented by defa(lt to ret(rn t#e followin!.
+lassna"e[#eEade+i"alstrin!representationof#as#+ode.
Example 2:
Cyste".o(t.printlnBa/9JJI[3e25a5
Cyste".o(t.printlnBa)>/9JJI[1&$21f
Cyste".o(t.printlnBa)>)>/9JJ)
'iagram:
7/22/2019 Durga Core Java Book
17/459
17
Example 3:
int>> a8new int2>>9
Cyste".o(t.printlnBa/9JJI[3e25a5
Cyste".o(t.printlnBa)>/9JJn(ll
Cyste".o(t.printlnBa)>)>/9JJR.
7/22/2019 Durga Core Java Book
18/459
1$
+#ar> +#8QaQ-QeQ-QiQ-QoQ-Q(Q=9Bvalid/
Ctrin!> s8NbalayyaN-Nven?iN-Nna!N-N+#ir(N=9Bvalid/
@e +an eEtend t#is s#ort +(t even for "(lti di"ensional arrays also.Example:
int>> a81)-2)-3)=-4)-5)==9]
'iagram:
Example:
int>>> a81)-2)-3)=-4)-5)==-6)=-7)-$)=-&)-1))-11)===9'iagram:
int>>> a81)-2)-3)=-4)-5)==-6)=-7)-$)=-&)-1))-11)===9
Cyste".o(t.printlnBa)>1>1>/9JJ5)Bvalid/
Cyste".o(t.printlnBa1>)>2>/9JJR.2>2>/9JJ11)Bvalid/
Cyste".o(t.printlnBa2>1>)>/9JJR.
7/22/2019 Durga Core Java Book
19/459
1&
Example:
int> E8new int3>9
Cyste".o(t.printlnBE.len!t#B//9JJ'.
7/22/2019 Durga Core Java Book
20/459
2)
int> a8new int>1)-2)-3)-4)=9Bvalid/
Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBs("Bnew int>1)-2)-3)-4)=//9JJ1))
=
p(bli+ stati+ int s("Bint> E/
int total8)9
forBint E1E/
total8totalFE19
=
ret(rn total9
=
=
7rra( element assignments:
*ase 1: In t#e +ase of pri"itive array as array ele"ent any type is allowed w#i+# +an be
pro"oted to de+lared type.Example 1:Hor t#e int type arrays t#e allowed array ele"ent types are byte- s#ort- +#ar int.
int> a8new int1)>9
a)>8&79JJBvalid/
a1>8QaQ9JJBvalid/
byte b81)9
a2>8b9JJBvalid/
s#ort s82)9
a3>8s9JJBvalid/
a4>81)l9JJ'. a8new b,e+t1)>9
a)>8new Inte!erB1)/9JJBvalid/
a1>8new b,e+tB/9JJBvalid/
a2>8new Ctrin!BNb#as?arN/9JJBvalid/
7/22/2019 Durga Core Java Book
21/459
21
Example 2:
:("ber> n8new :("ber1)>9
n)>8new Inte!erB1)/9JJBvalid/n1>8new Do(bleB1).5/9JJBvalid/
n2>8new Ctrin!BNb#as?arN/9JJ'. r8new R(nnable1)>9
r)>8new T#readB/9
r1>8new Ctrin!BNb#as?arN/9JJ'.
7/22/2019 Durga Core Java Book
22/459
22
/ote: In t#e +ase of ob,e+t type arrays +#ild type array +an be assi!n to parent type array
variable.
Example:
Ctrin!> s8NAN-NN=9
b,e+t> o8s9
*ase 2:@#enever we are assi!nin! one array to anot#er array internal ele"ents wont be +opy
,(st referen+e variables will be reassi!ned #en+e si0es are not i"portant b(t types "(st be
"at+#ed.
Example:
int> a81)-2)-3)-4)-5)-6)-7)=9
int> b8$)-&)=9
a8b9JJBvalid/
b8a9JJBvalid/
'iagram:
*ase 3:@#enever we are assi!nin! one array to anot#er array di"ensions "(st be "at+#ed
t#at is in t#e pla+e of one di"ensional array we s#o(ld provide t#e sa"e type only ot#erwise
we will !et +o"pile ti"e error.
Example:
int>> a8new int3>>9
a)>8new int4>5>9JJ'.8new int4>9JJBvalid/
/ote: @#enever we are perfor"in! array assi!n"ents t#e types and di"ensions "(st be
"at+#ed b(t si0es are not i"portant.
Example 1:
7/22/2019 Durga Core Java Book
23/459
23
int>> a8new int3>2>9
a)>8new int3>9
a1>8new int4>9
a8new int4>3>9
'iagram:
Total #ow "any ob,e+ts +reatedW
7ns: 11Sow "any ob,e+ts eli!ible for ^' 6
Example 2:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
Ctrin!> ar!#8NAN-NN=9
ar!s8ar!#9
Cyste".o(t.printlnBar!s.len!t#/9JJ2
forBint i8)9iZ8ar!s.len!t#9iFF/
Cyste".o(t.printlnBar!si>/9
=
=
=
!utput:
,ava Test E y
R9E:ArrayIndeE(tfo(nds
7/22/2019 Durga Core Java Book
24/459
24
R9E:ArrayIndeE(tfo(nds
7/22/2019 Durga Core Java Book
25/459
25
If t#e val(e of a variable is varied fro" ob,e+t to ob,e+t s(+# type of variables are +alledinstan+e variables.
Hor every ob,e+t a separate +opy of instan+e variables will be +reated. Instan+e variables will be +reated at t#e ti"e of ob,e+t +reation and destroyed at t#e
ti"e of ob,e+t destr(+tion #en+e t#e s+ope of instan+e variables is eEa+tly sa"e as
s+ope of ob,e+ts.
Instan+e variables will be stored on t#e #eap as t#e part of ob,e+t. Instan+e variables s#o(ld be de+lared wit# in t#e +lass dire+tly b(t o(tside of any
"et#od or blo+? or +onstr(+tor.
Instan+e variables +an be a++essed dire+tly fro" Instan+e area. (t +annot be a++esseddire+tly fro" stati+ area.
(t by (sin! ob,e+t referen+e we +an a++ess instan+e variables fro" stati+ area.Example:
+lass Test
int i81)9
p(bli+ stati+ void "ainBCtrin!> ar!s/
JJCyste".o(t.printlnBi/9JJ'.
7/22/2019 Durga Core Java Book
26/459
26
Cyste".o(t.printlnBt.b/9JJfalse
=
= Instan+e variables also ?nown as ob,e+t level variables or attrib(tes.
tati$ varia,les:
If t#e val(e of a variable is not varied fro" ob,e+t to ob,e+t s(+# type of variables is notre+o""ended to de+lare as instan+e variables. @e #ave to de+lare s(+# type of
variables at +lass level by (sin! stati+ "odifier.
In t#e +ase of instan+e variables for every ob,e+t a separate +opy will be +reated b(t int#e +ase of stati+ variables for entire +lass only one +opy will be +reated and s#ared by
every ob,e+t of t#at +lass.
Ctati+ variables will be +rated at t#e ti"e of +lass loadin! and destroyed at t#e ti"e of+lass (nloadin! #en+e t#e s+ope of t#e stati+ variable is eEa+tly sa"e as t#e s+ope of t#e
9$lassfile.
Ctati+ variables will be stored in "et#od area. Ctati+ variables s#o(ld be de+lared wit# int#e +lass dire+tly b(t o(tside of any "et#od or blo+? or +onstr(+tor.
Ctati+ variables +an be a++essed fro" bot# instan+e and stati+ areas dire+tly. @e +an a++ess stati+ variables eit#er by +lass na"e or by ob,e+t referen+e b(t (sa!e of
+lass na"e is re+o""ended.
(t wit#in t#e sa"e +lass it is not reV(ired to (se +lass na"e we +an a++ess dire+tly.1/ Ctart *%.2/ 'reate and start %ain T#read by *%.3/ Lo+ateBfind/ Test.+lass by "ain T#read.4/ Load Test.+lass by "ain T#read.5/
7/22/2019 Durga Core Java Book
27/459
27
=
=
Hor t#e stati+ variables it is not reV(ired to perfor" initiali0ation eEpli+itly- *% willalways provide defa(lt val(es.
Example:
+lass Test
stati+ Ctrin! s9
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBs/9JJn(ll
==
Example:
+lass Test
int E81)9
stati+ int y82)9
p(bli+ stati+ void "ainBCtrin!> ar!s/
Test t18new TestB/9t1.E8$$$9
t1.y8&&&9
Test t28new TestB/9
Cyste".o(t.printlnBt2.EFNNFt2.y/9JJ1)&&&
=
=
'iagram:
Ctati+ variables also ?nown as +lass level variables or fields.Lo$al varia,les:
Co"e ti"e to "eet te"porary reV(ire"ents of t#e pro!ra""er we +an de+larevariables inside a "et#od or blo+? or +onstr(+tors s(+# type of variables are +alled lo+al
variables or a(to"ati+ variables or te"porary variables or sta+? variables.
7/22/2019 Durga Core Java Book
28/459
2$
T#e lo+al variables will be +reated as part of t#e blo+? eEe+(tion in w#i+# it is de+laredand destroyed on+e t#at blo+? eEe+(tion +o"pletes. Sen+e t#e s+ope of t#e lo+al
variables is eEa+tly sa"e as s+ope of t#e blo+? in w#i+# we de+lared.Example 1:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
int i8)9
forBint ,8)9,Z39,FF/
i8iF,9=
=
=
Example 2:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
try
int i8Inte!er.parseIntBNtenN/9
=
+at+#B:(ll\ointer
7/22/2019 Durga Core Java Book
29/459
2&
Hor t#e lo+al variables *% wont provide any defa(lt val(es +o"p(lsory we s#o(ldperfor" initiali0ation eEpli+itly before (sin! t#at variable.
Example:
Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
int E9
ifBar!s.len!t#)/
E81)9
=
Cyste".o(t.printlnBE/9JJ'. ar!s/
int E9
ifBar!s.len!t#)/
E81)9
=
else
E82)9
=
Cyste".o(t.printlnBE/9
=
7/22/2019 Durga Core Java Book
30/459
3)
=
!utput:
,ava Test E1)
,ava Test E y
1)
,ava Test
2)
It is never re+o""ended to perfor" initiali0ation for t#e lo+al variables inside lo!i+alblo+?s be+a(se t#ere is no !(arantee of eEe+(tin! t#at blo+? always at r(nti"e.
It is #i!#ly re+o""ended to perfor" initiali0ation for t#e lo+al variables at t#e ti"e ofde+laration at least wit# defa(lt val(es.
/ote: T#e only appli+able "odifier for lo+al variables is final. If we are (sin! any ot#er
"odifier we will !et +o"pile ti"e error.
Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
p(bli+ int E81)9private int E81)9
prote+ted int E81)9 *9E: illegal start of expression
stati+ int E81)9
volatile int E81)9
transient int E81)9
final int E81)9JJvalid;
=
=
*on$lusions:1/ Hor t#e stati+ and instan+e variables it is not reV(ired to perfor" initiali0ation eEpli+itly
*% will provide defa(lt val(es. (t for t#e lo+al variables *% wont provide any
defa(lt val(es +o"p(lsory we s#o(ld perfor" initiali0ation eEpli+itly before (sin! t#at
variable.
2/ Hor every ob,e+t a separate +opy of instan+e variable will be +reated w#ereas for entire+lass a sin!le +opy of stati+ variable will be +reated. Hor every T#read a separate +opy of
lo+al variable will be +reated.
7/22/2019 Durga Core Java Book
31/459
31
3/ Instan+e and stati+ variables +an be a++essed by "(ltiple T#reads si"(ltaneo(sly and#en+e t#ese are not T#read safe b(t lo+al variables +an be a++essed by only one T#read
at a ti"e and #en+e lo+al variables are T#read safe../ initialied arra(s
Example:
+lass Test
int> a9
p(bli+ stati+ void "ainBCtrin!> ar!s/
Test t18new TestB/9
Cyste".o(t.printlnBt1.a/9JJn(llCyste".o(t.printlnBt1.a)>/9JJR./9JJR.9
Cyste".o(t.printlnBob,.a/9JJI[3e25a5
Cyste".o(t.printlnBob,.a)>/9JJ)
tati$ level:
Example 1:
stati+ int> a9
Cyste".o(t.printlnBa/9JJn(ll
Cyste".o(t.printlnBa)>/9JJR.9
Cyste".o(t.printlnBa/9JJI[3e25a5
Cyste".o(t.printlnBa)>/9JJ)
Lo$al level:
Example 1:
int> a9
Cyste".o(t.printlnBa/9 *9E: varia,le a mig#t not #ave ,een initialied
Cyste".o(t.printlnBa)>/9
7/22/2019 Durga Core Java Book
32/459
32
Example 2:
int> a8new int3>9
Cyste".o(t.printlnBa/9JJI[3e25a5Cyste".o(t.printlnBa)>/9JJ)
n+e we +reated an array every ele"ent is always initiali0ed wit# defa(lt val(esirrespe+tive of w#et#er it is stati+ or instan+e or lo+al array.
-ar< arg met#ods varia,le no of argument met#ods; 195;
;ntil 1.4v we +ant de+lared a "et#od wit# variable no. f ar!("ents. If t#ere is a+#an!e in no of ar!("ents +o"p(lsory we #ave to define a new "et#od. T#is approa+#
in+reases len!t# of t#e +ode and red(+es readability. (t fro" 1.5 version onwards we
+an de+lare a "et#od wit# variable no. f ar!("ents s(+# type of "et#ods are +alled
varar! "et#ods. @e +an de+lare a varar! "et#od as follows.
@e +an +all or invo?e t#is "et#od by passin! any no. f int val(es in+l(din! 0eron("ber.
Example:
+lass Test
p(bli+ stati+ void "et#odneBint... E/
Cyste".o(t.printlnBNvarar! "et#odN/9
=
p(bli+ stati+ void "ainBCtrin!> ar!s/
"et#odneB/9
"et#odneB1)/9
"et#odneB1)-2)-3)/9
=
=
!utput:
varar! "et#od
varar! "et#od
varar! "et#od
Internally varar! para"eter i"ple"ented by (sin! sin!le di"ensional array #en+ewit#in t#e varar! "et#od we +an different ar!("ents by (sin! indeE.
Example:
7/22/2019 Durga Core Java Book
33/459
33
+lass Test
p(bli+ stati+ void s("Bint... E/
int total8)9
forBint i8)9iZE.len!t#9iFF/
total8totalFEi>9
=
Cyste".o(t.printlnBNT#e s(" NFtotal/9
=
p(bli+ stati+ void "ainBCtrin!> ar!s/
s("B/9
s("B1)/9
s("B1)-2)/9
s("B1)-2)-3)-4)/9
=
=
!utput:
T#e s(" )T#e s(" 1)
T#e s(" 3)
T#e s(" 1))
*ase 1:
"#i$# of t#e follo%ing var
7/22/2019 Durga Core Java Book
34/459
34
"et#odneBint... a-int b/Binvalid/
*ase 4:@e +an ta?e only one varar! para"eter inside varar! "et#od
Example:"et#odneBint... a-int... b/Binvalid/
*ase 5:
+lass Test
p(bli+ stati+ void "et#odneBint i/
Cyste".o(t.printlnBN!eneral "et#odN/9
=
p(bli+ stati+ void "et#odneBint... i/
Cyste".o(t.printlnBNvarar! "et#odN/9
=
p(bli+ stati+ void "ainBCtrin!> ar!s/
"et#odneB/9JJvarar! "et#od
"et#odneB1)-2)/9JJvarar! "et#od
"et#odneB1)/9JJ!eneral "et#od
==
In !eneral varar! "et#od will !et least priority t#at is if no ot#er "et#od "at+#ed t#enonly varar! "et#od will !et t#e +#an+e t#is is eEa+tly sa"e as defa(lt +ase inside a
swit+#.
*ase 6:Hor t#e varar! "et#ods we +an provide t#e +orrespondin! type array as ar!("ent.
Example:
+lass Test
Cyste".o(t.printlnBNvarar! "et#odN/9
=
p(bli+ stati+ void "ainBCtrin!> ar!s/
"et#odneBnew int>1)-2)-3)=/9JJvarar! "et#od
=
7/22/2019 Durga Core Java Book
35/459
35
=
*ase :
+lass Test
p(bli+ void "et#odneBint> i/=
p(bli+ void "et#odneBint... i/=
=
!utput:
'o"pile ti"e error.
'annot de+lare bot# "et#odneBint.../ and "et#odneBint>/ in Test
ingle 'imensional 7rra( -s -ar/9
=
=
p(bli+ stati+ void "ainBCtrin!> ar!s/
int> l81)-2)-3)=9
7/22/2019 Durga Core Java Book
36/459
36
int> "84)-5)=9
"et#odneBl-"/9
=
=
!utput:
1)
4)
7nal(sis:
=ain =et#od
@#et#er t#e +lass +ontains "ainB/ "et#od or not and w#et#er it is properly de+lared ornot t#ese +#e+?in!s are not responsibilities of t#e +o"piler- at r(nti"e *% is
responsible for t#is. If ,v" (nable to find t#e reV(ired "ainB/ "et#od t#en we will !et
r(nti"e eE+eption sayin! :oC(+#%et#od
7/22/2019 Durga Core Java Book
37/459
37
1/ T#e order of "odifiers is not i"portant t#at is instead of p(bli+ stati+ we +an ta?e stati+p(bli+.
2/ @e +an de+lare strin!> in any a++eptable for"1/ Ctrin!> ar!s2/ Ctrin! >ar!s3/ Ctrin! ar!s>
3/ Instead of ar!s we +an (se any valid ,ava identifier.4/ @e +an repla+e strin!> wit# varar! para"eter.
Example:
"ainBCtrin!... ar!s/
5/ "ainB/ "et#od +an be de+lared wit# t#e followin! "odifiers. final- syn+#roni0ed- stri+tfp.
"#i$# of t#e follo%ing main; met#od de$larations are valid&
1/ p(bli+ stati+ void "ainBCtrin! ar!s/=Binvalid/2/ p(bli+ syn+#roni0ed final stri+tfp void "ainBCtrin!> ar!s/= Binvalid/3/ p(bli+ stati+ void %ainBCtrin!... ar!s/= Binvalid/4/ p(bli+ stati+ int "ainBCtrin!> ar!s/=JJint ret(rn type we +anQt ta?eJJBinvalid/5/ p(bli+ stati+ syn+#roni0ed final stri+tfp void "ainBCtrin!... ar!s/=Bvalid/
In %#i$# of t#e a,ove $ases %e %ill get $ompile time error&
:o +ase- in all t#e +ases we will !et r(nti"e eE+eption. verloadin! of t#e "ainB/ "et#od is possible b(t *% always +alls strin!> ar!("ent
"ainB/ "et#od only.
Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBNCtrin!> array "ain "et#odN/9 overloaded met#ods
=
p(bli+ stati+ void "ainBint> ar!s/
Cyste".o(t.printlnBNint> array "ain "et#odN/9
=
=
!utput:
Ctrin!> array "ain "et#od
T#e ot#er overloaded "et#od we #ave to +all eEpli+itly t#en only it will be eEe+(ted.
7/22/2019 Durga Core Java Book
38/459
3$
In#eritan+e +on+ept is appli+able for stati+ "et#ods in+l(din! "ainB/ "et#od #en+ew#ile eEe+(tin! +#ild +lass if t#e +#ild +lass doesnt +ontain "ainB/ "et#od t#en t#e
parent +lass "ainB/ "et#od will be eEe+(ted.Example 1:
+lass \arent
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBNparent "ainN/9 arent9java
=
=
+lass '#ild eEtends \arent=
7nal(sis:
Example 2:
+lass \arent
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBNparent "ainN/9
=
=+lass '#ild eEtends \arent arent9java
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBN'#ild "ainN/9
=
=
7nal(sis:
7/22/2019 Durga Core Java Book
39/459
3&
It see"s to be overridin! +on+ept is appli+able for stati+ "et#ods b(t it is not overridin!it is "et#od #idin!.
*ommand line arguments:
T#e ar!("ents w#i+# are passin! fro" +o""and pro"pt are +alled +o""and linear!("ents. T#e "ain ob,e+tive of +o""and line ar!("ents are we +an +(sto"i0e t#e
be#avior of t#e "ainB/ "et#od.
Example 1:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
forBint i8)9iZ8ar!s.len!t#9iFF/
Cyste".o(t.printlnBar!si>/9
=
=
=
!utput:
,ava Test E y 0
ArrayIndeE(tfo(nds?@operator a+ts as strin! +on+atenation b(t not arit#"eti+ addition.
Example:
+lass Test
7/22/2019 Durga Core Java Book
40/459
4)
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnBar!s)>Far!s1>/9
=
=
!utput:
/9
=
=
!utput:
7/22/2019 Durga Core Java Book
41/459
41
;s(ally +lass na"es are no(ns. C#o(ld starts wit# (pper+ase letter and if it +ontains "(ltiple words every inner word
s#o(ld starts wit# (pper +ase letter.Example:
*oding standards for interfa$es:
;s(ally interfa+e na"es are ad,e+tives. C#o(ld starts wit# (pper +ase letter and if it +ontains "(ltiple words every inner word
s#o(ld starts wit# (pper +ase letter.
Example:
1/ Ceriali0able2; R(nnable adje$tives3/ 'loneable
*oding standards for met#ods:
;s(ally "et#od na"es are eit#er verbs or verb no(n +o"bination. C#o(ld starts wit# lower+ase +#ara+ter and if it +ontains "(ltiple words every inner
word s#o(ld starts wit# (pper +ase letter.
Example:
*oding standards for varia,les:
;s(ally variable na"es are no(ns. C#o(ld starts wit# lower+ase alp#abet sy"bol and if it +ontains "(ltiple words every
inner word s#o(ld starts wit# (pper +ase +#ara+ter.
Example:
len!t#
na"e
salary nouns
a!e
"obile:("ber
*oding standards for $onstants:
;s(ally +onstants are no(ns. C#o(ld +ontain only (pper+ase +#ara+ters and if it +ontains "(ltiple words t#en t#ese
words are separated wit# (nders+ore sy"bol.
7/22/2019 Durga Core Java Book
42/459
42
;s(ally we +an de+lare +onstants by (sin! pu,li$ stati$ final modifiers9Example:
%APAL;< nouns%I:AL;/
Cyste".o(t.printlnBNA +lass "ain "et#od is eEe+(tedN/9
=
=
+lass
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnBN +lass "ain "et#od is eEe+(tedN/9
=
=
+lass '
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnBN' +lass "ain "et#od is eEe+(tedN/9=
=
+lass D
=
!utput:
7/22/2019 Durga Core Java Book
46/459
46
DY*ava,ava A
A +lass "ain "et#od is eEe+(ted
DY*ava,ava
+lass "ain "et#od is eEe+(ted
DY*ava,ava '
' +lass "ain "et#od is eEe+(ted
DY*ava,ava D
/
ArrayList l8new ArrayListB/9
=
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava3 +annot find sy"bol
sy"bol +lass ArrayList
lo+ation +lass Test
ArrayList l8new ArrayListB/9
Test.,ava3 +annot find sy"bol
sy"bol +lass ArrayList
lo+ation +lass Test
7/22/2019 Durga Core Java Book
47/459
47
ArrayList l8new ArrayListB/9
@e +an resolve t#is proble" by (sin! f(lly V(alified na"e K,ava.(til.ArrayList l8new,ava.(til.ArrayListB/9. (t proble" wit# (sin! f(lly V(alified na"e every ti"e is itin+reases len!t# of t#e +ode and red(+es readability.
@e +an resolve t#is proble" by (sin! i"port state"ents.Example:
i"port ,ava.(til.ArrayList9
+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
ArrayList l8new ArrayListB/9
=
=!utput:
DY*ava,ava+ Test.,ava
Sen+e w#enever we are (sin! i"port state"ent it is not reV(ire to (se f(lly V(alifiedna"es we +an (se s#ort na"es dire+tly. T#is approa+# de+reases len!t# of t#e +ode and
i"proves readability.
*ase 1: 8(pes of Import tatements:
T#ere are 2 types of i"port state"ents.1/
7/22/2019 Durga Core Java Book
48/459
4$
+lass %yArrayList eEtends ,ava.(til.ArrayList
= T#e +ode +o"piles fine even t#o(!# we are not (sin! i"port state"ents be+a(se we
(sed f(lly V(alified na"e.
@#enever we are (sin! f(lly V(alified na"e it is not reV(ired to (se i"port state"ent.Ci"ilarly w#enever we are (sin! i"port state"ents it is not reV(ire to (se f(lly V(alified
na"e.
*ase4:
Example:
i"port ,ava.(til.U9
i"port ,ava.sVl.U9+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
Date d8new DateB/9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,avaTest.,ava7 referen+e to Date is a"bi!(o(s- bot# +lass ,ava.sVl.Date in ,ava.sVl and +lass
,ava.(til.Date in ,ava.(til "at+#
Date d8new DateB/9
/ote:
7/22/2019 Durga Core Java Book
49/459
4&
Date d8new DateB/9
==
T#e +ode +o"piles fine and in t#is +ase (til pa+?a!e Date will be +onsidered.*ase6:
@#enever we are i"portin! a pa+?a!e all +lasses and interfa+es present in t#at pa+?a!eare by defa(lt available b(t not s(b pa+?a!e +lasses.
Example:
8o use pattern $lass in our program dire$tl( %#i$# import statement is reAuired&
*ase:
In any ,ava pro!ra" t#e followin! 2 pa+?a!es are not reV(ire to i"port be+a(se t#eseare available by defa(lt to every ,ava pro!ra".
1. ,ava.lan! pa+?a!e2. defa(lt pa+?a!eB+(rrent wor?in! dire+tory/
*aseB:
KI"port state"ent is totally +o"pile ti"e +on+ept if "ore no of i"ports are t#ere t#en"ore will be t#e +o"pile ti"e b(t t#ere is Kno +#an!e in eEe+(tion ti"e.
'ifferen$e ,et%een * language Cin$lude and java language import9
In t#e +ase of ' lan!(a!e in+l(de all t#e #eader files will be loaded at t#e ti"e ofin+l(de state"ent #en+e it follows stati+ loadin!.
(t in ,ava i"port state"ent no K.+lass will be loaded at t#e ti"e of i"port state"entsin t#e neEt lines of t#e +ode w#enever we are (sin! a parti+(lar +lass t#en only
+orrespondin! K.+lass file will be loaded. Sen+e it follows Kdyna"i+ loadin! or Kload
on Mde"and or Kloadonfly.
tati$ import:
T#is +on+ept introd(+ed in 1.5 versions. A++ordin! to s(n stati+ i"port i"provesreadability of t#e +ode b(t a++ordin! to worldwide pro!ra""in! eEports Bli?e (s/ stati+
7/22/2019 Durga Core Java Book
50/459
5)
i"ports +reates +onf(sion and red(+es readability of t#e +ode. Sen+e if t#ere is no
spe+ifi+ reV(ire"ent never re+o""ended to (se a stati+ i"port.
195 versions ne% features1/ Hor
7/22/2019 Durga Core Java Book
51/459
51
==
!utput:
DY*ava,ava+ Test.,avaDY*ava,ava Test
2.)
2)
).43)2$53$47363$&1
Explain a,out (stem9out9println statement&
Example 1 and example 2:
Example 3:
i"port stati+ ,ava.lan!.Cyste".o(t9
+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
o(t.printlnBN#elloN/9
o(t.printlnBN#iN/9==
!utput:
DY*ava,ava+ Test.,ava
DY*ava,ava Test
#ello
#i
Example 4:
i"port stati+ ,ava.lan!.Inte!er.U9
7/22/2019 Durga Core Java Book
52/459
52
i"port stati+ ,ava.lan!.yte.U9
+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnB%APAL;
7/22/2019 Durga Core Java Book
53/459
53
'iagram:
;sa!e of stati+ i"port red(+es readability and +reates +onf(sion #en+e if t#ere is nospe+ifi+ reV(ire"ent never re+o""ended to (se stati+ i"port.
"#at is t#e differen$e ,et%een general import and stati$ import&
@e +an (se nor"al i"ports to i"port +lasses and interfa+es of a pa+?a!e. w#enever weare (sin! nor"al i"port we +an a++ess +lass and interfa+es dire+tly by t#eir s#ort na"eit is not reV(ire to (se f(lly V(alified na"es.
@e +an (se stati+ i"port to i"port stati+ "e"bers of a parti+(lar +lass. w#enever weare (sin! stati+ i"port it is not reV(ire to (se +lass na"e we +an a++ess stati+ "e"bers
dire+tly.
a$+age statement:
It is an en+aps(lation "e+#anis" to !ro(p related +lasses and interfa+es into a sin!le"od(le.
8#e main o,je$tives of pa$+ages are:
To resolve na"e +onfe+ts. To i"prove "od(larity of t#e appli+ation. To provide se+(rity.
T#ere is one (niversally a++epted na"in! +onversion for pa+?a!es t#at is to (se internetdo"ain na"e in reverse.
Example:
o% to $ompile pa$+age program:
Example:
pa+?a!e +o".d(r!a,obs.it,obs9
+lass Syd*obs
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnBNpa+?a!e de"oN/9
7/22/2019 Durga Core Java Book
54/459
54
=
=
*ava+ Syd*obs.,ava !enerated +lass file will be pla+ed in +(rrent wor?in! dire+tory.'iagram:
*ava+ Md . Syd*obs.,ava d "eans destination to pla+e !enerated +lass files K. "eans +(rrent wor?in! dire+tory. ^enerated +lass file will be pla+ed into +orrespondin! pa+?a!e str(+t(re.
'iagram:
If t#e spe+ified pa+?a!e str(+t(re is not already available t#en t#is +o""and itself will+reate t#e reV(ired pa+?a!e str(+t(re.
As t#e destination we +an (se any valid dire+tory.If t#e spe+ified destination is not available t#en we will !et +o"pile ti"e error.
Example:
DY*ava,ava+ d + Syd*obs.,ava
'iagram:
If t#e spe+ified destination is not available t#en we will !et +o"pile ti"e error.Example:
DY*ava,ava+ d 0 Syd*obs.,ava
If is not available t#en we will !et +o"pile ti"e error.o% to exe$ute pa$+age program:
DY*ava,ava +o".d(r!a,obs.it,obs.Syd*obs
At t#e ti"e of eEe+(tion +o"p(lsory we s#o(ld provide f(lly V(alified na"e.*on$lusion 1:
7/22/2019 Durga Core Java Book
55/459
55
In any ,ava pro!ra" t#ere s#o(ld be at "ost one pa+?a!e state"ent t#at is if we areta?in! "ore t#an one pa+?a!e state"ent we will !et +o"pile ti"e error.
Example:pa+?a!e pa+?19
pa+?a!e pa+?29
+lass A
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ A.,ava
A.,ava2 +lass- interfa+e- or en(" eEpe+tedpa+?a!e pa+?29
*on$lusion 2:
In any ,ava pro!ra" t#e 1stnon +e"ent state"ent s#o(ld be pa+?a!e state"ent if it isavailable> ot#erwise we will !et +o"pile ti"e error.
Example:
i"port ,ava.(til.U9
pa+?a!e pa+?19
+lass A
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ A.,ava
A.,ava2 +lass- interfa+e- or en(" eEpe+ted
pa+?a!e pa+?19
0ava sour$e file stru$ture:
All t#e followin! are valid ,ava pro!ra"s.
/ote:An e"pty so(r+e file is a valid ,ava pro!ra".
*lass =odifiers
7/22/2019 Durga Core Java Book
56/459
56
@#enever we are writin! o(r own +lasses +o"p(lsory we #ave to provide so"einfor"ation abo(t o(r +lass to t#e ,v". Li?e
1/ etter t#is +lass +an be a++essible fro" anyw#ere or not.2/ etter +#ild +lass +reation is possible or not.3/ @#et#er ob,e+t +reation is possible or not et+.
@e +an spe+ify t#is infor"ation by (sin! t#e +orrespondin! "odifiers. T#e only appli+able "odifiers for 8op Level+lasses are
1/ \(bli+2/ Defa(lt3/ Hinal4/ Abstra+t5/ Ctri+tfp
If we are (sin! any ot#er "odifier we will !et +o"pile ti"e error.Example:
private +lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
int i8)9
forBint ,8)9,Z39,FF/
i8iF,9=
Cyste".o(t.printlnBi/9
==
!.8.8:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava1 "odifier private not allowed #ere
private +lass Test (t Hor t#e inner +lasses t#e followin! "odifiers are allowed.
'iagram:
"#at is t#e differen$e ,et%een a$$ess spe$ifier and a$$ess modifier& In old lan!(a!es X' Bor/ X'FF pu,li$- private- prote$ted- default are +onsidered as
a++ess spe+ifiers and all t#e re"ainin! are +onsidered as a++ess "odifiers.
7/22/2019 Durga Core Java Book
57/459
57
(t in ,ava t#ere is no s(+# type of division all are +onsidered as a++ess "odifiers.u,li$ *lasses
If a +lass de+lared as p(bli+ t#en we +an a++ess t#at +lass fro" anyw#ere.ED7=LE:
rogram1:
pa+?a!e pa+?19
p(bli+ +lass Test
p(bli+ void "et#odneB/
Cyste".o(t.printlnBNtest +lass "et#odone is eEe+(tedN/9
==
*ompile t#e a,ove program:DY*ava,ava+ d . Test.,ava
rogram2:
pa+?a!e pa+?29
i"port pa+?1.Test9
+lass Test1
p(bli+ stati+ void "ainBCtrin! ar!s>/
Test t8new TestB/9
t."et#odneB/9==
!.8.8:
DY*ava,ava+ d . Test1.,ava
DY*ava,ava pa+?2.Test1
Test +lass "et#odone is eEe+(ted.
If +lass Test is not p(bli+ t#en w#ile +o"pilin! Test1 +lass we will !et +o"pile ti"e errorsayin! pa$+198est is not pu,li$ in pa$+1 $annot ,e a$$essed from outside pa$+age.
'efault *lasses:
If a +lass de+lared as t#e defaultt#en we +an a++ess t#at +lass only %it#in t#e $urrentpa$+age#en+e defa(lt a++ess is also ?nown as Kpa$+age level a$$ess.
Example:
rogram 1:
pa+?a!e pa+?19
+lass Test
p(bli+ void "et#odneB/
Cyste".o(t.printlnBNtest +lass "et#odone is eEe+(tedN/9
7/22/2019 Durga Core Java Book
58/459
5$
==
rogram 2:
pa+?a!e pa+?19i"port pa+?1.Test9
+lass Test1
p(bli+ stati+ void "ainBCtrin! ar!s>/
Test t8new TestB/9
t."et#odneB/9
==
!.8.8:
DY*ava,ava+ d . Test.,avaDY*ava,ava+ d . Test1.,ava
DY*ava,ava pa+?1.Test1
Test +lass "et#odone is eEe+(ted
Final =odifier
Hinal is t#e "odifier appli+able for +lasses- "et#ods and variables.Final =et#ods:
@#atever t#e "et#ods parent #as by defa(lt available to t#e +#ild. If t#e +#ild is not allowed to override any "et#od- t#at "et#od we #ave to de+lare wit#
final in parent +lass. T#at is final "et#ods +annot overridden.Example:
rogram 1:
+lass \arent
p(bli+ void propertyB/
Cyste".o(t.printlnBN+as#F!oldFlandN/9
=
p(bli+ final void "arria!eB/
Cyste".o(t.printlnBNs(bbala?s#"iN/9==
rogram 2:
+lass +#ild eEtends \arent
p(bli+ void "arria!eB/
Cyste".o(t.printlnBNT#a"annaN/9
==
!.8.8:
7/22/2019 Durga Core Java Book
59/459
5&
'o"pile ti"e error.
DY*ava,ava+ \arent.,ava
DY*ava,ava+ +#ild.,ava$#ild9java:3: marriage; in $#ild $annot override marriage; in arent overridden met#od is
final
pu,li$ void marriage;
Final *lass:
If a +lass de+lared as t#e final t#en we +annt +reates t#e +#ild +lass t#at is in#eritan+e+on+ept is not appli+able for final +lasses.
ED7=LE:
rogram 1:
final +lass \arent
=
rogram 2:
+lass +#ild eEtends \arent
=
!.8.8:
'o"pile ti"e error.
DY*ava,ava+ \arent.,avaDY*ava,ava+ +#ild.,ava
$#ild9java:1:$annot in#erit from final arent
+lass +#ild eEtends \arent :ote
7/22/2019 Durga Core Java Book
60/459
6)
7,stra$t =odifier:
Abstra+t is t#e "odifier appli+able only for "et#ods and +lasses b(t not for variables.7,stra$t =et#ods:
7/22/2019 Durga Core Java Book
61/459
61
All t#e 6 +o"binations are ille!al.7,stra$t $lass:
Hor any ,ava +lass if we are not allow to +reate an ob,e+t s(+# type of +lass we #ave tode+lare wit# abstra+t "odifier t#at is for abstra+t +lass instantiation is not possible.
Example:
abstra+t +lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/
Test t8new TestB/9
==
!utput:
'o"pile ti"e error.DY*ava,ava+ Test.,ava
Test.,ava4 Test is abstra+t9 +annot be instantiated
Test t8new TestB/9
"#at is t#e differen$e ,et%een a,stra$t $lass and a,stra$t met#od&
If a +lass +ontain at least on abstra+t "et#od t#en +o"p(lsory t#e +orrespondin! +lasss#o(ld be de+lare wit# abstra+t "odifier. e+a(se i"ple"entation is not +o"plete and
#en+e we +ant +reate ob,e+t of t#at +lass.
7/22/2019 Durga Core Java Book
62/459
62
'o"pile ti"e error.
DY*ava,ava+ \arent.,ava
\arent.,ava3 "issin! "et#od body- or de+lare abstra+tp(bli+ void "et#odneB/9
Example2:
+lass \arent
p(bli+ abstra+t void "et#odneB/=
=
!utput:
'o"pile ti"e error.
\arent.,ava3 abstra+t "et#ods +annot #ave a bodyp(bli+ abstra+t void "et#odneB/=
Example3:
+lass \arent
p(bli+ abstra+t void "et#odneB/9
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ \arent.,ava\arent.,ava1 \arent is not abstra+t and does not override abstra+t "et#od "et#odneB/ in
\arent
+lass \arent
If a +lass eEtends any abstra+t +lass t#en +o"p(lsory we s#o(ld provide i"ple"entationfor every abstra+t "et#od of t#e parent +lass ot#erwise we #ave to de+lare +#ild +lass as
abstra+t.
Example:
abstra+t +lass \arent
p(bli+ abstra+t void "et#odneB/9
p(bli+ abstra+t void "et#odTwoB/9
=
+lass +#ild eEtends \arent
p(bli+ void "et#odneB/=
=
!utput:
7/22/2019 Durga Core Java Book
63/459
63
'o"pile ti"e error.
DY*ava,ava+ \arent.,ava
\arent.,ava6 +#ild is not abstra+t and does not override abstra+t "et#od "et#odTwoB/ in\arent
+lass +#ild eEtends \arent
If we de+lare +lass +#ild as abstra+t t#en t#e +ode +o"piles fine b(t +#ild of +#ild isresponsible to provide i"ple"entation for "et#odTwoB/.
"#at is t#e differen$e ,et%een final and a,stra$t&
Hor abstra+t "et#ods +o"p(lsory we s#o(ld override in t#e +#ild +lass to providei"ple"entation. @#ereas for final "et#ods we +ant override #en+e abstra+t final
+o"bination is ille!al for "et#ods.
Hor abstra+t +lasses we s#o(ld +o"p(lsory +reate +#ild +lass to provide i"ple"entationw#ereas for final +lass we +ant +reate +#ild +lass. Sen+e final abstra+t +o"bination is
ille!al for +lasses.
Hinal +lass +annot +ontain abstra+t "et#ods w#ereas abstra+t +lass +an +ontain final"et#od.
Example:
/ote:
;sa!e of abstra+t "et#ods- abstra+t +lasses and interfa+es is always !ood pro!ra""in!pra+ti+e.
tri$tfp:
stri+tfp is t#e "odifier appli+able for "et#ods and +lasses b(t not for variables.
Ctri+tfp "odifier introd(+ed in 1.2 versions. If a "et#od de+lare as t#e Ctri+tfp t#en all t#e floatin! point +al+(lations in t#at "et#od
#as to follow I
7/22/2019 Durga Core Java Book
64/459
64
If a +lass de+lares as t#e Ctri+tfp t#en every +on+rete "et#odBw#i+# #as body/ of t#at+lass #as to follow I
7/22/2019 Durga Core Java Book
65/459
65
DY*ava,ava+ d . .,ava
.,ava2 pa+?1.A is not p(bli+ in pa+?19 +annot be a++essed fro" o(tside pa+?a!e
i"port pa+?1.A9 In t#e above pro!ra" even t#o(!# "et#odneB/ "et#od is p(bli+ we +ant a++ess fro"
+lass be+a(se t#e +orrespondin! +lass A is not p(bli+ t#at is bot# +lasses and "et#ods
are p(bli+ t#en only we +an a++ess.
'efault mem,er:
If a "e"ber de+lared as t#e defa(lt t#en we +an a++ess t#at "e"ber only wit#in t#e+(rrent pa+?a!e #en+e defa(lt "e"ber is also ?nown as pa+?a!e level a++ess.
Example 1:
rogram 1:
pa+?a!e pa+?19+lass A
void "et#odneB/
Cyste".o(t.printlnBN"et#odne is eEe+(tedN/9
==
rogram 2:
pa+?a!e pa+?19
i"port pa+?1.A9
+lass
p(bli+ stati+ void "ainBCtrin! ar!s>/
A a8new AB/9
a."et#odneB/9
==
!utput:
DY*ava,ava+ d . A.,ava
DY*ava,ava+ d . .,ava
DY*ava,ava pa+?1."et#odne is eEe+(ted
Example 2:
rogram 1:
pa+?a!e pa+?19
+lass A
void "et#odneB/
Cyste".o(t.printlnBN"et#odne is eEe+(tedN/9
7/22/2019 Durga Core Java Book
66/459
66
==
rogram 2:
pa+?a!e pa+?29i"port pa+?1.A9
+lass
p(bli+ stati+ void "ainBCtrin! ar!s>/
A a8new AB/9
a."et#odneB/9
==
!utput:
'o"pile ti"e error.DY*ava,ava+ d . A.,ava
DY*ava,ava+ d . .,ava
.,ava2 pa+?1.A is not p(bli+ in pa+?19 +annot be a++essed fro" o(tside pa+?a!e
i"port pa+?1.A9
rivate mem,ers:
If a "e"ber de+lared as t#e private t#en we +an a++ess t#at "e"ber only wit# in t#e+(rrent +lass.
\rivate "et#ods are not visible in +#ild +lasses w#ere as abstra+t "et#ods s#o(ld bevisible in +#ild +lasses to provide i"ple"entation #en+e private- a,stra$t +o"bination isille!al for "et#ods.
rote$ted mem,ers:
If a "e"ber de+lared as t#e prote+ted t#en we +an a++ess t#at "e"ber wit#in t#e+(rrent pa+?a!e anyw#ere b(t o(tside pa+?a!e only in +#ild +lasses.
\rote+ted8defa(ltF?ids. @e +an a++ess prote+ted "e"bers wit#in t#e +(rrent pa+?a!e anyw#ere eit#er by +#ild
referen+e or by parent referen+e b(t fro" o(tside pa+?a!e we +an a++ess prote+ted
"e"bers only in +#ild +lasses and s#o(ld be by +#ild referen+e only t#at is we +ant (se
parent referen+e to +all prote+ted "e"bers fro" o(tside lan!(a!e.Example:
rogram 1:
pa+?a!e pa+?19
p(bli+ +lass A
prote+ted void "et#odneB/
Cyste".o(t.printlnBN"et#odne is eEe+(tedN/9
==
7/22/2019 Durga Core Java Book
67/459
67
rogram 2:
pa+?a!e pa+?19
+lass eEtends A
p(bli+ stati+ void "ainBCtrin! ar!s>/
A a8new AB/9
a."et#odneB/9
b8new B/9
b."et#odneB/9
A a18new B/9
a1."et#odneB/9
==!utput:
DY*ava,ava+ d . A.,ava
DY*ava,ava+ d . .,ava
DY*ava,ava pa+?1.
"et#odne is eEe+(ted
"et#odne is eEe+(ted
"et#odne is eEe+(ted
Example 2:
*ompression of privateH defaultH prote$ted and pu,li$:
visi,ilit( private default prote$ted pu,li$
1;"it# in t#e
same $lass
2;From $#ild
$lass of same
pa$+age
3;From non/
Test t8new TestB/9
Cyste".o(t.printlnBt.i/9
==
!utput:
DY*ava,ava+ Test.,ava
DY*ava,ava Test
7/22/2019 Durga Core Java Book
69/459
6&
)
If t#e instan+e variable de+lared as t#e final +o"p(lsory we s#o(ld perfor" initiali0ationw#et#er we are (sin! or not ot#erwise we will !et +o"pile ti"e error.
Example:
rogram 1:
+lass Test
int i9
=
!utput:
DY*ava,ava+ Test.,ava
DY*avarogram 2:
+lass Test
final int i9
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava1 variable i "i!#t not #ave been initiali0ed+lass Test
Rule:
Hor t#e final instan+e variables we s#o(ld perfor" initiali0ation before +onstr(+tor+o"pletion. T#at is t#e followin! are vario(s possible pla+es for t#is.
1; 7t t#e time of de$laration:Example:
+lass Test
final int i81)9=
!utput:
DY*ava,ava+ Test.,ava
DY*ava
2; Inside instan$e ,lo$+:Example:
+lass Test
7/22/2019 Durga Core Java Book
70/459
7)
final int i9
i81)9==
!utput:
DY*ava,ava+ Test.,ava
DY*ava
3; Inside $onstru$tor:Example:
+lass Test
final int i9TestB/
i81)9
==
!utput:
DY*ava,ava+ Test.,ava
DY*ava
If we are perfor"in! initiali0ation anyw#ere else we will !et +o"pile ti"e error.Example:+lass Test
final int i9
p(bli+ void "et#odneB/
i81)9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,avaTest.,ava5 +annot assi!n a val(e to final variable i
i81)9
Final stati$ varia,les:
If t#e val(e of a variable is not varied fro" ob,e+t to ob,e+t s(+# type of variables is notre+o""ended to de+lare as t#e instan+e variables. "e #ave to de$lare t#ose varia,les
at $lass level ,( using stati$ modifier9
Hor t#e stati+ variables it is not reV(ired to perfor" initiali0ation eEpli+itly ,v" willalways provide defa(lt val(es.
7/22/2019 Durga Core Java Book
71/459
71
Example:
+lass Test
stati+ int i9
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnBNval(e of i is NFi/9
==
!utput:
DY*ava,ava+ Test.,ava
DY*ava,ava Test
al(e of i is )
If t#e stati+ variable de+lare as final t#en +o"p(lsory we s#o(ld perfor" initiali0ationeEpli+itly w#et#er we are (sin! or not ot#erwise we will !et +o"pile ti"e error.
Example:
Rule:
Hor t#e final stati+ variables we s#o(ld perfor" initiali0ation before +lass loadin!+o"pletion ot#erwise we will !et +o"pile ti"e error. T#at is t#e followin! are possible
pla+es.
1; 7t t#e time of de$laration:Example:
+lass Test
final stati+ int i81)9
=
!utput:
DY*ava,ava+ Test.,ava
DY*ava
2; Inside stati$ ,lo$+:Example:
+lass Test
final stati+ int i9
7/22/2019 Durga Core Java Book
72/459
72
stati+
i81)9==
!utput:
'o"pile s(++essf(lly.
If we are perfor"in! initiali0ation anyw#ere else we will !et +o"pile ti"e error.Example:
+lass Test
final stati+ int i9
p(bli+ stati+ void "ainBCtrin! ar!s>/i81)9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava5 +annot assi!n a val(e to final variable i
i81)9
Final lo$al varia,les: To "eet te"porary reV(ire"ent of t#e pro!ra""er so"eti"e we +an de+lare t#e
variable inside a "et#od or blo+? or +onstr(+tor s(+# type of variables are +alled lo+al
variables.
Hor t#e lo+al variables ,v" wont provide any defa(lt val(e +o"p(lsory we s#o(ldperfor" initiali0ation eEpli+itly before (sin! t#at variable.
Example:
+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/int i9
Cyste".o(t.printlnBN#elloN/9
==
!utput:
DY*ava,ava+ Test.,ava
DY*ava,ava Test
Sello
Example:
7/22/2019 Durga Core Java Book
73/459
73
+lass Test
p(bli+ stati+ void "ainBCtrin! ar!s>/int i9
Cyste".o(t.printlnBi/9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava5 variable i "i!#t not #ave been initiali0ed
Cyste".o(t.printlnBi/9
/
final int i9
Cyste".o(t.printlnBN#elloN/9
==
!utput:DY*ava,ava+ Test.,ava
DY*ava,ava Test
#ello
/ote: 8#e onl( appli$a,le modifier for lo$al varia,les is final if %e are using an( ot#er
modifier %e %ill get $ompile time error9
Example:
!utput:
'o"pile ti"e error.
7/22/2019 Durga Core Java Book
74/459
74
DY*ava,ava+ Test.,ava
Test.,ava5 ille!al start of eEpression
private int E81)9Formal parameters:
T#e for"al para"eters of a "et#od are si"ply a++ess lo+al variables of t#at "et#od#en+e it is possible to de+lare for"al para"eters as final.
If we de+lare for"al para"eters as final t#en we +ant +#an!e its val(e wit#in t#e"et#od.
Example:
tati$ modifier: Ctati+ is t#e "odifier appli+able for "et#ods- variables and blo+?s. @e +ant de+lare a +lass wit# stati+ ,ut inner $lasses$an ,e de$laring as t#e stati$. In t#e +ase of instan+e variables for every ob,e+t a separate +opy will be +reated b(t in
t#e +ase of stati+ variables a sin!le +opy will be +reated at +lass level and s#ared by all
ob,e+ts of t#at +lass.
Example:
!utput:
7/22/2019 Durga Core Java Book
75/459
75
DY*ava,ava+ Test.,ava
DY*ava,ava Test
$$$.....2) Instan+e variables +an be a++essed only fro" instan$e area dire$tl( and %e $ant a$$ess
from stati$ area dire$tl(.
(t stati+ variables +an be a++essed fro" ,ot# instan$e and stati$ areas dire$tl(.1/ Int E81)92/ Ctati+ int E81)93/ \(bli+ void "et#odneB/
Cyste".o(t.printlnBE/9
=
4/ \(bli+ stati+ void "et#odneB/Cyste".o(t.printlnBE/9
=
"#i$# are t#e follo%ing de$larations are allo% %it#in t#e same $lass simultaneousl(&
a; 1 and 3Example:
+lass Test
int E81)9
p(bli+ void "et#odneB/
Cyste".o(t.printlnBE/9
==
!utput:
'o"pile s(++essf(lly.
,; 1 and 4Example:
+lass Test
int E81)9
p(bli+ stati+ void "et#odneB/
Cyste".o(t.printlnBE/9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava5 nonstati+ variable E +annot be referen+ed fro" a stati+ +onteEt
Cyste".o(t.printlnBE/9
$; 2 and 3Example:
+lass Test
7/22/2019 Durga Core Java Book
76/459
76
stati+ int E81)9
p(bli+ void "et#odneB/
Cyste".o(t.printlnBE/9
==
!utput:
'o"pile s(++essf(lly.
d; 2 and 4Example:
+lass Test
stati+ int E81)9p(bli+ stati+ void "et#odneB/
Cyste".o(t.printlnBE/9
==
!utput:
'o"pile s(++essf(lly.
e; 1 and 2Example:
+lass Test
int E81)9stati+ int E81)9
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
Test.,ava4 E is already defined in Test
stati+ int E81)9
f; 3 and 4Example:
+lass Testp(bli+ void "et#odneB/
Cyste".o(t.printlnBE/9
=
p(bli+ stati+ void "et#odneB/
Cyste".o(t.printlnBE/9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+ Test.,ava
7/22/2019 Durga Core Java Book
77/459
77
Test.,ava5 "et#odneB/ is already defined in Test
p(bli+ stati+ void "et#odneB/
verloadin! +on+ept is appli+able for stati+ "et#od in+l(din! "ain "et#od also.Example:
In#eritan+e +on+ept is appli+able for stati+ "et#ods in+l(din! "ainB/ "et#od #en+e w#ileeEe+(tin! +#ild +lass- if t#e +#ild doesnt +ontain "ainB/ "et#od t#en t#e parent +lass "ain"et#od will be eEe+(ted.
Example:
+lass \arent
p(bli+ stati+ void "ainBCtrin! ar!s>/
Cyste".o(t.printlnBNparent "ainB/ "et#od +alledN/9
=
=
+lass +#ild eEtends \arent
=
!utput:
Example:
!utput:
7/22/2019 Durga Core Java Book
78/459
7$
It see"s to be overridin! +on+ept is appli+able for stati+ "et#ods b(t it is not overridin! it is"et#od #idin!.
Hor stati+ "et#ods +o"p(lsory i"ple"entation s#o(ld be available w#ere as for abstra+t"et#ods i"ple"entation s#o(ld be available #en$e a,stra$t stati$ $om,ination is illegal for
met#ods9
/ative modifier:
:ative is a "odifier appli+able only for "et#ods b(t not for variables and +lasses. T#e "et#ods w#i+# are i"ple"ented in non ,ava are +alled native "et#ods or forei!n "et#ods.
8#e main o,je$tives of native +e(%ord are:
To i"prove perfor"an+e of t#e syste". To (se already eEistin! le!a+y non ,ava +ode.
8o use native +e(%ord:
seudo $ode:
Hor native "et#ods i"ple"entation is already available and we are not responsible toprovide i"ple"entation #en+e native "et#od de+laration s#o(ld +o"p(lsory ends wit#
se"i+olon.
\(bli+ native void "et#odneB/invalid \(bli+ native void "et#odneB/9valid
7/22/2019 Durga Core Java Book
79/459
7&
Hor native "et#ods i"ple"entation is already available w#ere as for abstra+t "et#odsi"ple"entation s#o(ld not be available +#ild +lass is responsible to provide t#at- #en+e
abstra+t native +o"bination is ille!al for "et#ods. @e +ant de+lare a native "et#od as stri+tfp be+a(se t#ere is no !(aranty w#et#er t#e
old lan!(a!e s(pports I
7/22/2019 Durga Core Java Book
80/459
7/22/2019 Durga Core Java Book
81/459
$1
b/ ariable na"in! +onfli+ts7/ %ar?er interfa+e$/ Adapter +lass&/ Interfa+e vs abstra+t +lass vs +on+rete +lass.1)/Differen+e between interfa+e and abstra+t +lassW11/'on+l(sions
'ef1:Any servi+e reV(ire"ent spe+ifi+ation Bsrs/ is +alled an interfa+e.
Example1: C(n people responsible to define *D' A\I and database vendor will provide
i"ple"entation for t#at.
'iagram:
Example2:C(n people define C
7/22/2019 Durga Core Java Book
82/459
$2
ummer( def:Any servi+e reV(ire"ent spe+ifi+ation BCRC/ or any +ontra+t between +lient and
servi+e provider or 1))` p(re abstra+t +lasses is +onsidered as an interfa+e.
'e$laration and implementation of an interfa$e::ote1 @#enever we are i"ple"entin! an interfa+e $ompulsor( for ever( met#od of t#at
interfa$e %e s#ould provide implementation ot#er%ise %e #ave to de$lare $lass as a,stra$tin
t#at +ase $#ild $lass is responsi,le to provide implementation for remaining met#ods.
:ote2 @#enever we are i"ple"entin! an interfa+e "et#od $ompulsor( it s#ould ,e de$lared
as pu,li$ ot#er%ise %e %ill get $ompile time error.
Example:
interfa+e Interf
void "et#odneB/9void "et#odTwoB/9
=
+lass C(bCervi+e\rovider eEtends Cervi+e\rovider
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ C(bCervi+e\rovider.,ava
C(bCervi+e\rovider.,ava1 C(bCervi+e\rovider is not abstra+t and does not override abstra+t
"et#od "et#odTwoB/ in Interf
+lass C(bCervi+e\rovider eEtends Cervi+e\rovider
Extends vs implements:
A +lass +an eEtend only one +lass at a ti"e.Example:
+lass ne
p(bli+ void "et#odneB/
=
=
+lass Two eEtends ne
=
A +lass +an i"ple"ents any no. f interfa+es at a ti"e.
7/22/2019 Durga Core Java Book
83/459
$3
Example:
interfa+e ne
p(bli+ void "et#odneB/9=
interfa+e Two
p(bli+ void "et#odTwoB/9
=
+lass T#ree i"ple"ents ne-Two
p(bli+ void "et#odneB/
=
p(bli+ void "et#odTwoB/
==
A +lass +an eEtend a +lass and +an i"ple"ent an interfa+e si"(ltaneo(sly.interfa+e ne
void "et#odneB/9
=
+lass Two
p(bli+ void "et#odTwoB/
==
+lass T#ree eEtends Two i"ple"ents ne
p(bli+ void "et#odneB/
=
=
An interfa+e +an eEtend any no. f interfa+es at a ti"e.Example:
interfa+e ne
void "et#odneB/9=
interfa+e Two
void "et#odTwoB/9
=
interfa+e T#ree eEtends ne-Two
=
1; "#i$# of t#e follo%ing is true&
7/22/2019 Durga Core Java Book
84/459
$4
1. A +lass +an eEtend any no. f +lasses at a ti"e.2. An interfa+e +an eEtend only one interfa+e at a ti"e.3. A +lass +an i"ple"ent only one interfa+e at a ti"e.4. A +lass +an eEtend a +lass and +an i"ple"ent an interfa+e b(t not bot#
si"(ltaneo(sly.
5. :one of t#e above.Ans 5
2/ 'onsider t#e eEpression D extends J for w#i+# of t#e possibility of P and t#iseEpression is tr(eW
1. ot# E and y s#o(ld be +lasses.2. ot# E and y s#o(ld be interfa+es.3. ot# E and y +an be +lasses or +an be interfa+es.4. :o restri+tion.
Ans 3
3; D extends JH K& P- - s#o(ld be interfa+es.
4; D extends J implements K& P- s#o(ld be +lasses. s#o(ld be interfa+e.
5; D implements JH K& P s#o(ld be +lass. - s#o(ld be interfa+es.
6; D implements J extend K&Example:
interfa+e ne
=
+lass Two
=
+lass T#ree i"ple"ents ne eEtends Two
=!utput:
'o"pile ti"e error.
DY*ava,ava+ T#ree.,ava
T#ree.,ava5 QQ eEpe+ted
+lass T#ree i"ple"ents ne eEtends Two
7/22/2019 Durga Core Java Book
85/459
$5
p(bli+ oid "et#odneB/9
abstra+t oid "et#odneB/9 EAual
p(bli+ abstra+t oid "et#odneB/9 As every interfa+e "et#od is always p(bli+ and abstra+t we +ant (se t#e followin!
"odifiers for interfa+e "et#ods.
rivateH prote$tedH finalH stati$H s(n$#roniedH nativeH stri$tfp9Inside interfa$e %#i$# met#od de$larations are valid&
1. p(bli+ void "et#odneB/=2. private void "et#odneB/93. p(bli+ final void "et#odneB/94. p(bli+ stati+ void "et#odneB/95. p(bli+ abstra+t void "et#odneB/9
Ans 5
Interfa$e varia,les:
An interfa+e +an +ontain variables to define reV(ire"ent level +onstants.
7/22/2019 Durga Core Java Book
86/459
$6
Transient olatile
Hor t#e interfa+e variables +o"p(lsory we s#o(ld perfor" initiali0ation at t#e ti"e ofde+laration only ot#erwise we will !et +o"pile ti"e error.
Example:
interfa+e Interf
int E9
=
!utput:
'o"pile ti"e error.
DY*ava,ava+ Interf.,avaInterf.,ava3 8 eEpe+ted
int E9
"#i$# of t#e follo%ing de$larations are valid inside interfa$e&
1. int E92. private int E81)93. p(bli+ volatile int E81)94. p(bli+ transient int E81)95. p(bli+ stati+ final int E81)9
Ans 5 Interfa+e variables +an be a++ess fro" i"ple"entation +lass b(t +annot be "odified.
Example:
interfa+e Interf
int E81)9
=
Example 1:
7/22/2019 Durga Core Java Book
87/459
$7
Example 2:
+lass Test i"ple"ents Interf
p(bli+ stati+ void "ainBCtrin! ar!s>/
int E82)9
JJ#ere we de+larin! t#e variable E.
Cyste".o(t.printlnBE/9
=
=
!utput:
DY*ava,ava+ Test.,ava
DY*ava,ava Test2)
Interfa$e naming $onfli$ts:
=et#od naming $onfli$ts:
*ase 1:
If two interfa+es +ontain a "et#od wit# sa"e si!nat(re and sa"e ret(rn type in t#ei"ple"entation +lass only one "et#od i"ple"entation is eno(!#.
Example 1:
interfa+e Left
p(bli+ void "et#odneB/9
=
Example 2:
interfa+e Ri!#t
p(bli+ void "et#odneB/9
=
Example 3:
+lass Test i"ple"ents Left-Ri!#t
p(bli+ void "et#odneB/
==
!utput:
DY*ava,ava+ Left.,ava
DY*ava,ava+ Ri!#t.,ava
DY*ava,ava+ Test.,ava
7/22/2019 Durga Core Java Book
88/459
$$
*ase 2:
if two interfa+es +ontain a "et#od wit# sa"e na"e b(t different ar!("ents in t#ei"ple"entation +lass we #ave to provide i"ple"entation for bot# "et#ods and t#ese"et#ods a+ts as a overloaded "et#ods
Example 1:
interfa+e Left
p(bli+ void "et#odneB/9
=
Example 2:
interfa+e Ri!#t
p(bli+ void "et#odneBint i/9
=
Example 3:
+lass Test i"ple"ents Left-Ri!#t
p(bli+ void "et#odneB/
=
p(bli+ void "et#odneBint i/
==
!utput:
DY*ava,ava+ Left.,ava
DY*ava,ava+ Ri!#t.,ava
DY*ava,ava+ Test.,ava
*ase 3:
If two interfa+es +ontain a "et#od wit# sa"e si!nat(re b(t different ret(rn types t#enit is not possible to i"ple"ent bot# interfa+es si"(ltaneo(sly.
Example 1:
interfa+e Left
p(bli+ void "et#odneB/9
=
Example 2:
interfa+e Ri!#t
7/22/2019 Durga Core Java Book
89/459
$&
p(bli+ int "et#odneBint i/9
=
@e +ant write any ,ava +lass t#at i"ple"ents bot# interfa+es si"(ltaneo(sly.Is a ,ava +lass +an i"ple"ent any no. f interfa+es si"(ltaneo(slyW
es- eE+ept if two interfa+es +ontains a "et#od wit# sa"e si!nat(re b(t different ret(rntypes.
-aria,le naming $onfli$ts:
Two interfa+es +an +ontain a variable wit# t#e sa"e na"e and t#ere "ay be a +#an+evariable na"in! +onfli+ts b(t we +an resolve variable na"in! +onfli+ts by (sin! interfa+e
na"es.
Example 1:
interfa+e Left
int E8$$$9
=
Example 2:
interfa+e Ri!#t
int E8&&&9
=
Example 3:+lass Test i"ple"ents Left-Ri!#t
p(bli+ stati+ void "ainBCtrin! ar!s>/
JJCyste".o(t.printlnBE/9
Cyste".o(t.printlnBLeft.E/9
Cyste".o(t.printlnBRi!#t.E/9
=
=
!utput:DY*ava,ava+ Left.,ava
DY*ava,ava+ Ri!#t.,ava
DY*ava,ava+ Test.,ava
DY*ava,ava Test
$$$
&&&
7/22/2019 Durga Core Java Book
90/459
&)
=ar+er interfa$e: if an interfa+e doesnt +ontain any "et#ods and by i"ple"entin! t#at
interfa+e if o(r ob,e+t !ets so"e ability s(+# type of interfa+es are +alled %ar?er interfa+e Bor/
Ta! interfa+e Bor/ Ability interfa+e.Example:
Cerili0able
+loneable
Rando"A++ess T#ese are "ar?ed for so"e ability
Cin!leT#read%odel
.
.
.
.Example 1:y i"ple"entin! Cerili0able interfa+e we +an send t#at ob,e+t a+ross t#e networ?
and we +an save state of an ob,e+t into a file.
Example 2:y i"ple"entin! Cin!leT#read%odel interfa+e Cervlet +an pro+ess only one +lient
reV(est at a ti"e so t#at we +an !et KT#read Cafety.
Example 3:y i"ple"entin! 'loneable interfa+e o(r ob,e+t is in a position to provide eEa+tly
d(pli+ate +loned ob,e+t.
"it#out #aving an( met#ods in mar+er interfa$e #o% o,je$ts %ill get a,ilit(&
Internally *% will provide reV(ired ability."#( 0-= is providing t#e reAuired a,ilit(&
To red(+e +o"pleEity of t#e pro!ra""in!.Is it possi,le to $reate our o%n mar+er interfa$e&
es- b(t +(sto"i0ation of *% is reV(ired.7dapter $lass:
Adapter +lass is a si"ple ,ava +lass t#at i"ple"ents an interfa+e only wit# e"ptyi"ple"entation for every "et#od.
If we i"ple"ent an interfa+e dire+tly for ea+# and every "et#od +o"p(lsory we s#o(ldprovide i"ple"entation w#et#er it is reV(ired or not. T#is approa+# in+reases len!t# of
t#e +ode and red(+es readability.Example 1:
interfa+e P
void "1B/9
void "2B/9
void "3B/9
void "4B/9
JJ.
JJ.
7/22/2019 Durga Core Java Book
91/459
&1
JJ.
JJ.
void "5B/9=
Example 2:
+lass Test i"ple"ents P
p(bli+ void "3B/
Cyste".o(t.printlnBN"3B/ "et#od is +alledN/9
=
p(bli+ void "1B/=
p(bli+ void "2B/=
p(bli+ void "4B/=p(bli+ void "5B/=
=
@e +an resolve t#is proble" by (sin! adapter +lass. Instead of i"ple"entin! an interfa+e if we +an eEtend adapter +lass we #ave to provide
i"ple"entation only for reV(ired "et#ods b(t not for all "et#ods of t#at interfa+e.
T#is approa+# de$reases lengt# of t#e $odeand i"proves readability.Example 1:
abstra+t +lass AdapterP i"ple"ents P
p(bli+ void "1B/=p(bli+ void "2B/=
p(bli+ void "3B/=
p(bli+ void "4B/=
JJ.
JJ.
JJ.
p(bli+ void "1)))B/=
=
Example 2:p(bli+ +lass Test eEtend AdapterP
p(bli+ void "3B/
==
Example:
7/22/2019 Durga Core Java Book
92/459
&2
^eneri+ Cervlet si"ply a+ts as an adapter +lass for Cervlet interfa+e."#at is t#e differen$e ,et%een interfa$eH a,stra$t $lass and $on$rete $lass&
"#en %e s#ould go for interfa$eH a,stra$t $lass and $on$rete $lass&
If we dont ?now anyt#in! abo(t i"ple"entation ,(st we #ave reV(ire"entspe+ifi+ation t#en we s#o(ld !o for interfa+e.
If we are tal?in! abo(t i"ple"entation b(t not +o"pletely Bpartial i"ple"entation/t#en we s#o(ld !o for abstra+t +lass.
If we are tal?in! abo(t i"ple"entation +o"pletely and ready to provide servi+e t#en wes#o(ld !o for +on+rete +lass.
Example:
"#at is t#e 'ifferen$e ,et%een interfa$e and a,stra$t $lass&
interfa+e Abstra+t +lass1/ If we dont ?now anyt#in! abo(t
i"ple"entation ,(st we #ave
reV(ire"ent spe+ifi+ation t#en we
s#o(ld !o for interfa+e.
1/ If we are tal?in! abo(t i"ple"entationb(t not +o"pletely Bpartial
i"ple"entation/ t#en we s#o(ld !o for
abstra+t +lass.
2/
7/22/2019 Durga Core Java Book
93/459
&3
wit# t#e "odifiers private- prote$ted-
final- stati$- s(n$#ronied- native-
stri$tfp.
+lass "et#od "odifiers.
4/
7/22/2019 Durga Core Java Book
94/459
&4
Ever( met#od present inside interfa$e is a,stra$t ,ut in a,stra$t $lass also %e $an ta+e onl(
a,stra$t met#ods t#en %#at is t#e need of interfa$e $on$ept&
@e +an repla+e interfa+e +on+ept wit# abstra+t +lass. (t it is not a !ood pro!ra""in!pra+ti+e. @e are "is(sin! t#e roll of abstra+t +lass.
!perator and assignments1/ In+re"ent and de+re"ent operator2/ Arit#"eti+ operators3/ Ctrin! +on+atenation operator4/ Relational operators5/
7/22/2019 Durga Core Java Book
95/459
&5
&/ Type +ast operator1)/Assi!n"ent operator11/'onditional operator12/new operator13/> operator14/*ava operator pre+eden+e15/
7/22/2019 Durga Core Java Book
96/459
&6
:estin! of in+re"ent or de+re"ent operators is not allowed.Example:
@e +ant apply in+re"ent or de+re"ent operator for final variables.Example:
@e +an apply in+re"ent or de+re"ent operator for any pri"itive type eE+ept oolean.Example:
7/22/2019 Durga Core Java Book
97/459
&7
If we apply any arit#"eti+ operator between two variables Ka and Kb t#e res(lt type isalways.
maxintHt(peof aHt(peof ,;
Example 1:
Example 2:
In t#e +ase of in+re"ent or de+re"ent operator t#e reV(ired type+astin! will beperfor"ed a(to"ati+ally by t#e +o"piler.
Example:
byte b81)9
bFF9
Cyste".o(t.printlnBb/9JJ11
7rit#meti$ operators:BF--U- J- `/
7/22/2019 Durga Core Java Book
98/459
&$
If we apply any arit#"eti+ operation between two variables Ka and Kb. T#e res(lt typeis always.
maxintH t(pe of aH t(pe ,;
Example:
Cyste".o(t.printlnBQaQF1/9JJ&$
Cyste".o(t.printlnBQaQFQbQ/9JJ1&5
Cyste".o(t.printlnB1)F).5/9JJ1).5Cyste".o(t.printlnBQaQF3.5/9JJ1)).5
Infinit(:
In t#e +ase of inte!ral arit#"eti+ Bbyte- s#ort- int- lon!/ t#ere is no way to representinfinity.
Sen+e if infinity is t#e res(lt t#en we will !et Arit#"eti+
7/22/2019 Durga Core Java Book
99/459
&&
Sen+e if t#e res(lt is (ndefined- we wont !et any r(nti"e eE+eption in floatin! pointarit#"eti+.
Example:Cyste".o(t.printlnB).)J)/9JJ:a:
Cyste".o(t.printlnB).)J)/9JJ:a:
Cyste".o(t.printlnB)J).)/9JJ:a:
Hor any xval(e in+l(din! /a/t#e followin! eEpressions ret(rn false.Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
int E81)9
Cyste".o(t.printlnBEHloat.:a:/9JJfalse
Cyste".o(t.printlnBEZHloat.:a:/9JJfalse
Cyste".o(t.printlnBE8Hloat.:a:/9JJfalse
Cyste".o(t.printlnBEZ8Hloat.:a:/9JJfalse
Cyste".o(t.printlnBE88Hloat.:a:/9JJfalse
=
=
Hor any xval(e in+l(din! /a/t#e followin! eEpression ret(rn tr(e.Example:
+lass Test
p(bli+ stati+ void "ainBCtrin!> ar!s/
int E81)9
Cyste".o(t.printlnBE8Hloat.:a:/9JJtr(e
Cyste".o(t.printlnBHloat.:a:8Hloat.:a:/9JJtr(e
==
ummar(:
7/22/2019 Durga Core Java Book
100/459
1))
tring $on$atenation operator:
T#e only operator w#i+# is overloaded in ,ava is K? operator. Co"eti"e it a+ts asarit#"eti+ addition operator and so"e ti"e +on+atenation operator.
If at least one ar!("ent is strin! type t#en K? operator a+ts as +on+atenation and ifbot# ar!("ents are n("ber type t#en it a+ts as arit#"eti+ addition operator.
Example 1:
Ctrin! a8Nb#as?arN9
int b81)-+82)-d83)9
Cyste".o(t.printlnBaFbF+Fd/9JJb#as?ar1)2)3)
Cyste".o(t.printlnBbF+FdFa/9JJ6)b#as?ar
Cyste".o(t.printlnBbF+FaFd/9JJ3)b#as?ar3)
Cyste".o(t.printlnBbFaF+Fd/9JJ1)b#as?ar2)3)Example 2:
Example 3:
Relational operator:H MH NH NM;
@e +an apply relational operators for every pri"itive type eE+ept boolean.Example:
@e +ant apply relational operators for t#e ob,e+t types.Example:
7/22/2019 Durga Core Java Book
101/459
1)1
@e +ant perfor" nestin! of relational operators.Example:
EAualit( operator :B88-8/
@e +an apply eV(ality operators for every pri"itive type in+l(din! boolean type also.Example:
Cyste".o(t.printlnB1)881).)/9JJtr(e
Cyste".o(t.printlnBQaQ88&7.)/9JJtr(e
Cyste".o(t.printlnBtr(e88tr(e/9JJtr(e
Cyste".o(t.printlnBQaQ8QbQ/9JJtr(e @e +an apply eV(ality operator even for ob,e+t referen+e also. In t#e +ase of ob,e+t referen+es 88 Bdo(ble eV(al operator/ is always "eant for
referen+e +o"parison only Baddress +o"parison/.
i.e. r188r2 ret(rn tr(e if and only if bot# r1 and r2 point to t#e sa"e ob,e+t.Example:
T#read t18new T#readB/9
T#read t28new T#readB/9
T#read t38t19Cyste".o(t.printlnBt188t2/9JJfalse
Cyste".o(t.printlnBt188t3/9JJtr(e
'iagram:
To (se eV(ality operator +o"p(lsory t#ere s#o(ld be so"e relations#ip betweenar!("ent typeBeit#er parent+#ild Bor/+#ildparent Bor/ sa"e type/ot#erwise we will !et
+o"pile ti"e error sayin! in+o"parable types.Example:
b,e+t o8new b,e+tB/9
Ctrin! s8new Ctrin!BNb#as?arN/9
Ctrin!(ffer sb8new Ctrin!(fferB/9
Cyste".o(t.printlnBo88s/9JJfalse
Cyste".o(t.printlnBo88sb/9JJfalse
7/22/2019 Durga Core Java Book
102/459
1)2
'iagram:
Hor any ob,e+t referen+e of- r88n(ll is always false. (t n(ll88n(ll is tr(e.MM -s 9eAuals;:
88operator is always "eant for referen+e +o"parison w#ereas .eV(alsB/ "et#od "ostly"eant for +ontent +o"parison.
Example:
Ctrin! s18new Ctrin!BNb#as?arN/9
Ctrin! s28new Ctrin!BNb#as?arN/9
Cyste".o(t.printlnBs188s2/9JJfalse
Cyste".o(t.printlnBs1.eV(alsBs2//9JJtr(e
'iagram:
Instan$eof operator: @e +an (se t#is operator to +#e+? w#et#er t#e !iven ob,e+t is of parti+(lar type Bor/ not.
(ntax:
Example:
T#read t8new T#readB/9
Cyste".o(t.printlnBt instan+eof T#read/9JJtr(e
Cyste".o(t.printlnBt instan+eof b,e+t/9JJtr(e
Cyste".o(t.printlnBt instan+eof R(nnable/9JJtr(e
'iagram:
/ote:
7/22/2019 Durga Core Java Book
103/459
1)3
To (se Kinstan+eof operator +o"p(lsory t#ere s#o(ld be so"e relations#ip betweenar!("ent types Beit#er parent+#ild Bor/ +#ildparent Bor/ sa"e type/ ot#erwise we will
!et +o"pile ti"e error sayin! Kin+onvertible types.Example:
@#enever we are +o"parin! parent ob,e+t is +#ild type or not by (sin! instan+eofoperator t#en we will !et Kfalse as o(tp(t.
Example:b,e+t o8new b,e+tB/9
Cyste".o(t.printlnBo instan+eof Ctrin!/9JJfalse
Hor any +lass or interfa+e Enull instan$eof x t#e res(lt is always Kfalse.
Example:
Cyste".o(t.printlnBn(ll instan+eof Ctrin!/9JJfalse
it%ise operators:
O 7/';: If bot# ar!("ents are tr(e t#en res(lt is tr(e.
P !R;:if at least one ar!("ent is tr(e. T#en t#e res(lt is tr(e.Q D
Cyste".o(t.printlnBtr(eOfalse/9JJtr(e
Cyste".o(t.printlnBtr(efalse/9JJtr(e
@e +an apply bitwise operators even for inte!ral types also.Example:
it%ise $omplement ; tilde s(m,ol; operator:
@e +an apply t#is operator only for inte!ral types b(t not for boolean types.Example 1:
Example 2:
7/22/2019 Durga Core Java Book
104/459
1)4
Cyste".o(t.printlnB4/9JJ5
'iagram:
oolean $omplement S; operator:
T#is operator is appli+able only for boolean types b(t not for inte!ral types.Example:
Cyste".o(t.printlnBtr(e/9JJfalse
Cyste".o(t.printlnBfalse/9JJtr(e
ummar(:
c
O Appli+able for bot# boolean and inte!ral types.
Appli+able for inte!ral types only.
Appli+able for boolean types only.
#ort $ir$uit OOH PP; operators:
T#ese operators are eEa+tly sa"e as nor"al bitwise operators c- O eE+ept t#e followin!differen+es.
OHP OOHPP
1/ ot# ar!("ents s#o(ld be eval(atedalways.
1/ Ce+ond ar!("ent eval(ation isoptional.
2/ Relatively perfor"an+e is low. 2/ Relatively perfor"an+e is #i!#.3/ Appli+able for bot# inte!ral and
boolean types.
3/ Appli+able only for boolean types b(tnot for inte!ral types.
1; r1OOr2 r2 will be eval(ated if and only if r1 is tr(e.2; r1PPr2 r2 will be eval(ated if and only if r1 is false.
Example 1:
+lass peratorsDe"o
7/22/2019 Durga Core Java Book
105/459
1)5
p(bli+ stati+ void "ainBCtrin!> ar!s/
int E81)- y8159
ifBFFE1)Boperator/FFyZ15/
FFE9
=
else
FFy9
=Cyste".o(t.printlnBEFNNFy/9
=
=
!utput:
operator x (
c 11 17
O 12 16
cc 11 17
OO 12 15
Example 2:
+lass peratorsDe"o
p(bli+ stati+ void "ainBCtrin!> ar!s/
int E81)9
ifBFFEZ1)Boperator/EJ)1)/
Cyste".o(t.printlnBN#elloN/9
=
else
Cyste".o(t.printlnBN#iN/9
=
=
=
OO !utput:Si
O !utput:R.
7/22/2019 Durga Core Java Book
106/459
1)6
,ava.lan!.Arit#"eti+
7/22/2019 Durga Core Java Book
107/459
1)7
Example 2:
int E813)9
byte b8Bbyte/E9
Cyste".o(t.printlnBb/9JJ126
7nal(sis:
@#enever we are assi!nin! bi!!er data type val(e to t#e s"aller data type variable byeEpli+it type +astin! t#e "ost si!nifi+ant bitB%C/will be lost.
Example:
int E815)9
s#ort s8Bs#ort/E9
Cyste".o(t.printlnBs/9JJ15)
byte b8Bbyte/E9
Cyste".o(t.printlnBb/9JJ1)6
@#enever we are assi!nin! floatin! point data types to t#e inte!er data type by eEpli+ittype +astin! t#e di!its after t#e de+i"al point will be loosed.
Example:
7ssignment operators:
T#ey are t#ree types of assi!n"ent operators.imple assignment:
7/22/2019 Durga Core Java Book
108/459
1)$
Example: int E81)9
*#ained assignment:
Example:int a-b-+-d9
a8b8+8d82)9
Cyste".o(t.printlnBaFNNFbFNNF+FNNFd/9JJ2)2)2)2)
@e +ant perfor" +#ained assi!n"ent dire+tly at t#e ti"e of de+laration.Example 1:
Example 2:
int a-b-+-d9
a8b8+8d83)9
*ompound assignment:
Co"eti"es we +an "iE assi!n"ent operator wit# so"e ot#er operator to for"+o"po(nd assi!n"ent operator.
T#e followin! is t#e list of all possible +o"po(nd assi!n"ent operators in ,ava.Example 1:
In t#e +ase of +o"po(nd assi!n"ent operator t#e reV(ired type +astin! will beperfor"ed a(to"ati+ally by t#e +o"piler si"ilar to in+re"ent and de+re"ent
operators.
Example 2:
Example 3:
7/22/2019 Durga Core Java Book
109/459
1)&
*onditional operator:
T#e only ternary operator w#i+# is available in ,ava is +onditional operator.Example 1:
int E8B1)2)/W3)4)9
Cyste".o(t.printlnBE/9JJ4)
@e +an perfor" nestin! of +onditional operator also.Example 2:
Example 5:
Example 6:
final int a81)-b82)9
byte +18Bab/W3)4)9
byte +28BaZb/W3)4)9
Cyste".o(t.printlnB+1/9JJ4)
Cyste".o(t.printlnB+2/9JJ3)ne% operator:
@e +an (se Knew operator to +reate an ob,e+t. T#ere is no Kdelete operator in ,ava be+a(se destr(+tion of ob,e+ts is t#e responsibility
of !arba!e +olle+tor.
TU operator:
@e +an (se t#is operator to de+lare and +onstr(+t arrays.0ava operator pre$eden$e:
.nar( operators:
7/22/2019 Durga Core Java Book
110/459
11)
>- EFF- P-
FFE-E- --
new- Ztype.7rit#meti$ operators:
U-J-`-
F-.
#ift operators:
--ZZ.
*ompression operators:
Z- Z8--8- instan+eof.
EAualit( operators:
88-8it%ise operators:
c--O.
#ort $ir$uit operators:
cc-OO.
*onditional operator:
BW/
7ssignment operators:
F8-8-U8-J8-`8.
Evaluation order of java operands: T#ere is no pre+eden+e for operands before applyin! any operator all operands will be
eval(ated fro" left to ri!#t.
Example:
+lass peratorsDe"o
p(bli+ stati+ void "ainBCtrin!> ar!s/
Cyste".o(t.printlnB"1B1/F"1B2/U"1B3/J"1B4/U"1B5/F"1B6//9
=p(bli+ stati+ int "1Bint i/
Cyste".o(t.printlnBi/9
ret(rn i9
=
=
7/22/2019 Durga Core Java Book
111/459
111
Example 1:
Example 2:int i819
iF8FFi F iFF F FFi F iFF9
Cyste".o(t.printlnBi/9JJ13Analysis
i8iF FFi F iFF F FFi F iFF9
i81F2F2F4F49
i8139
7/22/2019 Durga Core Java Book
112/459
112
Flo% *ontrol Hlow +ontrol des+ribes t#e order in w#i+# all t#e state"ents will be eEe+(ted at r(n
ti"e.
'iagram:
ele$tion statements:
19 if/
int E8)9
ifBE/
Cyste".o(t.printlnBN#elloN/9
=else
Cyste".o(t.printlnBN#iN/9
===
!.8.8:
'o"pile ti"e error
DY*ava,ava+
7/22/2019 Durga Core Java Book
113/459
113
p(bli+ stati+ void "ainBCtrin! ar!s>/
int E81)9
ifBE82)/
Cyste".o(t.printlnBN#elloN/9
=else
Cyste".o(t.printlnBN#iN/9
===
!.8.8:
'o"pile ti"e error
DY*ava,ava+
7/22/2019 Durga Core Java Book
114/459
114
Cyste".o(t.printlnBN#elloN/9
=else
Cyste".o(t.printlnBN#iN/9
===
!.8.8:
Si
ot# elseand $url( ,ra$esare optional. @it#o(t +(rly bra+es we +an ta?e only one state"ent (nder if- b(t it s#o(ld not be
de+larative state"ent.
ED7=LE 6:
p(bli+ +lass /
ifBtr(e/Cyste".o(t.printlnBN#elloN/9
==
!.8.8:
Sello
ED7=LE :
p(bli+ +lass /
ifBtr(e/9
==
!.8.8::o o(tp(t
ED7=LE B:
p(bli+ +lass /
ifBtr(e/
int E81)9
==
!.8.8:
'o"pile ti"e error
DY*ava,ava+ /
ifBtr(e/
int E81)9
===
7/22/2019 Durga Core Java Book
115/459
115
!.8.8:
DY*ava,ava+ /
swit+#BE/
Cyste".o(t.printlnBN#elloN/9
===
7/22/2019 Durga Core Java Book
116/459
116
!.8.8:
'o"pile ti"e error.
DY*ava,ava+
7/22/2019 Durga Core Java Book
117/459
117
final int y82)9
swit+#BE/
+ase 1)
Cyste".o(t.printlnBN1)N/9
+ase y
Cyste".o(t.printlnBN2)N/9
===
!.8.8:
1)
2)
Cwit+# ar!("ent and +ase label +an be eEpressions also- ,ut $ase s#ould ,e $onstantexpression9
ED7=LE 4:p(bli+ +lass /
int E81)9
swit+#BEF1/
+ase 1)
+ase 1)F2)
+ase 1)F2)F3)
===
!.8.8::o o(tp(t.
7/22/2019 Durga Core Java Book
118/459
11$
reV(ired byte
+ase 1)))
D(pli+ate +ase labels are not allowed.ED7=LE 6:
p(bli+ +lass /
int E81)9
swit+#BE/
+ase &7
Cyste".o(t.printlnBN&7N/9
+ase &&
Cyste".o(t.printlnBN&&N/9
+ase QaQCyste".o(t.printlnBN1))N/9===
!.8.8:
'o"pile ti"e error.
DY*ava,ava+
7/22/2019 Durga Core Java Book
119/459
11&
Cyste".o(t.printlnBN)N/9
+ase 1
Cyste".o(t.printlnBN1N/9
brea?9
+ase 2
Cyste".o(t.printlnBN2N/9
defa(lt
Cyste".o(t.printlnBNdefa(ltN/9
===
!.8.8:
DM xM1 xM2 xM3
) 1 2 defa(lt
1 defa(lt
'EF7.L8 *7E: @it#in t#e swit+# we +an ta?e t#e defaultanyw#ere- b(t at "ost on+e it is +onvention
to ta?e defa(lt as last +ase.
ED7=LE B:
p(bli+ +lass /
int E8)9
swit+#BE/
defa(lt
Cyste".o(t.printlnBNdefa(ltN/9+ase )
Cyste".o(t.printlnBN)N/9
brea?9
+ase 1
Cyste".o(t.printlnBN1N/9
+ase 2
Cyste".o(t.printlnBN2N/9
===
!.8.8:
DM xM1 xM2 xM3
) 1 2 defa(lt
2 )
I8ER78I-E 878E=E/8:
"#ile loopif we dont ?now t#e no of iterations in advan+e t#en best loop is w#ile loopED7=LE 1:
w#ileBrs.neEtB//
=
7/22/2019 Durga Core Java Book
120/459
12)
ED7=LE 2:
w#ileBe.#as%oreel/
w#ileBtr(e/
Cyste".o(t.printlnBN#elloN/9
==
!.8.8:
Sello Binfinite ti"es/.
7/22/2019 Durga Core Java Book
121/459
121
ED7=LE 3:
p(bli+ +lass /w#ileBtr(e/9
==
!.8.8:
:o o(tp(t.
ED7=LE 4:
p(bli+ +lass /
w#ileBtr(e/
int E81)9==
!.8.8:
'o"pile ti"e error.
DY*ava,ava+ /
w#ileBtr(e/
int E81)9
===
!.8.8:
:o o(tp(t.
;nrea+#able state"ent in w#ileED7=LE 6:
p(bli+ +lass /
w#ileBtr(e/
Cyste".o(t.printlnBN#elloN/9
=
Cyste".o(t.printlnBN#iN/9
7/22/2019 Durga Core Java Book
122/459
122
==
!.8.8:
'o"pile ti"e error.DY*ava,ava+ /
w#ileBfalse/
Cyste".o(t.printlnBN#elloN/9=
Cyste".o(t.printlnBN#iN/9
==
!.8.8:
DY*ava,ava+ /
final int a81)-b82)9
w#ileBaZb/
Cyste".o(t.printlnBN#elloN/9
7/22/2019 Durga Core Java Book
123/459
123
=
Cyste".o(t.printlnBN#iN/9
==!.8.8:
'o"pile ti"e error.
DY*ava,ava+ /
final int a81)9w#ileBaZ2)/
Cyste".o(t.printlnBN#elloN/9
=
Cyste".o(t.printlnBN#iN/9
==
!.8.8:
DY*ava,ava+
7/22/2019 Durga Core Java Book
124/459
124
!.8.8:
Sello Binfinite ti"es/.
'o
7/22/2019 Durga Core Java Book
125/459
125
!utput:
DY*ava,ava+ /do w#ileBtr(e/
Cyste".o(t.printlnBN#elloN/9
w#ileBtr(e/9
==
!utput:
Sello Binfinite ti"es/.
Rearrange t#e a,ove example:
p(bli+ +lass /do
w#ileBtr(e/
Cyste".o(t.printlnBN#elloN/9
w#ileBtr(e/9
==
!utput:
Sello Binfinite ti"es/.
7/22/2019 Durga Core Java Book
126/459
126
Example 6:
p(bli+ +lass /do
w#ileBtr(e/9
==
!utput:
'o"pile ti"e error.
DY*ava,ava+
7/22/2019 Durga Core Java Book
127/459
127
==
!utput:
SelloSi
Example V:
p(bli+ +lass /
int a81)-b82)9
do
Cyste".o(t.printlnBN#elloN/9
=w#ileBaZb/9
Cyste".o(t.printlnBN#iN/9
==
!utput:
Sello Binfinite ti"es/.
Example 1:
p(bli+ +lass /
int a81)-b82)9do