You are on page 1of 23

Tuoo types of aPplientions con be deve lopeal using java

PhOaban
gland Alone Applieetion :Thad nunse
mwm mwmmwnmm on a
Single machine.
web Aprlicoian That hung on 9evenal machines.

Sthuctune af JAVA Pnogtiam-


The guckube of Java pboabam Can be ghodn
in he telloding oliagham-
Documentafion SecHon 9agpesles
Package Sechion_ Opianal
Inpobt 9talement- ophiona

Intasface DeAnikion Opional


Useh-deines alas Defriion optiona
Main melhod class Deknihon Essential

Documentation Section-
T deschibes a bnief descbiphon about tha
Phogpam I4 Can be bitten using eommetline.
7(Single Cotmment Line)
/ . 4 / (Mali Cemmet Line)

PackogeSection
T deHnes a 9et ef easses and melhods
(fancions) hat can be used in a Pbeaham asohen

hequined
. Impobt lalemenH
T is used to include an aseh-deltned packag
in he Phegnol
.Intenface Deiniisn
T an atenaive of mulfiple inhenitence.
Useh-defines elasg De-intton
denes he behaviouh and eha.
ebistics (vasiables) ol on abject ahan
Main Methes clas
T detnes he object ob te
useh-
deied olass, T is the stabHng Point of
dny java
Pharom

JAVA Development Kat (JDK)*


JDK is a plaffobm of a 9el o.f l o
commands. That is used sMpile and un javo
Pheghan
Vabious tols abe

javaa- T is Java Compileh, 1t eCompies the soune


Code and Con vebls it into Byle codes (elas1 fle).

i java 4 s Java TntenpheBen. + Conveis ByBe csle


to Machine Cde cexe Ale).
ii aapplebviedeb-
mmw
T bung Applet Pnegban, al4haut usig a
web bhoses
1ab-T i TAVA Debuagen.T is used to check ad
debug the enbobg.

JAVA Vintual Machine (JVm):-


Java compilen(javoac) phoduces an
Temid diel Code knoan as Byle ade on a Machine
ha does not exisBs,+his mechine is called Java
Vintual Machine and i ig exist only nsTdeHe

CoMputen Meneny
9eubce CodeGJau aa CanpileniosubyBe aode &clas

vn Byle Code (.clau:)}Pva nepee-tj Mackine eode (ere


3

Pin-tin9 JAVA
0- values in
Thene ahe tdo me thods o pntnt values in
Java. Ihe methods abe

Dpint)unchon
The pminBt) Funchion is used to phint values
The
nd
nd afe
afHen pnining the Cub3eh Oill bemain in 3o.me
line

Exonple- SysBem.out phin ("Hello")


pinIn()funeionnon-
The prlntnc) uncion is used psint
values and aflen pbintina h e eun3oh aill nansfen
the nex line.

Example- 9ysBem.out pmintln(" Hells");


in JAVA-
LReading values
use iast
While insenting values in Java ,@e
inbul4 elasses o Connec+he phogban t h keyboabd
and hen se Some in built unctions to bead values
Phom 4e keyboahd
inbuiH alasses abe
he vabious
Ycannen, 2) Dala Tnput 9team 3) Buffehed Readeh
Vabious inbuil+-unchons of 4he anneh
Scanneh
olasses abe
nextBooleant) to bend tnue() oh talse (o)values.
-> Uae
values.
nerd Byle )> Use to head 1 byBe integan
to bytes integen values
head 2
)netShont)UR values.
Use to ead 4 byBes intege
next Intt)->
nexLongt)> Use to head ? bytes in+egen values.
to head floating-point valaes.
hexd Floal()> Use vake
nex+Double()-> Use te head double phecision Ploairg Poiot
to hend
Chabacte ob 9hmg values.
Use
ne xtLinet) ckaraclen Hlaut dklespace
uSe to head 9thirg on

next )
Dalotypeg
Dalatype nefens o ype o value
to a van iable

.Qlaaikcalieng
DATAT YPE
Pnemitive/Basic Denived
Usen-deined
Fundamental
Elementar Aproy clasg
Funchion indenae
Numeni .
Non-imebie
Inlegen Floahng ehaacten Boolean
Poin+/ Rea cha
byle boolean
ghebd Ploa
in dleuble
\on

Paimihve Dalakype
byle
MwnmmmeDaBatre: A byle dataty pe is usel o declae
an integen vaniable hat needs
byBe of memory
The ange of value tha- can be
Stoned i - t o +-1).
Syntan byte vabiabe-name = value;
Example- byle d-5;
5

9honDaiatype A shol dalatype ie uses to


declahe Can
integen vahiable hat needp 2 byBes of
Memob

he ange of value ha an be
sBoed is-5 to +(2).
ymak shent vahiable-ha =value;
Exanple- ghent a- 50;

indatape An int datatype is ued to decdae


an ineaen vahiable 4had needs 4 byes B memoby
memoby.
Ihe nonge o value tha Can be
stobed is3 l-1).
.Synhar-int vabiable-name =value;
Example-int a 2500

ong alolype: A on datatype s ued to declabe


vaniable ha needs 3 byBes o memepy
oHegen
Ihe hange of value tha can be
slohed -3 o +-1)
yniax lon vapiable-nahe = value;

Exasple long a-5o00

Ploa- Datatype: A Plead datatype is usedto declase


umbe. thaB neels
Single phe cisionloaing-poin
4 byes
memo
The nange of value+hat ean be
9tohed i -34 to + (23-1).
Sy ntax Ploaf vabiable -name =
value
Exanple- float a 5-593;
double Dadabype: A deuble dadotype.. ix used tod
double Ploating-point numbeb ha needg
8byes of Memoby

The mage ovalue Can be


stoned is -2o +(-).
.9yndax double vabiable -name= value;
Evanple - double
a-5.9652399;
ehab Datatype: A ahah datotype is usedl t deda
a single chahaclon value thal neesls 2 yes ofmeMeh

The hane of valae fhak can be


So
is-5 to +(25-1).
Syntax eha vabiable -name =Nalue
RaMpie chan a= "A';

boolean Dalajpe: A berolean datatype is used o-

declane a boslean vabiable thal needs i bit oMeMoh

The value is eitkoh o On 1.

tax- boolean vabiable-name =Valuej


e Exaple- boolean
Class
A class is an useh. dekncd datatype, 4haB is
usedo defne the chanaclenisie (vaniable/ Projchtio)
andhe behaviouh uncion tas) of an
objec
.yndax
elass olass-name
Vaiable declanation/instialization
melhod definuton

Komple
elass A

in a=l0
void fnO
SysHem.out phid (a)

DObjecl
An sbject ig an insBance of a class. j i s a
Hhig ha must have he folloain 3 phepenties
Tdenh Acadion value/1deny uiue idanhil)
Look/ Face|Feel
DBehavioun/ Fuchon
yntox elasoname objel-name = ned constnuctob(),

Exole- A obj ne ACO;

lass Membeb
The vaniable and the method declabed on defined
elass is caled Class Membeh.
OHhin a
The vaniables ahe ealled Membes vaniable/ Tnslace
Vanlable
The melhods ahe called Member melkods.
Exole-
Class A

n a-1o; II
void Membeb/Tnslance vanable
h) UMembeh Methos
Syslen.out. pnind (a); oass Mo

Conshacton Ohjecl Conolhaclon


A CsBhaclon is a
n memben melhod thad is
9pecial memben
used fo
o a elasg. nutialize ee
initialize te

Chabaslenisie/Propenhies
has Some name as hat of class nane.
T has
hetuntype.
ho

i Ts automalically invokedCalle) a hen e deie


tHe objec

.Qlasificadion
Consthactoh
Delault
Con slnacto rahamelenized
onsthactot Con gthaclos

Delaul Const badob, A eonghacBon having


pabaneensg is Calles Delaul Consthacton
no
abgunehd
Exomple-
ampo Java. io.*
class Dcong

mt ab.e,
Dcons (Delaull Cohsheaclo
a-10;
b20
atb
9y3em.aul paih (e);

elasy DelaultConstnactoh
public slatie void main(
9thing ahgst1)
Dcons obj =nea Deonst);/* T antoaically call
the delault Conshach

PanamelenizasQonsdthacte: A onslhaclon hairg angumesi


oh
pabameleng is Calles ahameleni zed Congthacton.
Exanple
impont javaio.*
olas Pcons
aybe
Pcens(int pinta) I Pabamelen' zed Consthactob
a-P
C= atb
9ysem.out phlnt ();

clasg Panameenizes Congiacto


public sdic void main(Strig angti)
eall
Peons ebj =
ned PconyCio,2o); /T4 aill
Panamelerited
Congthacden/
3
A panamelenized Conglbar
ngBnact
CopyCopy ConsthacBon: olkon obi
anotkon
dhieh De can ecpy one objeck inBo
objel i
Called Copy ConsthacBoh.

Example-
impobt Java.ío.
elass cons

i l a,b onstnaclm
econs (inl Pinda) !/ Pahameleized

b-
ConsBhacfey
Cons Ccens ob) // Gy
a-ob.a
Obb
3
void display ()

Syutam out pnint("n a= "4g);


9ystem o ut phint ("\n b "4)

3
class Capy Cansthacbh
public staic void main (Shing agsti
Ccons oji -hedd Ccons Cio,20) J il) Call
Pahameleited Gonshy
ebjd. displayey
Caons abj- ne Ccons(obj 4); /*Ikoill call ay
objo. alisphy ) Conslhoelo
1
Constnucton Ovenloadira/ Ovonloade d Conslnuctob
Usina tdo On mone
Consinuston-
havna Plenen 9thuclune
hav con9tnucton in clasg
is oalled
a

Constnucton Ovenloadi
Example- Same as
Copy Congtntuctob

Mehad Ovenloadng Funchon Oven


Using oo
loading:-
on mobe melhosg in
Same name a elass hang
an
diffenent suctuhe is called Melhad
ovenloadina
Fxample-
impon Java.1o.*
elass Ove
in ab
Void fnCnt p,int a)

a b
void Pn ()

Systemout.prind (o);
Sy 3Hem.ou.phint( b);

elass MO
publie otatic void ManStning angstI)

Oven abj= ned Oven);


obj. fn( to,2o)
obj. fn);
Inhenience
Inhenilexe is he abili to beuse 4ke
it is called
code.
Hence, Reusabilit. T shanes tle
phopenties
of elass/ elasses ino anol hen
olass/elasse3.
The ele class (g) abe calle Base
elas
Supen elass Panen Qlas. The neo elass(s) a e called
Derived elass/ 9ub elas/ ehild alass.

ypes o Inheitence
TNHERITENCE
Sindle MuHiple Hybhid
Tohebrfece In henitence Inhenitexe
Multilevel Heenanchicak
Inhenidence Inhehitence

SyntaX
classhile_elas.-name exdendo abent_class-nome

IICede

Exomple-
class B exends A
H Code
Single Tnhenitenee ingle Tnhenideme nelen
Mul

chss hom an old olasg.


ne
Q h e a t

cheaing
Cheaing a
hich

Pahenl olas
(A) Base clas/
elend
6)Debived oloss/child olass

Example-
Impen Java.io. X
elass A
mpo
class
indin ab;
void inpuc)

a-do, b=20,

class B exdends A clas

void Cal ()

C atb
Void di9play) clas

9ystem outphint (e),

closs 31
clas
public sBalic voiw
cunining angsL1?
B obj=ne B
obj.inpudO ebj.cale obj. disphyt
15
DMul
.Jklevel nhendence-Muiklevel Inhen.lence nefeng to
Oheai
Cheahng a ed clagg Phom an ald olagg fnom

ahich
hich toe
ode Can Cheale nolhen ne cla99 and 90
9o on
on
means heales a aenehaion ot
elames
ABase lars
enlonds
(Base of e/ Denivecd of A
elends
Denived elasy
Exonple
npobt Java.io.*
clase A

int ab
void inpul)

a-10 b-20

class B exdendg A

in C
vod Qal C)

C atb

clase exlends
void display t)
System.esupnid (e);

class mu
publie sBaic void maun(Stin ahgst1

obj.input(0
obj.cal
obj.displo;
Malkple Inhehitence Java doesnt SuPPont

Inkenitence. Thene is an aHennafive sf h


Multple
Inkebitence hich is called TntenFace.

Base das (A) (B) Base elass


exdendo

Denived olass

Hienabchioal In kenidence Hienanekical Inheniteme


hefebs o shabing tHe phopenhies of a base class
in 4s vaioug ehild olass es.

(A Base cass
exiends extends

Deni vesd elass Debi ved c)as3

Example-
mpot java.io.k)
olass A

in ab
void inpuBy
a-i6 b - 20,

elas B edends A

void Pn)

e atb

3 Syslem.out pninte);
class C extends A
int d
void fnC)

da-b
System.out phint (d
1
class H1

publie static void main g4hing ahac

B bji ne BC
obj. input0;
obj fn0
C obj9 n e e ) ;
oy9 inpuk U
ob 2. fn (

Hybnid Inhenidence - Hbmid TnheniHence is 4he


Combination ot any ob mehe othen inhe»itences.

Single Inhenitence

Hienahchical Tnheieme

D
malliple Tkenidence

Mul4label Tnkenitence

l a e Memben:
of a elass con be accesyed
Staie Membens
thout cheaing an object

That mean9 e can acces tle stahie Membee


le class nam.
dibectly by
Memben
Syrtax- OlassName. 9taie
The membengof he elass (Vahiables ond method-
The alatie.
Must be dekne aith le key@ehd
h e 9aic vabiable? abe clles Class vaniable
called Class Melkod.
and e sBatie melhods abe
xample-
mpont Java. to.k*
elass A

staie int a;// 94aie vakiable ohlass Vahial


slatie void fno gBkic Meshed oh
vahiable
a-1o
Syshen.outppint a)

elass B
publie iaie void mainnig angs[1)
A.n)

Method Ovenpidirg
Methed Ovebniding nefens to defining aMe
o a base elass in ts debived elasy
havina 9ome
stnuctune
Example-
impont Java.io.*
elass A

voi isplay t)

3yslem.out-pbint (a);
3
olass B exlendo A

void display / Methed ovennidia


3 System.out-phin+(a);
Nofe To call floot angunen use fn1-25P);
elass Mo

Dubliopubl statie void


mainbing aast1)
B ebj-ned BC;
obj. display 0;

Fina Vaniable -
he vabiable ahose value eannot be chonged
in any olass i alled Final Vaniable.

T makes o vabiable Constanm


The Keydobdfinal is ugedto make a
vahiable Constont vaniable.

Example- inal float pi-3.14

Final Method
A method hieh Canhot be ovebhide in Hs
denived elass is Called Final Method

T pheteads Melhod Ovet


cing
Example-
mpen Java. o.*
elass A
Pinal vsid n ) / Ana me4ked
Syslem.oul.pbint(" Hello );

3
elass B enlenda A
void n) // Melhod ovehhidina
yslem.out prind ("DonH);
olass Fm
publio statie void main(S4irg angsti)
B obj ned BO;
Can+ Ovende
obj-Pnu; / Enhob !

Final Class
A elass ohich Qannot be inhenides Punhes
alled Fina Clas9
The KoyDond inal is used+o make aFial
olass
Example
Anal chs9 A/ Final clas

clasn B exlends A/Enoh Cat inkeil edasn A

Ctackage-
n Java, packages ane te ay
8mouping avaie of elasgeg eh intenfa ces gea

.Qlassificalion-
Packoge
ubnay/ in-buutt/ Usen defined packaae
Java APT Ahckoge/
Syglem dekned
21

LwaAP1 Package/.Sslem defined Packoge


Java AP1 pnovi des a lange umben of
classes anouped
gnouped into direrent packages acconding to
foncionality . Follo@ing aue shoDs Some elasseg
Ai4h hein packages.

Main
| Java
package

(util) lay)aul) applel Sub

packes

Package Name Contents

Java.10 ConHains inpu-output elasses Pon


Vabious input-oufput methods.
java-uil Conlaing uklit elasses such as

vecton, dale, Seannen ete


Java. laa Contains dasses ahich defines
Premetive Dataype Math Funchiong elk
,
---

Java.adt I4 Centaing classes on


implemen in
naphical intenface like bufton , menw
Dindod
Java.applel J Conteing elasses on Cbeatina Qe
implemenira pplek

Synda to use 4le 9ysHem deßned packaaes

mpo ackagemme. clasyname,

mpon packagerame

E-impoht Java.io.*
Usen defined ackge
ackages 4hese ae chented b P
use On Phognammen is alled Uren- by4le
PacXage ol

Syotanr elassName.java
pac koge. packnge name
publie class elaseName

Body of 4e clagg

.Accessing lackag
impont packogeNomé. elass,
impob packageName. *; l

DiagnaM
D

Paekoge
Paek
A- java

.P. jova
mpon
A Java
package Pack,
public clasy A
publie veid display()
9yslem oul.pmint (Helle Packone ),
23

P Java

impon Java.10*
impopt aeki.*,
elass P

public 9toie veid main( thinA argsti)


A ebj= ned AO
obj. display;

Advaages of Package-
AJava pockage is used do Cataqonies e dasseg
and indenlaces. So +haf, iCan be easily maintained.
Java package phovides access pholecion.
Java Package emeves naming eollition
Java

POP VS. Oop:-

Subjeck Pop Oop


shonl fon Procceduhe Onníented object ophiented Pnegna-
eapamming mnn
Hhoblem Decempose he main poblen Decempoe te main phoblem
Deconpesirin in small pabBs alled kanchon in s mall pais alled okjeck
9Connecton Connecks small papts of 4ke Connecte mall panis ef
o Pants Phegnam by passing param-ite phognam by passin
elens 8. messages
aEphas Enphasizing en hunctions. Emphasizisg en dala
Zing
d'se of In lange Phegbam9, most. Fock ohject condnols dala
data funcHons use alo bal daBa. unden it
Desianarg Empleya Top-Doan apphoack. Eploys Botom-uf oppeack
PPPhea
Dada Thone is ne phopen ay o Pala is Ndden nd
Serunit hiding 4he dada, so dats ishence dota secaniky occws-
ngediue. ,
o a ha- Glsbad dada is shated anog Doto i3 9haned aMon
He funciens in 4e phoaan. bjecls 4ogh manben
unchoA
ceo
PDivae, phetecte
s9peciffen dbsebved.beal publie.
Pablfe
Hod No acces
CtJAVA
Exaple e, FoRTQAN, COBO
the mislakes ha eCan
Can Make
thal
Ebbons ane

sp hong
phoanom
ypes of Ennons hene ane t o pes of ehhe
hor
in Java

Compile- ime ebnot T includes


issing Semicolon s
Use of wdeclabed vabiables.
i Missing bhackelsin elasses and methe

Run-Hime ennonT4 ineludes-


Dividing an indegen by Zeho.
i Acessing an element that is ou o
bounds of Gn anhay

ni Altempting to use a
negative size of
aonay

Eception
An exception is a
Hn condion has is
CsUsed Run-ime enhon in +k pnogn
an
Prognan

whenhe Java i04enpheBen encounden n nho


Cheafe exception objeck ond thh6S

du , 4 2

You might also like