0% found this document useful (0 votes)
3 views

Java_Notes

The document provides an overview of programming concepts such as variables, data types, conditional statements, loops, and arrays in Java. It includes examples of using if-else statements, switch statements, and various types of loops to control program flow based on user input. Additionally, it explains how to work with arrays and manipulate their elements, emphasizing the importance of syntax and logical operations.

Uploaded by

Om Mundlik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java_Notes

The document provides an overview of programming concepts such as variables, data types, conditional statements, loops, and arrays in Java. It includes examples of using if-else statements, switch statements, and various types of loops to control program flow based on user input. Additionally, it explains how to work with arrays and manipulate their elements, emphasizing the importance of syntax and logical operations.

Uploaded by

Om Mundlik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

AND

l9gical opera bor Page No.:


W

YOUVA

Date:

Wariable 'contginerr haen ting memory


that SOme value.
Dalatype ype ot olata being sovc in a
Variable.
pertonm operatin
on opemands!
bake
To bake nput Jmom, useY aaan inteqer
nput
Pile bo inpor bb syitaxi
inport java.util.Scánncz;
lass
publie - . f
Soanner 9 = Neo Seanner (System ins
Sout (''Enter' the numbey "yi
h= Sc.nexIntC

+Coni ionad satemenhs


shaterye
Syntox: it (Condi bion ) f
blockineot coe
1f1 cendition o
evaluates to teue
co cle
then block/ line eXelute
a, f- else hateyenr
|Page No.:
YOUVA
Date:

block of code.
else
(blo ek otf Code
1 Cond:tion evaluakesexecuted,
to other
then block is
else block is executed.

Example: that checksiobethe


peson cligible for vohing.

System:out spintln (Ente you age.)


ScanneY eo Sconner 'Systern.in
SC
int agese-nex} Int()
1f(oe |8)
System-ou!printIn"You ane eligb
fo voting ")
slsef
Systemout grintlo"You ane not
eligible voBing ")

Above progmam accepts age of


user S an ipput an chedk bhethe
th¡n
evaludtes oybo gual to
euLe i.e
age is neaten than. or cquals o 13
Page No. YOUVA

Date:

then it qives output You anenot éhgible


voting Omeroise ""You ne
fon vo ting".
*-elsei-elge Staleren} to testnultple
Condition cvaluate[
Çordliions, any block
ther lornegpond Ing condition
ode Executeo. T al
evaluates to False non f the
e
sEd
evaluates. tobeue then
Conclition also
executd. 1t is
else block S
kno usn as stattmen t.
Snta' f (Conditon.
II blodls ot code
else if (ondition 2)
|| Block of cade,
else it (cndition 3)
Boc bt code

(condition )
eseit
Alock of cooe
I/
ése tlock otcode
M T

Page No.

Date: YOUVA

any, Conotion is evalua ted to


têue then block
that per bicugr wise
is execsfeod ypher
the 'else block f Code xeculeo.
O Loddey if else is used to check
uultiple COndlitions.
Cxanple rognambhat accept manks
of stuclen anc aicplay rades.
Sop ("Eoter manks "),
SoanneY SC = Se.pextFloaB (O;
tloat marks manks K3S)
t (o marks >o &
ezstesnoutpmintin( " Fail ":
else if( mauy ks= 95 &a marks<so)
9ystem. out-printlo (" qrade ")
if(marks50G mar ks <6s)
,System.out -pintln ("qmacle ")
clse if(marks = 65 &morks < 80)
System.outrinth ("B gnae "),
else, if Cmar ks=go)
System.outprintln ( "AqmageJ)
M [ W
Page No. YOUVA

Date:

Abee fPoqmoman acçe pts Marks o a


tualnt
jes belwech 0bo to bsthen it
"Fail"2t mamke ies be tuoeen 35 to
betoeen.
S0 thea it prints "o'rade ", 1f make
ies be boeen So to 5 then, it porints
9r ade".I maxksies
65 to 80 theni pints. bebween
f
prints "B qrade
Mar k lies be
betoeeh 80 bolod
pr ints A, q9 aole 1)
tertotSe 1f USeY
enters
0a neater
invalc mar k i.et, less th¡n
I:e
than 100 then it
Iwalid input"
P4 gtatement insiode another i
statenent
statement.
alled Desteo

io am, bo ind oudamges t


amon thmeekNumbeys
SOP ("Eoex Numbery "*
Soanner Sere ne Saanne (System.in):.
S0pEntex 0 Number
SCannex Sca eusscanntrlSystem.a
YOUVA
bate

Sop C'Enten qre NamberSeanne


: ")).(Sytem in
SCanney Sc3 = Deuo
jnt,c Se2 ngxtIntO
Syctem.out print ln(a'is greaBe
else
Syatem aab prinln( ka"isgreaBes
else
if(b¢)
Sout (b+ is qreatest);
else
Sout (C+ greaBest),;

he m ANing
IS Conposed 3 parts. he te h¥ny
consis bs o 3 operandls it is sed to
oper akan boolean exressin : he opemato decick
eyaluate
ohich alue oill be assiantd to the variable.
It Can be uicd insteas o i-elae. nakey the
code uch moneeasyi meadable Qno shont
aageaniate
vanale
caba type vamable Cordition ? Expnessioni exprË AS
on
ig corcl'tion evaluates bo, tee then walue o
Crp.1 is asigned bo the vaniable, otheNo0se value ofex
21s asgneol to the vaniabe.
W T

YOUVA
| Date:

*Soibglatemen:
usebooheck
Condibions like if-elseif-else. sta mulbiple
enent.
instead of using any else i stotcroents,
Sioitb statement canbe used.
Syatas'
aiteh (expresion)
Case
lcode
beeak;
Case vaue '2::

beeak :

detault

tey to match the valae ot


Cpnession ithb specif iel CaseS.
sybtax, eeSS onges atched to ary
specified casé volue (voluel,value 2,
theheomesporing block of coceis
éxecute.Bneak key s bia
irakes
Suuit ch Statement.
PageNo.
YOUVA
Date

match i h an
expression does hot letault blocki
specitied alue te
Cxecated.
roamami"s e h e )
Stafihe Seenher
-println( nEnter No"))
yctem.out new' Scanney(SysBemiin
Int numse' sc.nextLnt0
JCannem
Stoitchlnum)

Case1
Sop ('sunclay
breaki
Case
Sop C"'Mon day ")
becak

Case 7
Sop ("Satuclay"),;
bsea k
detaulti
Sor ("ovalid Ahpu")
Abo ve poroqr a
inhu
accepts ' on
if hunbe
nteger
entereo by,
useYTS hen pog ints unglay
numbey ent ere
2then its Monclay
usEN Enters
Lohichis
erend
Jess
studen

than
number
is
Page No.:
YOUVA
Date:

eaten than 7 then it goinhs"volid ipot


*LO0PAg6 gTATEMENT:
Loping catcments erecutes, he block of tok
repeatedly until a ondit ion iyevaltes to teve
LDo Dhile
Dkile loop

hile ( Condibion)
t executes the black ot11 isolso
Condition evaluates to t& ue 1+
called as exit conleaocp. becatuse, ît
checks the condIion atler block ot code
is executed.1 Condition eyaluates to Fose
then lock of code is exeeuted eee.atleast

Paanam bo
to
,print
the hum
num bers
humber
ber enterc
by User
T
Syctem.out-prints C" nter, he buob
Scanner Sc neo Sconner(System.n)
se hextIntO;
nt = 1;
clo
YCUVh

do
Sop Ci);

tobile (ikn)) prints numberns rom


Abave
1 to henr entevecl hy use
negativ. Nune
user enters ooes executed
*r. ter block ot Conalition eyaluat
Once even thoygh becauce, do-ohile loop
to alse eXEuting
check Conalit iop often
he code.
abhhile (oop:
Syala ie CCondi tion)

Systeco. but. printlo Please Enter anumb


Seanner.sc =heo Scannt (SysBeminang
Aehextint
nESCnxtnt)

Syslemgu.printin(i);
in! numbedJom
bo no. e n e e d
|Page ho.
YOUVA
|0ate 23-|-24

Hhile loop is alsocatled


entey Contot Loop beaUsE it axecutes
the oclk cocle bot oflerr
checking the ondi bion 1t he
Conition ealuatesto bEuei then
hothingis executel from the hlock

cxe Cutes he
statement foroop
unkil Conalikio n evaluntes
teue. As qs the Condibion
Satified the
the for lbop
temninates.
Gor (inibialiealisn i Consltion; ineaemenolec
eement)

blact o cocde
abovésyntax inibializationvaniable.
repnesernts Initializatlon
Condibiorn epsents an ¬xpmess ion thal,
False.
Inceement dectement vaniable
also cone in ianatune ot To loop.
for loap executes ie block ot code nti|)
ondition evaluates
-ion Evalual,es bo Talse then cursoy
Cocle lock
Page o.

Dete
YOUVA

vamigblg
Atler Inikializalsion
place, Condition s evalua teosue
erECuted. Ather
then
Condion cualuates is oleceement
block ot Code
that Incsfment place. Jhen conditiog
variable takes nt its
IS, cvaluatedl
execuHon aga bl»ck vano
alecsemento
ondhen inesemenbrepeat edly Hlt condi tin
Fakes tlace to
evaluates
hummbeps.
9um be pntesed y se

javq- util)
Class oanJavg
publk statie void nain (sing Iga
System.out.printn" nterr a' number
Soanner sc new.SCanner ystem in
int,n sC-ncxtIntl),
tot Cint i=1 iKDtt)
ycten out printdn(oi );
Page No.:
YOUVA
Date:

In enter
above example ausen
co
1 to
anumberr. Nurnben.st
numbe.nL9
the
oyuSer (ne pe in fed.

Synka
tan(olalatype varia ble : Callec biorn)

<syntax ollection hepnESents


amay, O |ist gr tome map
Sel thash05. eleoents itens af same
cBalabipe. Vasmiable mepmesents ipalividual
iemelenent s liec tien hat has
Sam e ala ta bype. One by One elementitem
ne aoed Naiable 5eqienialy
Block oteode is executeo af tea unil
thelátti elepent orr item a collection
qets stoned to *vanioble.

int numAaylo,20,50; 40, so


or Cint i FOi=numAnmgyfengbti it+)
siälem.aut.print inloam Amayi);
Pzge Ne

Dste

allecion elements o
Atsay ;: Collection
Sane dababype: cseated using
In ava Q220y S
Syntax: databype [I@
ollooing akle ncu
olatatypeLvaniable
Iside
size mepresents
Ta above synstax to be mesent in
Dumbe of elements
emple-Steing(s hame,s
Steingl];
o
ceafeo
ammay names' is delementsit
Penre, Maxioum
detaultsize g ie, anyay. Num ber of eleme
an
Can be stare in an amay js known a:
çnts stomedn
length ofan amay
elepents/iems bo aniuma
Syntax to add (index] valuej
amayName
mepnesents an index hunben
Hee index
which start tonom Zer.
AgyEsLo] =
asnmyNamelder]: value
A1

piyusha
Sayali
hames 21 11 Gautamn }I!

In aboue example Piyusha 'is inst


lement of havina
Jnolex humbeY O.Sayal;" is second
te

elemenb avray Luith nder num be


I: 'Saut omi"is hird eement,sith
4h element in an Jhen
ArayndexOutöp bound
bound Epept ioh gct urs
hecause, maximum,3 elements J; tetg can
bestoreol in above araN.
Updaling eletgenttem in ansr ay '
leisg anmayNamelinde] =value;
Rajkuo'"i
Tn 'above gle Raikussa",yalue is
CSsia bed to the thio' eleroent ikeio s n
anray having inclex umbesr 2. Jreplas
an existing vaue if aleady
:.e can declaYe an
an amay with oetaul
values using ollopnggyrtox
lelerent, L,element 2,
datatpe(T vayiate
exatbple clement 3,..
eing LlnameL piyusha Sayali
Gaubam Rajkuar
loping theough an a££ay

ng haes Ajay, Bhushan Shiyon


Jom,int i0,i< enames.engthii++)

Systcen.ort pintln (names(i]);


M

Page No.
YOUVA
Date

Uina Jor each Loop


tor Steing name names

mintin Crame);
oystemeui
Achantages.
opbimisation Sont alata/en
"HeCaadecan
netsive
Enents etiiently.
random Access aaba/ item I:elehmer
con oeteive any: position.
He atspecific inolex.
Jocatead
Disadvantag
iSize jsTinc :
only fixesizei of
Ne con store anmay. The stze ot
of ay
elements in an atrun tlme Colynamicing
does not qmow froblem linkecis
list
To solue this
use
Single dimensional onay (Nateix)
24 Malti dlimengiono amoy
+ayyltt -dimensional amay h such
*aMwti
Dt, is an amray of aray columm
Cue, daba is'stomedi in now and
basec inden.
a<so knoon as mateix.
dababype
Page No.:

Date: vOUVA

Syn ta '-.
datalye rooscolumrs varriable =ndaba typ< CJtJ:
Ehample '
int 2] [3J a: neo Int[JJ:
Hereuse have Ceeated bwo oliensien
al aay hauing ts0 -vBws and a column
ohich epresents 2x3 matx
* Agslaning element to multidlimeng ional
ammay
We can Qssign value bo
to malai using
rou anol eolunn, tnolex
any LrooIdex] columnIrex] yalue :
Example
We have assiqnieaalue s to mateix
postion having mow inge n Lcoumn index.
e sccond aw & secondelumn.
entigteoping Hheouah myulbidimentional ma
He ean iterabe toop throuah 2 olimen,
-sional arnay using nestecl ov loop.
As chao below. Whenenepmesents mow
inae x anod J' vepresens olu mnindeK.
Page No
Date:

o ( jes int i o i s column count Jt+)


ystem.oub print (anrayiJliJ);
System.outgmintn (" ");

Ne can declame and iotia lize an armayoab


single line as tot16uoS.
int t mateix .
1,2,3ti
{4,S, 6

G WAP to add too mateices.

12,2, 2

intJJ
{4,4, 4
f4, 4, 43
Page No.:
YOUVA
Date

tor (int jojKeelunt eoant jit+)


Ssten.out. prcint(enti1tJ, 6C:JLa):
8ystem outprin tin(" ");
Outpubi

Liss: e e can aod elemenl inbo he lis b cb


un Hme, Size o} ist qno usS
dynami Cally okn use add newelements
to the Lisb
Sntan Example ' List <Steing } names neo
ist<);
names. aol Sukanya)
Soyali ")
namesadoaikuoar"
ames.emove Sukanyo
nomes. Size ( ) //Gives numbh GinE646
MT wT
Page No.:

Date:
YOUVA

Nap Staing Int map


napput"aukonya", tbo),
Map.put("Sayal; ; 99);
hap-put (" Raikuusar " g8)) chamactens
Steing is a sEquence
prouides lot !
h pakage
avapenfom openrations On steings
vouvA

Class is collection o objects:.


Class is Q
Class is a blueprint
kluepaist
uses ole ined
of
of obijects. elatatype.
Each class prsents, an entiay: which is
a ncl Wonld obect Instahce varables
of class nepesent attsibutes o an entity
Example
Nafme Acldmes)

(Deignatio Employe
(emait 7Sala
(Dept)
Cla tengloycr
Lora idi
Ssing
StEina

deot
clouble _alay
ssing sesint atjon)
Class Test
fublic static void tpa in (Steing
Syntan for userdefine asabype iery clas
classname yariable: heuo clasnamelJ!
Date

Emplorce empti neo Employee ();.


empi
empi.name " Sukan ya
Emp. adyess "Lotur"
neto Enplayce0}
Employee cnpa
Cp2.ame:Ruchi

a instance of a clas
Object is£mployee is a clasS Ghd
Coject' example
Th abey ohjects of that elass
enpi, empa
Somé cxp. as of class.

Consteucbor s o funchion lme thod havno


hame olass hame.
iNo any netun,type.
TEis. invoked /called eoben obiect oÂ
class is seated.
be
Jhe pocESS
instanciation of a clas

Exanple
Class Employee
int i i
staing
Page No
YOUVA
Date.

Emdloyee Gint
this id-i
i, &teing n, Steing
this StIrgn)
this Steha: e

elass Test.

pyblic statie vod main(Stsig[1 anas)


Employe emplnew Emplayee (t,"'Ruthi'
"g8T654421
Empdoye empnes Employee(2
765432| 294")

Here, froployee iS a elass having


ateibutes id, name, Qontact. we haVe ceeated
tonst-éuctor oith thtee parameten's uhose
values ane going to, be assined to
instance Naniábles td, name, Contact
vespcetive<y, Using "hi_" key womd. "Hhis"
keywomd tnoiates an instoaDcelobiett 1 of
he class. Jhen e haye ceeated (est class.
Ne have cseated buoo cbjects oeoployee
class by passing untime parametens to
the constsuctor.
Non parametem Lse const &úctor is
Qalled default congteuctog: ohich
an.be
-ent
implemented as pem
pen usenr'neguiem

You might also like