Java
Java
To!
developed bg Janes Ghosling , Mike shendan and
Naughtan. Ihe þoject uses caled'Caseertalk and Patid
|he team developing the brojet sere called
its extension was.gt
sas just develo þed fo embedded systems ngtthe'Gyreenteom.
|
machines. uhen he þrolec oau Ginithed, for big and compex
ore membecs et'qveen
teom'
ituses tans feed wa
and ofter the develoþment,
to
Nomed 'Dak'. [5eve wa it
anottes Compony naned 'Oalk'
decided to change ta nane
Ajter gatheing many so feom
choose Tava
becau e it was acotfee. names,theteom
a
release þadlicy in l995 b Sun
Micosytemu'.
Who is diffesence betuoeen Tava and Ct++?
Sorme imþortat ifforence beteen Java andC+t axe:
) C++is platfo debendent,
bat, Java is plat+foom independent.
)C++ i mainy sqsto, langunge hut, Tove is mainly
) Ctt isa 00R lnquage, ut, apcation longuage
Tava is neary lo0. O0ts
iv) C++ sabpta
multile inhetance ond operator veslading languge.
Sapit d none
these features. butJava
Av)C++ has default arguements
bat, Tava doesnt sSubarts
3ohat is diffeence betuween JDKTNM ondJRE? t.
JDk: t stod tor JovaDeve oþmet K+
T+is asotwre
development envioment shich is ued
abpications and ablets. H contains a to
develo Taa
feuo othec Tesgurce þivote Tum od
such au intesbretes [loades
CompilerJavac)an orchiever joua)r aa
Cjavadoc). Tt phyi jor, adocunenteti on
genesator
caly exsists
TVM:T+ is anabstrac
becouse it does+ nachine.tiscalled
exststs pryaicaly. virtual machine
sheciAc java untime Iþrovides a
Can
envisoment in ohich Java, hyte Cydo
be ekecuted.
Hcan Tn hose Cudos stch is on ffen
in ony lanquoge but comiled in ytecsde.
TRE:
Tt sa Set of sotwae tools ushich ore use to develo
Jova apicotin.
u
He used to bKOuide theetime ioMent
the impergentetion TUm. -þhyicalyexsiSta.
what ia clais and obect io Jaua!
JassA class in Tav. is set d obiect shich shares gOne common
behavious and attibuteg. T+ is notareal wodd cntity
just temHate er bluehnint Itla
A
groub otbjects
classin Jon Can contetn
(()Athobutes i) Metthoda (ii )Constnctor (ivy Nested
(U) Trtes-face
clai
bijec: It is boaic unit Tovo programming-T+ is a seal-world
entity An object concist of Hhree thing
C) State:
His rebrsented hy attnbtes.
(n) Behauiaur:
Iia sesresented hy mettrds.
(iDTdett: Tt comnonda to give caniqu. nane to
eve
objcc
5 hatis chstacion inJova? Exahuith eal world exomple.
Sisstractioni T+ is a þOcess ol hicing the implementation
details anc shousing onlys
essentialfanctiondy
to the ser.
Real ite exam þle can be he ebsites spened in our inehile
phones, n appication
rning on ur cOmkuter or phona,etc.
ohat is encapsulaion in Java Dxplain
uithreal usvid eanble
Sncabsaton:It is a fundamental Concebt o0Ps Hhat
veferg to the bOndling
methods wsithig as singl cttibtes
ond
class injova. unit,hich is caled
Real ife exanþles can be a
mecdcinal casule, a stetiona
box a
hat
schss bogi etc.
ia inheitence in ava) Explan wtth eal lye exonde.
Inhectonce:H is one the importast concebt oDPs. TAi
mehaiAm by uohich aclas is alous to inhert
4hefeatures fonether. Tieclaus nheitthg the
onother clasi callodsehclas cnd the cla giving
Hs aa
sopeties called superelass. There are frur
tybes of inhesiten ce in Jàva.
)ingl) Muiltiev Hiexerchical Gy Hytsd
The besh examble o
inhenta ce ia the humascoluion
3
Soblean tyoe
SNanent tybe
boolea
chasactes valu Trtegral value.
har
Tnteges Floating -point
floa double
bute shot int long
(0Exblaio the Concet of
A
"tue
i
cauting" o
type coNersion
>Tyhe Castng: daype converted ito anhe datatype by the
Command od brogrammer wing cautng operatos. can
be aplic ocomþ cibk as oellas inconpatible It
dada
tyhe. We do typecasting-for situatin cohen the
destnation is small then the suurce.
Tybe Convessios; A datcctype is converted
hto anothes datatpe bythe
Conpiles. H Can be ony applied only to Comþotble
adatypez. there isrlt need for Cartng operator.
The destinati anneeds to be large
thanthe source.
Ochat is the pusþose of he Stsing datatype nJava?
H java stong is a Sequence e characteg that exsisis as an objet
the clas java lang- Stoing Java stings are created
manpuiated ttrough the and
ttlsvalue cant be changed. sting clas Java,, asting is imnutable
Tn
KAh
losgest ekerment in an aray
þuic da Maun
Systernsut. fsontc
3
Lorget: "+ may);
3
3 Find 4he dublicate eleantin an arrat
imþort java. uil- Anruyi)
Inpot java, util List;
þubic clas Main
public stecie. Listh< Irteger> Daicates (int LJ crr)
Ist <Integer> hiatn = ew Aragist<0)
intn= a.legthj
fox (inti=o,ien; tt)
int index =
arr Cinde]0CiJ7-^
t=n,
3
cw main.
addi
Yetum main,
3
List <Integer> =
ist
Desicates (aor);
for(inti: Gst) £
System-out: þint-i+" ");
MainC) S
Sgsten.ut- pint C"Con stauctor);
þublc stedic vid main(stsingC arg
Main ob= Meus
MainC,
Paranetesisc
liifaut ConstactosAconstuctos hat has craun ete%is J<nsus
foraneten2edcnstouctos.
þuhlic clas Main
String nanej
int ta,
Main Csting nane int
id)£
this. nune = naune
his. id =id',
þublic stete. void main (Sting C3
arg)
Main obj =
neLe MainC"Sonjeet"
);
3
coPg Constuctos T+ isa Constuctos that wses objet ot
Same cay that is cued to copy data.
þublic cdas Main
Stong nanej
iht ic)
Main(Stsing nane int id)
fhis. nome = name,
3
this.id= id}
Main CMain obi)
this.name =
objrane)
thic. id= obj- id
3
þubic static void main Cstsing CIargs)s
Man s
oh Des Man Sonjeet
Man
b > Deus Main(objJ,
3
orth
Can aclassinemert muHHeintefaces in Joun, Brain
Kande.
he isteerfaces in Jova is a mechanism to ochiee ahstaction.
hese can beony chstract nethod in ia (bterfaces io Java,
hot the methodbody is wed to oche ubto I00. abstrac
T
þalic nt hello)
3
)ohataxe the benttts of inheritace
The beni-fHs inheotance in Java are:
Ttalss Csdereusabi Gty.
i)t halþs us fo facibtate boymosbhisn
iT+ helbs us to maintatn rde.
ivA Ustd to achieve ahstcion,
)iA used to make Cude flexide
s
Can aclass inhe-from mulile classes injava?
No, owa
doesnit clous
clouses the mtile inhentance thmugh
because uutipatt nheitence creates on ogbiqity
4) Ohat is anexCebion in
In JoVa, EXception is Java
n
unsonted or
ohich occurz dusing wexpected evet;,
the exe cution of a paogrom,
imehat dispts 4he omalflou e ie, atYcn
lnsthuctions. the
kcepions Can be Caught andþrogan's h
PrOgram. ohen an
ekcebtions hondled the
an object This object- oCcus ustthin amethd it Creates
Cotains infosmotion iscalled tthe execchtion chject.
and desvdption theobyut the KcebtionSuch os H
rogsan shentthe
Srogro b exception and the stete odthe nme
ekcaption occurred. the
Explain
tte fr-catch blyck.
7oy sck is used to enclose
Xcestion. T+ is must be he codathat might throu
excefin has eeuYedtoin any wed wthin the
stategent nethd. Tf n
Yest of the stetemest otty
od he blsck wi
| not be brekthen,
exeuted.
Catch bsck in Java is used
declasing the type to handle the excebtion
excestiog within by
n
object that clas. The codch the þarameter ond
ty 6lock. bok mut be oftesthe
þublicclas Man
puble stetic void main
(string CIarg)
Systen- tut þrirt(3/0);
Cotch (Arstthnetcbxcepti
on e)
System tut. reinttn
C'Excestion: e. getmessagd),
3
3
WhtisGoau bluck. and t's se inJaua!
Tinaly blrek in Jaun is a blck used to exe cute
ode such as cosihg the imortant
socecs, etc. connection,setarha the Atm
Tt ia eKeuted e ithcr it excestiion hadled or iu
Kception ls not handl ed.
A Conteiny the ecessar
that isneed to be þrhted regordless oj Statem ortt
Os nat. 4heexebtion occ
þublc cla Main
þublic steicvd mainCsting 3
ag)
Systemt. pint C"D:3/o);
3
Catth
CAnthmetieExcestin obj
Siystem rut.
)
þsht("Message:"obj-
Cinaly
getessage0)
Systemnt.-prhtSomy! Not valuattd"J,
3
4oht is statiekespususd jaua
The stetic in
kesuwod in java
ent The stetc i mainy sed-for nemong nanagem
keywtSd
aclass. Tf a vanable belong to the class nottothe
or
methud is instas ce o
keyursd thes on Gneone CopH quaified with the statc
It Can be ull
cbicck the cla be genesoted for o
apied al the
to blrks,veneblo,
hethds and classes.
the se
hat is
sfatic kegwdinJaua!
stetic keqwod in Tova. is used tomake
VOablestetic netusdu ond
static.bk, static
nse
qualiied with the keyurodstetie clos. TRe cleenthat
bt the instence ceonnts. TA' majosare knousnas doys eleomants
feouresaro to sot
t5)Uhatis methcd oversdirg
is Taua!
Method Olesiding is cne
of the ways bt ohich oecan
Oneie time peynocbhism in Java
is a
tthot
alos featue
methsds
acub claus to bypvide
asþecficimplemontadion
already
nethod io beth prouided by its Caperclass. ohena
suborlos Cndsubclas hos some
(method nome, Siynatns
etumtybe, Same
Sabclas method is basametes)a then
tnethoe. saidto havo oveided tHhe the
kubedous
fublic fuy)
þalic fu ()
clans B cxtends AS
þullic
fnlo/l oNerided method.
þahic fun 2()
6
Housis melhsd ovesradirg ached in Jawa
Method oNeslgacing
nane cus
diffeset method to haUe -fhe sane
used tobt achieve"Comple-fime'
teit sgnatues to difesentiete them. Tt is
boypm usbisn in
þubc closs MoinS Java.
þuklic void func)
u cid
þasic fun nta)
public
sttic vsid main( stingDargs)
fusC);
fun Cs
ADha
A ika package in Java?
java backage
þackage is
isaau goupoqf simlar types classes irtesfaces ond
ckagcs. Puckags in jcwa Con be Coteoqanted
ackag. uses dofned intuo typas, but-n
ond
, lang package.. heYe many þackages such au
ust,jauax | Swng, not io, util, cqLietc. Hcan
we tohcido claisos 4at are be used to
hy oe fachages hbtt.
The backage ied n Tava
has so mang
adwagtege, co, itis used hJ ua.
us
to srevet the nene
diffesest husposes. cecs
betweentuuo
They
aret
classes fus
) helþs us
fo Seusch
U) witththohelks ot access fos asþecilied clos Vey ec
inside os shecfios ithelh cto contao|
otside þackage the access
wt
49)
is a verH qurd
fos data-idng
hat is an
abstract method in java
The abstsact method
Classes or intofoces.isMehods
uedfor cseing bluebsitsfos
are de-fned
dorst þovide the insdenestdion. but thes e metthudy
beimplesmented Abstract method can
ing subclases orcloses
he intestaces. These melhud oxe that im eret
nt
Sabclases esþonaibility becausesometimes geffered to
sbecified #hey have ne inplementation
inhe supes cdas.
bYovide a mettod defhatiog. Aibclas must ouesido thento
abstact clas Hello &
Void þritttC)