0% found this document useful (0 votes)
31 views14 pages

Unit-1 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views14 pages

Unit-1 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Uni

t-
ICor
e-Jav
a

Jav
a:

Javaisapr ogrammi ng l
anguageand apl
atf
[Link] aisa hi
gh l
evel
,
robust
,obj
ect-
orient
edandsecureprogr
amminglanguage.

Javawasdev opedbySunMi
el crosyst
ems( whichi
snowt hesubsidiar
yof
Oracl
e)[Link] i
ngi
sknownast hefatherofJav a.
Befor
e Java,is name [Link]
t nce Oak was al
ready a regi
ster
ed
company,soJamesGoslingandhisteam changedthenamef rom Oakto
Java.

Pl
atf
orm:Anyhardwareorsoft
wareenv
ironmenti
nwhichapr ogr
am runs,
i
sknownasapl atfor
[Link]
ahasar unt
imeenv
ironment(JRE)and
API
,i
tiscal
l
edapl atf
orm.

Cur
rentl
y,Javaisusedinint
ernetprogr
ammi ng,mobiledevices,games,e-busi
ness
sol
uti
ons,et
[Link]
lowi
ngar
egivensigni
fi
cantpoint
sthatdescr
ibethehist
oryofJava.

1)JamesGosli
ng,Mi keSher
idan,andPatri
ckNaught
oni ni
ti
atedt
heJaval
anguage
pr
ojecti
[Link]
lteam ofsunengi
neer
scal
ledGreenTeam.

Concept
sinJav
a

2)I
nit
ial
l
yitwasdesi
gnedf
orsmal
l
,embeddedsy
stems

i
nel
ect
roni
cappl
i
ancesl
i
keset
-t
opboxes.

3)Fi
rst
ly,
itwascal
l
ed"
Greent
alk"byJamesGosl
i
ng,
andt
hef
il
eext
ensi
onwas.
gt.

4)Af
tert
hat
,itwascal
l
edOakandwasdev
elopedasapar
toft
heGr
eenpr
oject
.

WhyJav
awasnamedas"
Oak"
?

5)WhyOak?Oakisasy mbolofstr
engthandchosenasanat
ionalt
reeofmany
count
ri
esl
i
ket
heU.
S.A.
,Fr
ance,Ger
many,Romani
a,et
c.

6)In1995,Oakwasr
enamedas"
Jav
a"becausei
twasal
readyat
rademar
kbyOak
Technol
ogi
es.

WhyJav
aPr
ogr
ammi
ngnamed"
Jav
a"?
Uni
t-
ICor
e-Jav
a

7)WhyhadtheychosethenameJav aforJavalanguage?Theteam gat heredt ochoose


anew [Link] edwordswer e"dynamic","
rev
olut
ionary"
,"Silk","j
olt
","
DNA",
et
[Link]
lectedtheessenceofthetechnology :revol
uti
onary
,
dynami
c,l
iv
ely,cool
,uni
que,andeasyt ospell
,andfuntosay.

Accor
dingt
oJamesGosli
ng,"Javawasoneofthetopchoi
cesalongwi t
hSil
k".Si
nce
Javawassouni
que,
mostoftheteam member
spref
err
edJavathanothernames.

8)JavaisanislandinIndonesi
awher ethefi
rstcof
feewaspr
oduced(
cal
ledJav
a
cof
fee)
.Iti
[Link]
i
ngwhil
e
hav
ingacupofcoffeenear
byhisoff
ice.

9)Not
icet
hatJav
aisj
ustaname,
notanacr
ony
m.

10)I
nit
ial
l
ydev
elopedbyJamesGosl
i
ngatSunMi
crosy
stems

(
whi
chi
snowasubsi
diar
yofOr
acl
eCor
por
ati
on)andr
eleasedi
n1995.

11)I
n1995,
Timemagazi
necal
l
edJav
aoneoft
heTenBestPr
oduct
sof1995.

12)JDK1. 0wasreleasedonJanuary23,[Link]
erthefi
rstr
eleaseofJava,t
herehave
beenmanyaddi t
ionalfeatur
esaddedt ot [Link] Jav aisbeingusedi n
Windowsappl i
cati
ons,Webappl i
cati
ons,enter
priseappli
cat
ions,mobil
eapplicat
ions,
cards,
[Link] ersi
onaddsnewf eat
uresinJava.

ARCHI
TECTUREOFJAVA:

JavaAr chi
tect
ureisacoll
ectionofcomponent
s,i.
e.,JVM,JRE,[Link]
i
ntegratestheprocessofinterpr
etat
ionandcompilati
on.I
tdefi
nesallthe
processesinvol
vedincreat
ingaJav aprogr
[Link] chi
tect
ureexpl
ains
eachandev erystepofhowapr ogram i
scompil
edandexecuted.

Jav
aAr
chi
tect
urecanbeexpl
ainedbyusi
ngt
hef
oll
owi
ngst
eps:

 Ther
eisapr
ocessofcompi
l
ati
onandi
nter
pret
ati
oni
nJav
a.

 Jav
acompi
l
erconv
ert
stheJav
acodei
ntoby
tecode.

 Af
tert
hat
,theJVM conv
ert
stheby
tecodei
ntomachi
necode.

 Themachi
necodei
sthenexecut
edbyt
hemachi
ne.

Thefoll
owingfigurerepr
esent
stheJav
aAr
chi
tect
urei
nwhi
cheachst
epi
s
el
aborategraphical
l
y.
Uni
t-
ICor
e-Jav
a

Component
sofJav
aAr
chi
tect
ure

TheJav
aar
chi
tect
urei
ncl
udest
het
hreemai
ncomponent
s:

o Jav
aVi
rt
ual
Machi
ne(
JVM)
o Jav
aRunt
imeEnv
ironment(
JRE)
o Jav
aDev
elopmentKi
t(JDK)

Jav
aVi
rt
ual
Machi
ne

Themai nf eatur
eofJav ai [Link] st andsforWrit
eOnceRun
[Link] eatur
estat
esthatwecanwr i
teourcodeonceandusei t
anywhereoronanyoper ati
[Link] canrunanyoft he
pl
atfor
msonl ybecauseoftheJavaVir
[Link]
saJav aplat
for
m
componentt hatgi
vesusanenv i
ronmenttoexecutejavapr
[Link]'s
maintaskistoconv er
tbyt
ecodeintomachinecode.

JVM,f i
rstofal
l,l
oadsthecodei nt
omemor yandv eri
fi
esit
.Aft
ert
hat,i
t
execut
es the code and pr
ovides a runt
ime env
ir onment
.Jav
a Vi
rt
ual
Machine(JVM)hasitsownarchit
ectur
e,whichi
sgivenbelow:

JVM Ar
chi
tect
ure

JVM i
sanabst
ractmachi
net
hatpr
ovi
dest
heenv
ironmenti
nwhi
chJav
a
Uni
t-
ICor
e-Jav
a

byt
ecodei
sexecut
[Link]
all
i
ngf
igur
erepr
esent
sthear
chi
tect
ureoft
he
JVM.

Cl
assLoader:ClassLoader i
s a subsy
stem used t
ol oad cl
ass f
il
es.
Cl
assLoaderfi
rstloadstheJavacodewhenev
erwerunit
.

Cl
assMet hodAr
ea:I
nthememor y,
ther
eisanar eawheretheclassdatais
st
ored duri
ng t
he code'
s executi
[Link] met hod area holds the
i
nformati
onofst
ati
cvari
abl
es,st
ati
cmet hods,stat
icbl
ocks,andinstance
methods.

Heap:Theheapareai sapartoft
heJVM memor yandiscreat
edwhenthe
JVM star
[Link]
ssi zecannotbestat
icbecauseiti
ncreaseordecr
ease
dur
ingtheappl
i
cationruns.

Stack:I
tisalso r
efer
red to ast hr
eadst ack.I
tiscr eat
edf orasi ngl
e
executi
onthr
[Link] hisar
eat ostor
et heelementsli
kethe
parti
alr
esul
t,l
ocal
vari
abl
e, datausedforcall
ingmethodandr et
urnsetc.

Nati
veStack:I
tcont
ainst
hei
nfor
mat
ionofal
lthenat
ivemet
hodsusedi
n
ourappl
i
cati
on.

Execut
ionEngi
ne:I
tist
hecent
ralpar
toft
heJVM.I
tsmai
ntaski
sto
Uni
t-
ICor
e-Jav
a

execut
ethebyt
ecodeandexecut
etheJav [Link]
ionengi
ne
hasthr
eemaincomponent
susedforexecuti
ngJav aclasses.

o Int
erpreter
:Itconv
er t
stheby t
ecodeint
onati
[Link]
sequential
l
y execut es t he code. The i nter
pret
er int
erpret
s
conti
nuouslyandev enthesamemet hodmult
ipleti
[Link]
sreduces
theperformanceoft hesystem,andtosol
vethis,t
heJITcompileri
s
i
ntroduced.
o JITCompiler
:JITcompil
eri
sintr
oducedt
oremovethedr
awbackof
theint
erpr
eter.I
tincr
easesthespeedofexecut
ionandimpr
oves
perf
ormance.
o GarbageCol lect
or:Thegar bagecol l
ect
orisused t
o managet he
memor y,andi ti
sapr ogr
am wr it
teninJava.I
twor
ksint wophases,
i
.e.
,Mar [Link] kisanar eawherethegarbagecoll
ector
i
dentif
ies the used and unused chunks ofmemory .The Sweep
r
emov estheidentif
iedobj
ectf r
om theMark

Jav
aNat
iveI
nter
face

Jav
aNat i
veInt
er f
acewor
ksasamedi
atorbet
weenJav
amet
hodcal
l
sand
nat
ivel
i
brari
es.̀

Jav
aRunt
imeEnv
ironment
(JRE)
I
tprovi
desanenv ir
onmenti nwhi
chJav
apr ogramsar [Link]
t
akesourJavacode,i
ntegr
atesi
twi
tht
herequi
redli
brar
ies,
andthenstart
s
t
heJVM toexecut
eit.

JREi
stheon-
diskpar
tofj
avat
hatcr
eat
etheJVM.

Jav
aDev
elopmentKi
t(
JDK)
Iti
sasof t
waredevelopmentenvi
ronmentusedint hedevel
opmentofJava
appli
cat
ionsandappl et
[Link],acompil
er,an
i
nterpr
eterorl
oader,
andsev er
aldevelopmentt
oolsinit
.

JDKal
l
owsdev
eloper
stocr
eat
ejav
apr
ogr
amst
hatcanbeexecut
edand
Uni
t-
ICor
e-Jav
a

r
unbyt
heJVM andJRE.

Si
mpl
eExampl
e:

I
ntr
oduct
ionofsi
mpl
epr
ogr
am ofj
avabyusi
ngcmdcommand

InJav a,
everyappli
cati
onbeginswithacl
assname,andthatcl
assmustmatchthe
fi
[Link]
rstJav
af i
le,
call
edDemo.j
ava,whi
chcanbedoneinanytext
editor(l
i
keNot epad)
.

cl
assDemo

Publ
i
cst
ati
cvoi
dMai
n(St
ri
ngar
gs[
])

Sy
stem.
out
.pr
int
ln(
“HELLO….
.!
!!
!!
!”
);

OUTPUT:
HELLO….
.!
!!
!!
!

Savet he code in Not


epad as "Demo.
jav
a".Open Command Prompt
([Link])
,navigat
et ot
hedirect
orywhereyousavedyourf
il
e,andtype
"j
avacMain.j
ava":

C:
\User
s\pat
( hwhi
chhav
etosav
eyourpr
ogr
am)
>jav
acMai
n.j
ava

Thiswi
llcompil
ey [Link]
herearenoerr
orsi
nthecode,t
hecommand
promptwill
takeyoutot henextl
[Link],t
ype"j
avaMain"t
orunt
hefi
l
e:

C:
\User
s\pat
h>j
avaMai
n
Uni
t-
ICor
e-Jav
a

Expl
anat
ion:
Everyl
ineofcodethatr
unsinJavamustbei
nsi
deaclass.I
nourexample,
wenamedt hecl
[Link]
dal
waysst
artwithanupper
case
fi
rstl
etter
.

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)
Anycodeinsi
det hemain(
)methodwillbeexecut
[Link]
eryJavapr
ogr
am
hasaclassnamewhi chmustmatchthefil
ename,andt
hatever
ypr
ogr
am
must0000000contai
nthemain()met
hod.
 cl
asskey
wor
disusedt
odecl
areacl
assi
nJav
a.
 publ
ickey
wordisan accessmodi
fi
ert
hatr
epr
esent
svi
sibi
l
ity
.It
meansiti
svi
sibl
etoal
l
.
 stat
icisakeyword.I
fwedecl ar
eanymet hodasst at
ic,i
tisknownas
thestati
[Link] eadvantageoft hestati
cmet hodisthat
ther
eisnoneedt ocreat
eanobj ecttoinvokethestati
cmet [Link]
main()methodisexecut
edbyt heJVM,soi tdoesn'
trequi
recreati
ng
anobjecttoi
nvokethemain()[Link],itsavesmemor y.
 v
oidisther
etur
nty
peoft
hemet
hod.I
tmeansi
tdoesn'
tret
urnany
v
alue.
 mai
nrepr
esent
sthest
art
ingpoi
ntoft
hepr
ogr
am.
 St
ri
ng[
]ar
gsorSt
ri
ngar
gs[
]isusedf
orcommandl
i
near
gument
.
 System.
[Link]
int
ln(
)isusedt oprintst
[Link],Syst
em i
sa
cl
ass,outisanobjectofthePri
ntStr
eam cl
ass,pr
int
ln(
)isamethod
ofthePri
ntStr
eam cl
ass.

Var
iabl
es:

Var
iabl
esar
econt
ainer
sforst
ori
ngdat
aval
ues.
Uni
t-
ICor
e-Jav
a

I
nJav
a,t
her
ear
edi
ff
erentt
ypesofv
ari
abl
es,
forexampl
e:

 St r
ing-st orestext,suchas" Hell
o".Stri
ngv al
uesaresur roundedby
doubl equot es
 i
nt-st oresint
eger s(wholenumber s),wi
thoutdecimals,suchas123
or- 123
 float-st or
esf l
oatingpointnumbers, wit
hdecimals,suchas19. 99or
-19.99
 char-st oressinglechar act
ers,suchas' a'or'
B'.Charv al
uesare
surr oundedbysi nglequotes
 bool ean-st or
esv alueswithtwostates:tr
ueorfalse

Decl
arat
ion&I
nit
ial
izat
ionofv
ari
abl
e:

Sy
ntax:

t
ypev
ari
abl
eName=v
alue;

Exampl
e:

Rul
eofdecl
ari
ngv
ari
abl
e:

 AvariablenamecanconsistofCapit
allet
ter
sA-Z,l
owercasel
etter
sa- z,
di
git
s0- 9,andt
wospeci
alcharact
erssuchasunder
scor
eanddollarSign.

 Thef
ir
stchar
act
ermustbeal
ett
er.

 Bl
ankspacescannotbeusedi
nvar
iabl
enames.

 Jav
akey
wor
dscannotbeusedasv
ari
abl
enames.

 Var
iabl
enamesar
ecase-
sensi
ti
ve.

Scopeofv
ari
abl
e:

Var
iabl
eScopemeans-Thatl
i
mit
,asf
arast
hev
ari
abl
ecanbeused.

I
nJav
ather
ear
evar
ioust
ypesofv
ari
abl
escope:

 Local
var
iabl
es
Uni
t-
ICor
e-Jav
a

 I
nst
ancev
ari
abl
es

 Cl
ass/
Stat
icv
ari
abl
es
LocalVar
iabl
e:

Avariablethatisdecl
aredwithint
hemet hodthati
scall
edlocalv
ariabl
es.I
t
i
sdef ined in method orot herstatement
s,such asdef i
ned and used
out
sidet heblockormet hod,
thevariabl
ecannotbeused.

I
nst
ancev
ari
abl
es

Anon-
stati
cvar i
abl
et hatisdecl
aredwi
thi
nt hecl
assbutnotinthemethod
i
scall
ed instancev ari
able.I
nst
ancev ar
iablesar
er el
ated t
o a speci
fi
c
obj
ect
;theycanaccesscl assvar
iabl
es.

Cl
ass/
Stat
icv
ari
abl
es

Avari
abl
ethatisdeclar
edwi t
hstati
ckey
wor
dinacl
assbutnoti
nthe
met
hodiscal
ledst
ati
corclassvar
iabl
e.

Exampl
eScopeofVar
iabl
e:

cl
assA

i
ntamount=100; //
inst
ancev
ariabl
e
st
ati
cintaccount
_no=2315;//
stati
cvar
iabl
e

publ
i
cst at
icv
oidmai
n(St
ri
ng[]ar
gs)
{
intage=35;
//l
ocal
vari
able
}

}
OUTPUT:
Uni
t-
ICor
e-Jav
a

DATATYPE:Dat
aty
pesspeci
fyt
hedi
ff
erentsi
zesandv
aluest
hatcanbest
oredi
n
t
hev
ari
abl
[Link]
ear
etwot
ypesofdat
aty
pesi
nJav
a:

Dat
aTy
pe Def
aul
tVal
ue Def
aul
tsi
ze

Bool
ean f
alse 1bi
t

Char '
\u0000' 2by
te

By
te 0 1by
te

Shor
t 0 2by
te

I
nt 0 4by
te

Long 0L 8by
te

Fl
oat 0.
0f 4by
te

Doubl
e 0.
0d 8by
te
Uni
t-
ICor
e-Jav
a

[Link]
imi
ti
vedat
aty
pes:Thepr
imi
ti
vedat
aty
pesi
ncl
udeBool
ean,char
,by
te,
shor
t,i
nt,
long,
floatanddoubl
e.
[Link]-
pri
mit
ive dat
a t
ypes:The non-
pri
mit
ive dat
a t
ypes
i
ncl
udeCl
asses,
Int
erf
aces,
andAr
ray
s.

Jav
aPr
imi
ti
veDat
aTy
pes
I
nJav al
anguage,pr
imi
ti
vedatatypesar
et hebui
l
dingblocksofdat
amani
pul
ati
on.
Thesear
ethemostbasicdat
atypesavai
lablei
nJavalanguage.

Ther
ear
e8t
ypesofpr
imi
ti
vedat
aty
pes:

1. bool
eandat
aty
pe
2. byt
edat
aty
pe
3. chardat
aty
pe
4. shor
tdat
aty
pe
5. i
ntdat
aty
pe
6. l
ongdat
aty
pe
7. f
loatdat
aty
pe
8. doubl
edat
aty
pe

Bool
eanDat
aTy
pe
TheBool
eandat
atypeisusedtostor
eonlyt
wopossibl
eval
ues:t
rueandf
[Link]
s
dat
atypei
susedforsi
mplefl
agsthattr
ackt
rue/
fal
secondi
ti
ons.

TheBooleandat
aty
pespeci
fi
esonebi
tofi
nfor
mat
ion,
buti
ts"
size"can'
tbedef
ined
preci
sel
y.

Exampl
e:

Bool
eanone=f
alse

By
teDat
aTy
pe
Theby
tedat
aty
pei
sanexampl
eofpr
imi
ti
vedat
aty
pe.I
tisan8-
bitsi
gnedt
wo'
s
Uni
t-
ICor
e-Jav
a

complementi
nteger.I
tsval
ue-r
ange l
i
es bet
ween -
128 to 127 (
incl
usi
ve)
.It
s
mini
mum val
ueis-128andmaxi
mum val
uei
[Link]
sdefaul
tval
ueis0.

Thebytedat aty
peisusedt osavememor yinlar
gearr
ayswherethememory
savi
ngsismostrequi
red.I
tsav
esspacebecauseabytei
s4timessmall
ert
hanan
i
nteger
.Itcanal
sobeusedinpl
aceof"
int
"dataty
pe.

Exampl
e:

by
tea=10,
byt
eb=-
20

Shor
tDat
aTy
pe
Theshortdatatypei
sa16- bitsi
gnedtwo'
scompl
ementint
[Link]
sval
ue-
rangel
ies
bet
ween- 32,
768t o32,767(incl
usi
ve)
.It
smini
mum val
ueis-32,768andmaximum
val
ueis32,767.I
tsdefaultv
alueis0.

Theshor
tdatat
ypecanal
sobeusedt
osav ememor
yjustl
i
keby
tedat
aty
pe.A
shor
tdat
aty
peis2ti
messmal
l
ert
hananint
eger
.

Exampl
e:

shor
ts=10000,
shor
tr=-
5000

I
ntDat
aTy
pe
Theintdat
atypeisa32-bitsignedt
wo'scompl ementint
eger.I
tsvalue-r
angeli
es
bet
ween-2,147,
483,
648(-2^31)to2,
147,483,
647(2^31-1)(i
ncl
usi
ve).It
smi ni
mum
val
ueis-2,
147,
483,
648andmaxi mum val
ueis2,147,
483,
[Link]
sdefaul
tv al
ueis0.

Theintdat
atypeisgener
all
yusedasadef
aul
tdat
aty
pef
ori
ntegr
alv
aluesunl
essi
f
ther
eisnoprobl
em aboutmemory.

Exampl
e:

i
nta=100000,
intb=-
200000
Uni
t-
ICor
e-Jav
a

LongDat
aTy
pe
Thel ongdat aty peisa64- bi
ttwo'scompl ementi [Link]
sval
ue-
rangeli
esbetween
-9,223,372,036,854,775,808(
-2^63)to9,223,372,036,854,775,
807(
2^63-1)(
incl
usi
ve)
.
I
t s mi nimum v al
ue i s - 9,223,
372,036,854,775,808and maximum v al
ue is
9,223,372,036,854,775,[Link]
tv al
uei [Link] ongdatatypeisusedwhen
youneedar angeofv al
uesmor ethanthosepr ov i
dedbyi nt.

Exampl
e:

l
onga=100000L,
longb=-
200000L

Fl
oatDat
aTy
pe
Thefl
oatdatatypeisasingl
e-pr
eci
sion32-bitI
EEE754f l
oat
ingpoint.
It
sval
uerange
i
sunli
mited.I
tisrecommendedt ouseaf l
oat(inst
eadofdoubl
e)ifyouneedtosave
memoryinlargearraysoffl
oati
ngpointnumber [Link]
oatdat
at ypeshoul
dnever
beusedforpreci
sev al
ues,
[Link]
sdefaul
tval
ueis0.0F.

Exampl
e:

f
loatf
1=234.
5f

Doubl
eDat
aTy
pe
Thedoubl edatat y
peisadoubl e-
preci
sion64-bi
tIEEE754floati
ngpoi
nt.I
tsvalue
rangei sunli
mit
[Link] atypeisgenerall
yusedfordecimalval
uesjust
l
ikef l
[Link]
atypealsoshouldneverbeusedforpreci
seval
ues,
suchas
currency.I
tsdefaul
tval
ueis0.0d.

Exampl
e:

doubl
ed1=12.
3

CharDat
aTy
pe
Thechardat
aty
pei
sasi ngle16-bi
tUnicodechar
act
er.I
tsv
alue-r
angeli
esbet
ween
'
\u0000'(
or0)t
o'\
uff
ff'(or65,535incl
usiv
e).
Thechardat
at ypeisusedtostor
e
char
acter
s.
Uni
t-
ICor
e-Jav
a

Exampl
e:

charl
ett
erA='
A'

OPERATORS:
Oper
atori
nJav
aisasy
mbol
thati
susedt
oper
for
m oper
ati
ons.

Forexampl
e:+,
-,*
,/et
c.

Ther
ear
eei
ghtt
ypesofoper
ator
sinJav
awhi
char
egi
venbel
ow:

1. Unar
yOper
ator
,
2. Ar
it
hmet
icOper
ator
,
3. Shi
ftOper
ator
,
4. Rel
ati
onalOper
ator
,
5. Bi
twi
seOper
ator
,
6. Logi
calOper
ator
,
7. Ter
nar
yOper
atorand
8. Assi
gnmentOper
ator
.

You might also like