You are on page 1of 25

Soece code fr Cempiled, ach

hen ava
CnediFclual elass put frnto Own otpat tle
ameo to he clasc and ufirg theclas exlenmm

hen yeu execulejava, you are ac ua lly


spe byq he name h e ela tha
that Ou
Lant to eXecate. Hsil aatematicall &earch
f ale by that name that ha he clas
elensicm
r the fle, i l l exceute the cccle
Cotained in the peahecd elas
Sample ecparn plaraten

Th t a sfrople ewa roqpaM


*
Call R le Xernple Tova
Ths & a co merit. he conlent oatommer

e fgmoved by the Ceopiln


Commens generally explain how Scme p o

he am toosks

class xample
This ne cARE the keywerd class t detle
ha a new clase fs beirg deltned

Tere
Erample s an fdenthter that t Tam

h e clay

publie Shrte velel matn


(Strig age I)L
Alt fava Applicaton beaio execiien by talln
naint ).

The publie Keyesert is an acces Speier


which alleos the noammey lo Corl

he visibialy g class membex .


(D
4
Exanple 2jaua
dnothes shoat Yample. (all h1 Kale
t
tleve
Exampe2 ava /
elaus Exmmple2Java
ublte strrtc veid
man (Stirg avqs(){
t m This declasnu d Vadable u
Oum 100 / Ihis assiopa um the value loo
Suslero odtpiilln ("This
ts num:"tnun)
Mum nwm * 23
The value d num 2i ";
Sutem ouprot
Ssleo out prstlo (nun);
ee the hollad
hen you Tun thr proghan, oyou
oatpul
Thisemum: 100
200
The vaue um2
Su l e )Tava Progar)
Douume htotn SAGo)
Partage StTemed
Impoit stafement
Ttare Statermei
claus Deini bety
main mehod ely
man mathed
de jniten
Dounenlatlo Sedion: t ConlU) commas
Com maag
tne ich one 3p
(multpleple line Comme
2 (Single line Commes
- ocurnentcom
Parage deelanatortdecdane he pco
Cshnt
and fo hom the
Compiley Hhat the
name
elass f d-toeel hene belengs t is
ac age
pac koge abe
clas a
clau b
Class C
Ioteree
4 Statemest
We can Creale an ftes ate h i s
e ttas hace keywero
Secuon i neaydred. ue
to Crete an nterhace
An toterfare t a dlsly dihfond
C o n t a n y a n d
em he clas. IH containt
cnly
nethed declanato
oteace ca
Veld stato)
volcd stopt)
3Tmpo-t stalemeit
The patkage coitarn many prede
Claues and fnteylate
T we wan to use any
claara pastculan package, twe meec To imp
hat claas
We the
we
impoit keyccovd to Rmpél
he clasc
Data TypE
The Pasbve Tyres
Jova denet ve
tepes dia
byla 5 Chah - choael
2 Shot 6. float
Thtege fiunsrg pint
nt doubleJ urnbed
8 booleo)-Bnolesa
oTrteaes
Jawa detnes 4 Jotege daa tyPes
byle
2Shot
An hexe
anf sqred, pourue
3. Int
and egaive valuet
unioned, pesive- o
7Java doe not Suppert
ttegeis
Norne Ofdth Range 374,e3
fss.
. . . To 1,233,
036,
ond 64 2 3 , 3 72,
2lu7, E3,67
int -2,u7, u&3, 6 uS ts
32
34,76 7
Shot -32,768 to
12¢ t5 (27
ogle
The mallest stear tye t by
Thts f a
qne 8-6tt ype that hau
haa a
ange o n -
g To 127
Vavdnbles yre byt eepeially u4ed
u e ie
when uou'Te working ha ztrear dala
Jem mewort o tle
a
Byfe vaaablee ane declaoned by ue he
byte keywod
byte b, c
I declaneg two byt varabls bc
Sho-t
Shert t a samed 16-6 type t he a
trorn-32,768 to 32,167. T probably
the Jeat-wed Jaa type
Sher shot S
shot t
1ot usecd int-gerd ype ie it
The oot commonly
3-6t type hat h a
T
aiqned
to ,147,u83,6u7
2,147, uB 3, 64
aarge
rge ror
ror
Vesiablee ype fot one C y employed
Fnder a9Ho"
toCoytol lcop and To
long ard t ueh
and uehd
signecd 6u-bit type
*lomg te
a
s
oxcauionc
Lohee an istype
for thac e
deired
holdhe
mot lange
enough to
vaue
+ The eang a ong is
qyite lasq
onq
dtctance ltght toovek using do
arnple: 1l Compile
aables
elaue Ligbti
publuc state void mao (Sting args) 2
ist ltgktspeed
long day
lng Seconds
Long dtance
l appoximmle Speed 9gtin iks e
ght speed » 186000o
days =to00, Speiby doya hes
Seronds- dayt 24 60 * 60; (onveit
lr
distante gbtpescl xsecond, }(ompcale duto
Sytem out psln(" Tn "+ days)
Celaiahtil trouel cht
(obetanee t+ "rile ');
3
h
protam genena les
theollocoing oatp
In loooclayg
ieht ll tsavel about 1607p40=
nlg
rlatnq4 fst pes
leanq -
Point mumbes ako kron
eal muobess, ne uged ohen erauaing expr
hat Sreauine kunctnal reésism
Thene ane Tuco
Sloat
kndeloating-popa
2 ouble
The p e tloat fe a SingepTedáen aue
hat ues 32 6 stoage
loat hiq ktemp loustemp;
double:
Double preciston, a denõted b the double
Keywot , use 6u bit to Stove a vaue
Ovex
When you nee to mcurstain accuhacy
maipuabng Jarge-value
Many tesa iorM,
O ae
many
numbes, Jouble fs he bezt choce
Chan aclers:
wed to
(o Sfore
wed
type
InJava, h e data
Chanac ley fs chas
JavJava chao- 166it vpe
Pange is t 65, 56
chars
Thene ane TO Tegouve
tveny chanaclev vaue chouldbe enclo
ngle quoley
Boolean
Java's piive Type , talled boolean, tor
leofcal valu .
TCan hawe only ome tuo pocsi ble a
O tue
taue
Thts is the type Tslerned by all sula
Opealod, as in he Case asb
Bcolean alo tHhe type 9eaioud eapisud
he Cord' uenal expoegionghat gove)
he
he
he Corbro sttemest sueh as
Varia bles
The voaia ble is he
bafc un torcge
Java ocaro

Avaiable f
dolned by he Corobinater
anfderter, a
type and an
oftono
iniGaize

Tn additon, all varia6les have a


itepe
shich de}ines theiviibity and Leta

Declasing a varHable
In Java, all vasables mul be declase
beove hey can be ued

The baslc fovm ofa vañable declasraton tr


The

shouonbeleuo
tqpe ldeALAie
dalartype vamable

datatype vaable =value


Hene Oe seNena ermples ofvarable
declacuor vasou type Notr hat s

frelude an fitaication
Int a,b,c declone thee tr, abt
Tt d3, e, f:s; | FriGaie
mOre, Iniual
d ard-f
Z 22 riCals
double pi 3.14159:declay an approvinalr
opprovimar
P
chon vostable hagevalue

Na allaus
pperly ovmee de
to hoNe any declaned yee
Dyname,ibalitaten

Java allosc vanatlee t be


Ini-salite
dumaically, eing an erpestiom valtel at
he
the time the vaiable t derlased

Emp le

Demendtvate dynamit initalteaten


clau OynIatt i
puble statc voil malo Shng arq [1) {
louble a3.0, b=+ O;

deuble C Math sqt (e*at b«k)/e's droiale


Tri tased
Stypten out pastln ("Hypetenues s + c
The Scep and Lletime Vaabler
Jasa alleo vadableg declasnec
declasecd
any blec k.
A e
bequn wth an Openinq CuYly bv0te
ad endecd by closinq cusly brece.

Abler deltne a gco pe

Thus each Gme yeu t a neo


bloc
youL ae Cea g a neu Stope
A Scope deemines ohat object ae

vis ble ts
athepantt u p
-

t ake detemmines the etme


hose cbjec
Thene oneuo mojo Stopea in Tawe
O Delined by clas a

methed

Dhe Stope deltned ya mefhed, beafns


with o opeinq Cwny brae.
genere ule, eunca 6le
a olrelaoec Pide
a ope ae mot Vie ble 6 code that

delnecd oiakicle he Scope

Thu hen you olec lave avartable


a
Scope Ou
ae localsng hat
Vaer fable and poolecng t tro uwrcuithorised
actex( moclihfcator)

can be nled
-SCepe
Te aneleand the eltect ded scapa,

I Demertgte slock ttope


cleu Scope
public State veel main (Sthng argst7) i
tntx 11Braen to all codle tthio mai

X = loS

(x19 stcet neuw scope


block
Kneon tthis
foty-20
int y-20
x y both kncwn hee
"
and y: '4x4"
"+1); 41)
lemOTt piatn (
X 2
Encun ere
Emor y not
y =00 Enowm hene
is SHI
/lx
o l pmanlnu +x
Als, a vesble declamed
lko, a
Hhin a blork..
lost vaue. hen he blockE & let

Thu,etfetine a vooiable is tone

totScope
Demniente tne 6avama ble
cla Letne
publte sttc voicl nainSting arqC)i
ttx

int
y- yt nthalced meh tme bleti
ented
Syfe. ot-pllny: )
pro
-lo0

Stem-ot prosn" s eLo"1)


u t pt

fs -

e w100

s-1

Note hough bloc ke can be nested, ycu Cann tt


derlane a Vaeria ble to have the tame ram
as mE fn an outexsope

class Scope F
tetic void main (Shing agel)
pubbe
int bar-l,
l Cx eate a new Spe
eo
iot bor2 | Compilo me

albeady de fined
ay

n ay s ke ke-ped Vasiabk,
COnmoD TaMe.
hat ane eeored t by a

any type Can be Cealed and mo


ng
have Ce mie dimenci ong
-
A Sped|te elemest in an anay
acee by ttodex
-

Åmays cbe a Comveniern mean


Telted Fopmaton

One- dtmendemal thray


One dtmenional atay , eenlal
aJit ke typed vasiabla

ToCreale an aAay
On
aay vasiiable n he dered type

tpe var-name

Enanp
t morh dayalJ
a
new

Mem0Y
Special eleatr hal tlot

The Tenena fo ne it
dmenimal ama
pple t om
eas loua
va neu
9leuia
may rable

that Link
Ltype data ued
to amo

Erample: moithdays - neuo tnt[2J

T a m that Creale an asay he nuobey


d a in each mort

Dernenalrale acne-dimentional avte

clo Anoy
pubbc stote vdd man (Sting agsl)
int rerih-da 1
morihdays = mew otJ
morithdaye lo = 3
: 28
31
marithdlays[3) 30;

(7-31
5 30
[4-31
(31
8-30
31
o-30

Syglem-ot wrhnll (" Apil hau "+ rmornthdlaypl


day ,11)

itp+fny t
y n s
When u pogpam,

umber
ashayin Galiser
An satet t tmma Separm
esí ons Susoucded
ep Cuxl braces
The commas sepeStale the valuu he
a9hay clement.
The aha will aütomatcally be cveated
lage enough t hold the Tumber
elemess you &pecu, tnheasoay iuabse

n n impo vee vricon ) he porevious proeparn


claas AtoAray
bu6lic state void nain(Strnq arga )
d
int month_daye lJ 31,2831, 30, 31, 30,3
a,30, 3l, 30,3
Seaten ot priolla (April hu't monthdl
+" days "

You might also like