Escolar Documentos
Profissional Documentos
Cultura Documentos
Thejavaprogramminglanguageisahighlevellanguagethatcanbecharacterizedby
thefollowinggivencharacteristics
Simple
ObjectOriented
Platformindependent
Secured
Robust
Architectureneutral
Portable
Dynamic
Interpreted
HighPerformance
Multithreaded
Distributed
Amongallthesepropertiesthemostimportantpropertyisplatformindependency.
Note:Platformisacombinationoftwothingsi.eoperatingsystemandHardware(processor).
Note:Everymachinecodeisdependentonitsoperatingsystemenvironment.
Anotherreasonwhichexplainthatallotherlanguagesexceptjavaareplatformdependentis
thatthemachinecodeofallotherlanguageareOSdependentaswellasProcessordependent.
ButthemachinecodeofjavaisnodoubtOSdependentbutalsoProcessorIndependent.
TheperformanceofCwillvaryatdifferentconfigurationbutjavagivesthesameperformanceat
allkindofprocessor.
CopyrightOSAHUB2014
invention,sowhatwasthenecessityforwhichjavawascreated?Theanswerliesinthehistory
ofthelanguages.letsstartwiththefirsteverlanguageandthenthesecondthenthirdandso
onandseethatwhatweretheproblemswhichcausetheinventionofJavalanguage.
Note:Assemblerwasusedtoconverttheassemblylanguagetothemachinelanguage
Thislanguagewasprocessordependentbecauseeveryprocessorhaddifferentsymbols.
Toovercomethisproblemstandardswereusedbyallprocessorstoimplementsamesymbols.
BasedonthesestandardsdifferentlanguagesweredevelopedwhichareknownasHighlevel
language.
CopyrightOSAHUB2014
Letsdiscusstheabovefigure.firstofalljavacompilercreateabytecodebutthisbytecodeisnot
OSdependent,notasingleOSlibraryusedtocreatethisbytecode.Butkeepinmindthatthisis
notthemachinecode.machinecodeisalwaysOSdependent.Nowallweneedtodoiscarry
thisbytecodetodifferentOSandrunit.SosupposewetakeittoadifferentOSsayOS1and
andOS2,wepastethebytecodeonboththeOSandtrytorunit.
Willitrun?
NO.NighterthisbytecodeiscompatibletothenewOSnoritisamachinecodethatcanberun
bytheprocessor.TheOSenvironmentwasntcompatibletothebytecodeoitwasimpossible
thatthebytecodecanberunonthedifferentOStothatitwascreatedin.SOavirtual
environmentwascreatedforthebytecodeinsidetheOSandthisvirtualenvironment.SOnow
wastheneedofsuchasoftwarethatwillcreateavirtualenvironmentinwhichthebytecodecan
beconvertedintoamachinecodebythehelpoftheOSandthesoftwarewhichwasusedfor
thepurposeisknownasJVM(javavirtualmachine).ButJVMareOSdependent,youcantjust
copyandpasteoneJVMfromoneOSandotherOSandrunit.Nowagainthequestionwas
whowillmaketheJVMforeachOS.AsasolutionSunmicrosystemmakeagenericJVMapi
whichtheOSvendorscoulddownloadfromtheirwebsiteandafteraddingafewfile(theirOS
file)theycouldeasilymadeaJVMfortheirOS.
Laterintimeafterjava1.5javaintroducetheconceptofJRE.NowthatlittlepartoftheJVMthat
OSvendorhadtoadd,doesntneedtobedone.ALltheworkisdonebytheSunmicrosystem
only.YoucanjustgototheirwebsiteanddownloadyourOSspecificJREandrunit.itwill
createthejavavirtualenvironmentforyou.
Andsojavasolvedalltheprogrammingrelatedproblems.
Note:JVMisthepartofJRE.
CopyrightOSAHUB2014
Compilation in Java
CopyrightOSAHUB2014
jvmhasthreestepsforexecutionoftheBytecode
step1:
Thejvmcarrythebytecodetothememoryfortheexecutionpurpose.JVMhasa
programinitwhichisknownasclassloader.classloaderisofthreetype(1)bootstraploader
(2)systemloader(3)extensionloader
step2:
Verifythecode.JVMhasanotherprogramwhichisusedtoverifythecodeforthe
securitypurpose.
step3:
Finalstepistheexecutionstepinwhichbytecodeisconvertedintothemachinecode
bytheexecutionengine.TheexecutionenginewhichisusedforthispurposeisJustintime
compiler
TheJVM:
thegivenfigureshowsthecomponentsofJVM
CopyrightOSAHUB2014
OrganizationofJVM
DetailedOrganizationofJVM
EDITProgrammerwritestheprogramandstoreitondisk
COMPILATIONCompilercreatesthebytecodefromthesourcecode
LOADNowJAVcomesintoplayandclassloaderloadthebytecodeintothememory
VERIFICATIONVerificationisdonetoensurebytecodedoesntviolatesecurity
requirements
EXECUTIONbytecodeisconvertedintothemachinecodebytheexecution
engine(Justintimecompiler)
CopyrightOSAHUB2014
I.
II.
III.
objectbasedprogramminglanguage
Pureobjectorientedprogramminglanguage
objectorientedprogramminglanguage
I.
itdoesnotsupportthetwofeaturesofoops
(i.)Inheritance
(ii.)RuntimePolymorphism
Exampleofsuchlanguagesarejavascript,vbscript,visualbasicetc.
II.
Question
:Isjavanormaloopsbasedlanguageortrulyoopsbasedlanguage?
Variable
Variableisanamegiventoamemorylocation.Justthinkabouttheneedofthevariables.Can
youmakeaprogramwhichtakestwonumbersfromtheuserandaddthemwithoutusingthe
conceptofvariable?No.Weneedtostorethethosetwovaluesandnamethembecausewe
aregonnaneedthemlaterfortheadditionoperation.Ifprocessordoesntknowaboutthe
locationofthosetwonumbersthenitwillbealmostimpossibletofindthosetwonumbersfrom
thestorage.Sotheconceptofvariablewasintroduced.Variablealsoknownascontainersoften
astheirtaskistoreservesomememoryforstoringtheinformation.
Nowsomeproblemsarrivewithvariablewhichcausestheinventionofdatatypeconcept.
supposeinitiallyavariablereserved2byteofmemorybutuserbought100byteofdatawhich
resultthedataloss.Inotherscenariovariablereserved100byteofmemorybutuserbought
only5byteofdatawhichresultthememoryloss.Sotoovercomethisproblem,theconceptof
DataTypewasintroduced.
Data Type
DataTypeconceptwasintroducedtocheckthedatalossandthememoryloss.
Thedatatypegivestwomajorinformationaboutthedata
1. Whatkindofdataisthis
2. howmuchm/mdoesitrequires
CopyrightOSAHUB2014
Question:Inallotherlanguagespriortojavacharusedtotakeonly1bytetostoresowhat
happenedinjavalanguage?
#Rule:Injava,bydefaultallthedatatypearesignedandsoonebitformabyteis
reservedforthe+orFormulaforcalculatingtherange:
2n1
=2n1
1{n=numberofbit)
81
81
2 =2 1
27
=27
1
128=127
#Rule:Injavaeverydatatypehasadefaultvalue.
byte=0boolean=false
f
short=0
float=0.0
d
int=0double=0.0
L
long=0
char=/u0000
CopyrightOSAHUB2014
Note:Injavawewriteeverythinginsideclassandtherearetwoplaceswithinaclasswhere
wecandefineavariableasshownbelow
Note:Javadoesnthaveanygarbagevalueforanyvariable.whichcanbeexplainedfromthe
belowgiventworules
#Ruleforclasslevelvariable:Theruleforthedefaultvalueofcertaindatatypeis
applicableonlyfortheclasslevelvariable.sonoclassnamevariablecanhavethe
garbagevalue
#Ruleforlocalvariable:Youmustinitializethelocalvariablebeforefirstuse.
Question:Whichofthegivenfunctionwillbecalled?
Inabovequestionwecanseethataccordingtopolymorphism10cangoinanyofthefunction
andsoambiguityerrormayoccursoforthisjavamadearulewhichisgivenbelow
#Rule:Bydefaultalltheintegerliteralsaretreatedasinteger(intdatatype)only
Note:Ifweexplicitlywantanintegertobestoredinthelongdatatypethenwehavetoputlin
thesuffixoftheinteger.
likeshow(10l
):thiswillgototheshow(longl
CopyrightOSAHUB2014
Question:
byteb=10
bytec=20
byted=b+c
Whatwillbetheoutputofthisprogram.
Ans:Whilecompilingerrorwillcome.Lookforthebelowgivencodetoknowthereason
Note:whenacalculationisdone,theresultissaidtobetheliteralbeforeitisstoredinthe
destinedvariable.
Sob+cwillgive30andthe30isconsideredasintegerandwhenwetrytostorethisinteger
valueinthebytetypevariable,atypemismatcherrorwillcomeatthecompiletime.
byted=(byte)b+c{thisiscorrect,andwhatwehavedonehereisknownastypecasting}
Question:Whichmethodwillbecalled?
Answer:Thesecondmethodwillbecalledanditcanbeexplainedbythefollowinggivenrule.
#Rule:Bydefault,allfloatingliteralsinjavaaretreatedasdouble.
Note:Ifweexplicitlywantadecimalnumbertobetreatedasthefloatingtypethenwehaveto
putfinthesuffixofthenumber.
likeshow(10.2f
):thiswillgototheshow(floatf
CopyrightOSAHUB2014
10
#Rule(convention):Alwayskeepthefirstletteroftheclassnamecapital.
Note:Eachtechnologygivesastartingpointfortheprogramandthisstartingpointisafunction
whichisknownasmainfunction.Injavathismainfunctionis
publicstaticvoidmain(String...s)
or
publicstaticvoidmain(Strings[])
#Rule(convention):Alwayskeepthenameofyourjavafilesameastheclassname.
#Rule:Thebytecodeofeveryclassisstoredinaseparate.classfileandnameof.class
filewillbethesameasyourclassname.
Question:Canwemakemorethanoneclassintoasinglejavafile?
Answer:Yes.
Question:Ifwearehavingmorethanoneclassinajavafilethencanwehavethemain
functionineveryclass?
Answer:yes
Explanation
:thebytecodeforeveryclassisstoredinseparate.classfilesoeach
Classisindependent.
#Rule(Convention):Alwayskeepaseparatejavafileforeachclass.
Question:Canwekeepmorethanonemainmainfunctioninoneclass?
Answer:Yes
Explanation:Javaisaoopsbasedlanguage,ithasapropertycalledpolymorphismaccording
towhichaclasscanhavemorethanonefunctionofsamename.Injava,mainisapartofa
classsoalltherulesoffunctionofclasscanbeappliedtomainfunction.
Question:Canwecallmain()functionofoneclassfromotherclass?How?
Answer:yes.Tryityourselfandtrycallingmainfunctionofboththesignatures
1. publicstaticvoidmain(Strings)
2. publicstaticvoidmain(Strings[]s)
Note:Thedifferencebetweenvariablelengthargumentandthearraytypeargumentisthatby
usingthevariablelengthargumentwecancallthemainfunctiondirectlywithoutusingany
argumentbutinarraytypeargumentwemustneedtocreateanarrayandpassitasa
parameter
CopyrightOSAHUB2014
11