10java instanceof-WPS Office
10java instanceof-WPS Office
ainst
anceofOper
ator
I
nthi
stut
ori
al,
youwi
l
llear
naboutJav
ainst
anceofoper
atori
ndet
ail
wit
hthehel
pofexampl
es.
Thei
nstanceofoper
atori
nJav
aisusedt
ocheckwhet
heranobj
ecti
sani
nst
anceofapar
ti
cul
ar
cl
assornot.
I
tssy
ntaxi
s
obj
ect
Namei
nst
anceOfcl
assName;
Here,
ifobj
ect
Namei
sani
nst
anceofcl
assName,
theoper
atorr
etur
nst
rue.Ot
her
wise,
itr
etur
ns
f
alse.
Exampl
e:Jav
ainst
anceof
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
eav
ari
abl
eofst
ri
ngt
ype
St
ri
ngname="
Progr
ami
z";
/
/checksi
fnamei
sinst
anceofSt
ri
ng
bool
eanr
esul
t1=namei
nst
anceofSt
ri
ng;
Sy
stem.
out
.pr
int
ln(
"namei
sani
nst
anceofSt
ri
ng:
"+r
esul
t1)
;
/
/cr
eat
eanobj
ectofMai
n
Mai
nobj
=newMai
n()
;
/
/checksi
fobj
isani
nst
anceofMai
n
bool
eanr
esul
t2=obj
inst
anceofMai
n;
Sy
stem.
out
.pr
int
ln(
"obj
isani
nst
anceofMai
n:"+r
esul
t2)
;
RunCode
Out
put
namei
sani
nst
anceofSt
ri
ng:
true
obj
isani
nst
anceofMai
n:t
rue
I
ntheabov
eexampl
e,wehav
ecr
eat
edav
ari
abl
enameoft
heSt
ri
ngt
ypeandanobj
ectobj
of
t
heMaincl
ass.
Her
e,wehav
eusedtheinstanceofoper
atortocheckwhet
hernameandobjar
einst
ancesoft
he
St
ri
ngandMaincl
assrespecti
vely
.And,theoperat
orr
etur
nstr
ueinbot
hcases.
Not
e:I
nJav
a,St
ri
ngi
sacl
assr
athert
hanapr
imi
ti
vedat
aty
pe.Tol
ear
nmor
e,v
isi
tJav
aSt
ri
ng.
Jav
ainst
anceofdur
ingI
nher
it
ance
Wecanuset hei
nstanceofoper
atort
ochecki
fobj
ect
soft
hesubcl
assi
sal
soani
nst
anceoft
he
super
class.Forexample,
/
/Jav
aPr
ogr
am t
ochecki
fanobj
ectoft
hesubcl
ass
/
/isal
soani
nst
anceoft
hesuper
class
/
/super
class
cl
assAni
mal
{
/
/subcl
ass
cl
assDogext
endsAni
mal
{
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
eanobj
ectoft
hesubcl
ass
Dogd1=newDog(
);
/
/checksi
fd1i
sani
nst
anceoft
hesubcl
ass
Sy
stem.
out
.pr
int
ln(
d1i
nst
anceofDog)
; /
/pr
int
str
ue
/
/checksi
fd1i
sani
nst
anceoft
hesuper
class
Sy
stem.
out
.pr
int
ln(
d1i
nst
anceofAni
mal
); /
/pr
int
str
ue
RunCode
I
ntheabov
eexampl
e,wehav ecr
eatedasubclassDogt
hati
nher
it
sfr
om t
hesuper
classAni
mal
.
Wehavecr
eat
edanobjectd1oftheDogclass.
I
nsi
det
hepr
intst
atement
,not
icet
heexpr
essi
on,
d1i
nst
anceofAni
mal
Here,
wear eusi
ngthei
nst
anceofoper
atort
ocheckwhet
herd1i
sal
soani
nst
anceoft
he
super
classAni
mal.
Jav
ainst
anceofi
nInt
erf
ace
Thei
nstanceofoper
atori
salsousedt
ocheckwhet heranobj
ectofacl
assi
sal
soani
nst
ance
oft
heinter
faceimpl
ementedbythecl
ass.Forexample,
/
/Jav
apr
ogr
am t
ochecki
fanobj
ectofacl
assi
sal
so
/
/ani
nst
anceoft
hei
nter
facei
mpl
ement
edbyt
hecl
ass
i
nter
faceAni
mal
{
cl
assDogi
mpl
ement
sAni
mal
{
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
eanobj
ectoft
heDogcl
ass
Dogd1=newDog(
);
/
/checksi
ftheobj
ectofDog
/
/isal
soani
nst
anceofAni
mal
Sy
stem.
out
.pr
int
ln(
d1i
nst
anceofAni
mal
);/
/ret
urnst
rue
RunCode
I
ntheabov
eexample,
theDogcl
assi
mpl
ement
stheAni
mal
int
erf
ace.I
nsi
det
hepr
intst
atement
,
not
icet
heexpr
essi
on,
d1i
nst
anceofAni
mal
Her
e,d1isani
nstanceofDogcl
ass.Thei
nst
anceofoper
atorchecksi
fd1i
sal
soani
nst
anceof
t
heint
erf
aceAnimal.
Note:
InJav
a, all
thecl
assesar
einheri
tedf
rom t
heObj
ectcl
ass.So,
inst
ancesofal
lthecl
asses
ar
ealsoaninstanceoft
heObjectcl
ass.
I
nthepr
evi
ousexampl
e,i
fwecheck,
d1i
nst
anceofObj
ect
Ther
esul
twi
l
lbet
rue.