Date post: | 10-Feb-2018 |
Category: |
Documents |
Upload: | anonymous-boresf |
View: | 220 times |
Download: | 0 times |
of 39
7/22/2019 25305511-Core-Java-Notes.doc
1/39
14/6/2007
Java Technology
1. Java Standard Edition (Java SE)2. Java Enterprise Edition (Java EE)
. Java !o"ile Edition ( Java !E)
http#//$ava.s%n.co&
'ersions1. rod%ct 'ersion
a. 1.0
". 1.1
c. 1.2d. 1.
e. 1.4
. *.0
g. 6.02. +eveloper 'ersion (J+, - JE)
a. 1.0". 1.1
c. 1.2
d. 1.e. 1.4
. 1.*
g. 1.6
Tools
1. Java +evelop&ent ,it (1.* - 1.6)2. Java %nti&e Environ&ent (1.* - 1.6)
Editor or IDE
1. otepad
2. Kawa
. eteans
4. Eclipse
*. ntelliJ6. ! 'is%al ge
7. 3racle J+eveloper
Etc.
J+, Javac.ee5 J'.EE5 J.EE etc.
JE Java 'irt%al !achine 8ar"age 9ollector
J'!9ode 'eriier JT (J%st:n:Ti&e) co&piler
History of Java
http://java.sun.com/http://java.sun.com/7/22/2019 25305511-Core-Java-Notes.doc
2/39
+eveloped in 1;;1 at S%n !icrosyste&s "y Ja&es 8osling ?
rena&ed to Java in 1;;*.
Featres of Java
1. Si&ple
2. Sec%re. 3"$ect 3riented
4. orta"le
*. lator& ndependent
a. .$ava J'9.EE .class (yte 9ode)JT inary
". J'! is a &achine dependent progra& that &a>es Java lator&
ndependent6. o"%st
a. n:"%ilt
i. "ig set o li"rariesii. 8ar"age 9ollection
". Eception handling acility7. !%ltitheaded
16/6/2007
9o&&ents
1. !%ltiline co&&entsa. /@ A @/
2. Single Bine
a. // co&&ents. +oc%&entation 9o&&entsa. /@@ and @/
Types of Java !rogra"s
1. Java pplication
2. Java pplets
#riti$g Java %pplicatio$ !rogra"s
1. Each application progra& &%st have an entry point
p&lic static void "ai$'(tri$g args)*+
2. Java provides a set o li"raries called pac>ages containing a related set o classes
a. $ava.lang (dea%lt) C rovides all co&&only %sed classes li>e String5 !athetc.
". $ava.io C /3 related classes. Dile5 Dileriter5 Dileeader5 np%tStrea&5
np%tStrea&eader5 %eredeader etc.
c. $ava.sFl or J+9
7/22/2019 25305511-Core-Java-Notes.doc
3/39
d. $ava.aey"oard and o%t or the &onitor. i$is an o"$ect o I$pt(trea"class and
otis an o"$ect o !ri$t(trea"class
rintStrea& class provides ðods1. print()
2. println()
. print()
#riti$g first /ava progra"
// irst progra& C irst.$ava
class TestI
p%"lic static void &ain(String argsK)ISyste&.o%t.println(LMello to allL)H
N
N
ote# Set the path o older "eore co&pilation
SET TMOPTMPH+#QJ+,1.*Q
o"pili$g Java progra"sJ'9 Rprogra&na&e
e.g.
J'9 irst.$ava Test.class
File a"i$g co$ve$tio$
1. ile can have any n%&"er o classes and choice o its na&e ecept i class isp%"lic then "oth &%st "e sa&e
7/22/2019 25305511-Core-Java-Notes.doc
4/39
2. progra& can have only one p%"lic class
$$i$g a class
J' Rclassna&e
Java Test
Data Types
1. ntegral types (ll are signed)
a. "yte : 1 "yte". short : 2 "ytes
c. int : 4 "ytes
d. long : "ytes
2. Dloatingsa. loat : 4 "ytes (6 dec. place)
". do%"le : "ytes (14 dec. place)
. 9haracters
a. char : 2 "ytes (Gnicode)4. ooleans
a. "oolean C 1 "it ( can have only tr%e or alse)
3aria&les
&e&ory location to store and retrieve the val%es that can "e changed any
7/22/2019 25305511-Core-Java-Notes.doc
5/39
Dloating literals (dea%lt is do%"le)
do%"le >O*6.6H
loat pO4.*H // never get co&piledloat pO4.*DH // Gse or D s%i or loating
9haracter literals: Enclosed in single F%otes
char chO??H
: Escape seF%ence characters
o Qn
o Qt
o QQ
o QV
Eg.Me said5 WQnV is or ne< line
String Biterals: Enclosed in do%"le F%otes
: Mandled "y (tri$gclass o $ava.lang pac>age
String na&eOV'i>asVH
eadi$g Data fro" 5ey&oard
!ethod 1
1. Gse (ca$$erclass o/ava.tilpac>age
a. !ethods o Scanner class
i. int netnt()ii. do%"le net+o%"le()
iii. String net()
17/6/2007
!ethod 2 C Gsing %eredeader: ead data ro& >ey"oard (Syste&.in) and pass it to np%tStrea&eader
: np%tStrea&eader converts the "yte into character
: ass the characters to %eredeader to place the& into a "%er till enter >eyis pressed
: ass the "%ered data to any place %sing readBine() ðod o%eredeader class
%eredeader "rOne< %eredeader(ne< np%tStrea&eader(Syste&.in))H
ote# readBine() ret%rns the data in String or&at. t need to "e converted to other types.
Java provides special >ind o classes or conversion called #rapper classes
7/22/2019 25305511-Core-Java-Notes.doc
6/39
rapper class is a special class corresponding to data types
"yteyte class
shortShort class
intnteger class
long Bongchar 9haracter
o$versio$ rle
datatype varia"leO
7/22/2019 25305511-Core-Java-Notes.doc
7/39
1. rith&etic : @ /
2. Bogical -- YY Z
. elational OO ZO O R RO instanceo 4. it
7/22/2019 25305511-Core-Java-Notes.doc
8/39
Test.
7/22/2019 25305511-Core-Java-Notes.doc
9/39
1. 8eneral class
2. Dinal class C fi$al>eyey "alance.
assign&ents]i&pecca"letrainers.co.in
2:-6-2008
!illars of !s
1. Encaps%lationa. ll &e&"ers &%st "e
7/22/2019 25305511-Core-Java-Notes.doc
10/39
class 9 etends // s%per class5 5parent class
IN
: Java provides only single inheritance
: Each $ava class in a child class o 3"$ect class: Gse tis>eyage is a older contains classes and interaces or speciic p%rpose: +ierent pac>age can have classes ageso +#Q"1p>g
: 9reate a older to p%t yo%r classes called as pac>age
o +#Q"1p>gQ&atc1:
: 9reate the classes and save the& into "atch1 older age "atch1H
class TestI
N
g
: &port the pac>age and %se its classes
Distri&tio$ of !ac5ages
1. ac>ages can "e distri"%ted in t
7/22/2019 25305511-Core-Java-Notes.doc
11/39
E.g. 9reating a J ile
J cv test.$ar @.gi @.$ava @.class
8oto the +3S pro&pt and then goto the older containing the pac>age
(d#Q"1p>g)
ss%e the co&&and to create a $arJ cv "1.$ar .
24/6/2007
I$trodctio$ to I$terfaces
: collection o a"stract ðods and inal ields
: ll ðods are p%"lic and a"stract "y dea%lt and each ield is p%"lic andinal "y dea%lt
: rovides a ðod to i&ple&ent the &%ltiple inheritance in Java
: class can inherit any n%&"er o interace %sing i"ple"e$ts>eyeyey
7/22/2019 25305511-Core-Java-Notes.doc
12/39
: Java provides ive >ey to al
7/22/2019 25305511-Core-Java-Notes.doc
13/39
Syste&.o%t.println(s1OOs2)H//val%e co&parison : Tr%e
o String 3"$ects
String s1One< String(Wa>eshV)H
String s2One< String(Wa>eshV)H
Syste&.o%t.println(s1OOs2)H//eerence co&parison C Dalse
: !ethods o String classo toGpper9ase()
o toBo
7/22/2019 25305511-Core-Java-Notes.doc
14/39
o void set'isi"le("oolean ")
o void setac>gro%nd(9olor c)
: o$tai$erclass provides co&&on ðods or all the containers
o void setBayo%t(Bayo%t!anager l&)
o void add(9o&ponent c)
o void re&ove(9o&ponent c):
Types of ,age
Dra&e class
: rovides top level container or applications: !ethods
o void setTitle(String s)
,e$eral ,
7/22/2019 25305511-Core-Java-Notes.doc
15/39
: !ethods
o void setTet(String tet)
o String getTet()
%tton class
: To create a p%sh "%tton
: 9onstr%ctor o %tton(String la"el)
: !ethod
o void setBa"el(String la"el)
o String getBa"el()
o void addctionBistener(o"$ect)
2/6/2007
Bayo%t!anagers
: Special classes that control the place&ent o co&ponents in a container: ll are inherited ro& Bayo%t!anager class
: They are
o orderBayo%t
+ea%lt or Dra&e
+ivides the screen into * sections
East
est
orth
So%th
9enter
+ea%lt is center
+eine the section
7/22/2019 25305511-Core-Java-Notes.doc
16/39
: Gse seto%nds() ðod o setBocation() and setSiUe() ðods
seto%nds(int 5 int y5 int eyressed(,eyEvent e)
p%"lic void >eyeleased(,eyEvent e)
Gse ðods o ,eyEvent
o 3"$ect getSo%rce()
o char get,ey9har()
7/22/2019 25305511-Core-Java-Notes.doc
17/39
o void cons%&e()
I$trodctio$ to %dapters
: Special classes corresponding to listeners
7/22/2019 25305511-Core-Java-Notes.doc
18/39
R/ME+
R3+^
R/3+^R/MT!B
TagX Tag is special code enclosed in R and deined "y 9. e.g RMT!BR/MT!B5
RM1R/M15 R!8 etc.
Types o TagsX
1. aired Tags
a. Maving opening and closing "oth
i. Rh1R/h15 R"R/"5 RiR/i etc.2. Sing%lar Tags
a. Maving only opening section
i. R 5 R!8 5 R!ET5 R8S3G+ etc.
%ttri&tes: roperties
7/22/2019 25305511-Core-Java-Notes.doc
19/39
a&eOVpara&eter na&eV
'al%eOVpara&eter val%eV
. pplets do not contain any &ain() ðod or the constr%ctor4. ll applets &%st "e p%"lic
*. Each $ava progra& to "e called as an applet &%st inherit ro& %ppletclass o
/ava.appletpac>agea. pplet class provides vario%s ðods
>etods of %pplet class
: p%"lic void init() C si&ilar to constr%ctor5 eec%tes only or once
: p%"lic void paint(8raphics g)
: p%"lic void repaint()
>etods of ,rapics class
: 8raphics is a class ro& $ava.aage to dra< strings5 i&ages5 shapes etc.
o p%"lic void dra
7/22/2019 25305511-Core-Java-Notes.doc
20/39
&age i&gOget&age(GB path5 String ilena&e)
:-8-2008
e$s
1. +rop +oe$=ar class o"$ecti. 9reate >e$class o"$ects
1. 9reate >e$Ite"class o"$ects
". Set the !en%ar on ra&e %sing set!en%ar() ðod o Dra&e classc. lace the ctionBistener on !en%te&
2. op%p!en%s
:9reate an o"$ect o !opp>e$ class and add !en% or !en%te& or "oth: To sho< the pop%p&en% %se !o%seBistener?s &o%seealeased() ðod
: 9hec> the right clic> %sing is!oppTrigger'+ðod o !o%seEvent class
: Gse sho
7/22/2019 25305511-Core-Java-Notes.doc
21/39
9reating Bisto
: llo"o()
o 9hec>"o(String caption)
o 9hec>o(String caption5 "oolean chec>ed)
: Gse &oolea$ get(tate'+ðod to chec> the state
9reating radio "%ttons
: Gse the sa&e ec5&o;class "o8ro%p and then the 9hec>"o
o 9hec>"o(String caption5 9hec>"o8ro%p cg5 "oolean state)
: Gse getState() to chec> the state
C-8-2008
J+9 (Java +ata"ase 9onnectivity)
: Java is a ront end that can "e connected ind o data"ase: ll classes and interaces are provides in tages
o $ava.sFlo $ava.sFl
(teps to se te JD=
1. 9hec> or the data"asea. !S ccess
". !y SFl
c. 3racle2. 9hec> or the driver class ile
a. S%n provides a dea%lt driver called J+9:3+9 ridge
7/22/2019 25305511-Core-Java-Notes.doc
22/39
4. Esta"lish the connection
7/22/2019 25305511-Core-Java-Notes.doc
23/39
o void setnt(int indeno5 int val%e)
o void setString(int indeno5 String val%e)
: lloa$agi$g Tra$sactio$s
: 9onnection interace provides three ðods
o set%to9o&&it("oolean state) :: dea%lt is tr%e
o co&&it() C hen all codes eec%ted
o roll"ac>() C hen so&e eception occ%rs
8-8-2008
oadi$g Drivers si$g (yste" !roperties
Syste&.setroperty(W$d"c.driversV5VdriverclassV)H
(tori$g i$for"atio$ a&ot driverB rlB logi$ a$d password otside te progra"
: Store the inor&ation in a ile eyOval%e relationship
7/22/2019 25305511-Core-Java-Notes.doc
24/39
//data"ase.properties
driverOs%n.$d"c.od"c.Jd"c3d"c+river%rlO$d"c#od"c#"1
loginO
passO
: ead the ile ro& dis> to &e&ory %sing FileI$pt(trea"class o $ava.io
pac>age
Dilenp%tStrea& One< Dilenp%tStrea&(Wdata"ase.propertiesV)H
: To separate the data or dierent properties5 %se !ropertiesclass o $ava.%tilpac>age
7/22/2019 25305511-Core-Java-Notes.doc
25/39
S
nsert into e&p val%es(]na&e5 ]e&ail)
83
on e< _%ery and rite yo%r proced%re: Select the roced%re and press D* to eec%te
: roced%re has "een created
alli$g a stored procedre
: 9reate a 9alla"leState&ent and %se eec%te() ðod
9alla"leState&ent csOcn.prepare9all(LIcall Save+ata(X5X)NL)H
cs.setString(15na&e)Hcs.setString(25e&ail)H
cs.eec%te()H
10-8-2008
Sage %sed $ava.s
7/22/2019 25305511-Core-Java-Notes.doc
26/39
o static String shos are set %sing a class called a$agerand applied %sing class
(wi$gndDeel() ðod G!anager
o G!anager.setBoo>ndDeel(L$ava.sndD
eelL)H
o G!anager.setBoo>ndDeel(Lco&.s%n.$ava.sndDeelL)H
o G!anager.setBoo>ndDeel(Lco&.s%n.$ava.s
7/22/2019 25305511-Core-Java-Notes.doc
27/39
: To create a slide to a select a val%e ro& &ini&%& to &ai&%&
: 9onstr%ctor
o JSlider(int direction5 int start5 int end5 int dea%ltval%e)
: Bistener %sed
o 9hangeBistener
p%"lic void state9hanged(9hangeEvent e): !ethdod
o void set!ini&%&(int val%e)
o int get!ini&%&()
o void set!ai&%&(int val%e)
o int get!ai&%&()
o void set'al%e(int n)
o int get'al%e()
o void setEtent(int val%e)
o void set!a$orTic>Spacing(int n)
o void set!inorTic>Spacing(int n)
o void setaintTic>s("oolean displaytic>s)
o void setaintBa"els("oolean displaytetla"els)
12-8-2008
JTetrea class
: Dor &%lti:line tet
: JTetrea(int roey)
To &a>e a hot >ey: 'irt%al >ey a constant given to so&e >ey inside the ,eyEvent class
o ,eyEvent.',`
o ,eyEvent.',`9,S9E
: To apply the shortc%t co&&and apply the Key(tro5eclass o"$ect to the
J!en%te& o"$ects %singo setccelarator(,eyStro>e >s) ðod
: ,eyStro>e class provides static ðod get,eyStro>e()
o Select the >ey and the !as> ,ey
!as> ,eys can "e lt5 Shit or 9trl provided in Event class
Event.9TB`!S,
Event.BT`!S,
Event.SMDT`!S,
7/22/2019 25305511-Core-Java-Notes.doc
28/39
,eyStro>e >sO,eyStro>e.get,eyStro>e(,eyEvent.',`5Event.9TB`!S,)H
&n%es)H
JToolar class
: To create a tool"ar: 9ontains a collection o JToolar%tton class o"$ects
:
JnternalDra&e class: rovide a ra&e
7/22/2019 25305511-Core-Java-Notes.doc
29/39
7/22/2019 25305511-Core-Java-Notes.doc
30/39
String class provides getytes() ðod
"yte K"Os.getytes()H
: hile reading read the data "yte "y "yte
et topic
!%ltithreading
17-8-2008
Thread is s%":process
7/22/2019 25305511-Core-Java-Notes.doc
31/39
2. eady State
a. hen start() ðod is called
. %nning Statea. Eec%tion o r%n()
4. loc> State
a. hen thread goes to Sleep5 ait or S%spend sit%ation*. +ead state
a. hen is inished to stop is called
(y$cro$iatio$
: hen teys
: t can "e %sed
7/22/2019 25305511-Core-Java-Notes.doc
32/39
Gsing $oin() ðod
: To hold the thread in &e&ory %ntil depending threads get ter&inated
T1.$oin()H// inished end
T2.$oin()H//inished second last
T.$oin()H //inished irst
7/22/2019 25305511-Core-Java-Notes.doc
33/39
: ll classes are provided in/ava.$etpac>age
: eting can "e o tet
o G+
Server Side +atagra&Soc>et
9lient Side
+atagra&Soc>et
: Every server &%st >no< its port n%&"er and every client &%st >no< the
ddress or +S along
7/22/2019 25305511-Core-Java-Notes.doc
34/39
: rovides a ðod accept()
Soc>et accept()H
Ea&ple
ServerSoc>et sOne< ServerSoc>et(0)H
Soc>et cOs.accept()H
lie$t (ide wit (oc5et
Soc>et sOne< Soc>et(WipaddressV50)H
Soc>et class provides ðods
np%tStrea& getnp%tStrea&()H
3%p%tStrea& get3%tp%tStrea&()
To send data in string or&at5 create an o"$ect o rintriter class
rintriter o%tOne< rintriter(c.get3%p%tStrea&()5 tr%e)H
Gsing print()5 print()5 println() ðod to send data.
To read data %se getnp%tStrea&() ðod at the place o Syste&.in
%eredeader "rOne< %eredeader(ne< np%tStrea&eader(s.getnp%tStrea&())H
24-8-2008
or>ing et
+atagra&ac>et
netddress
netddress hold inor&ation a"o%t a &achine and provides special static ðods that
ret%rn o"$ect o its o
7/22/2019 25305511-Core-Java-Notes.doc
35/39
+atagra&ac>et("yte K"5 int siUe)
+atagra&ac>et("yte K"5 int siUe5 netddress ia5 int port)
"yte Kget+ata()
int getBength()
+atagra&Soc>et class
: %sed to send o receive the data
+atagra&Soc>et(int port)
!ethod
send(+atagra&ac>et p)receive(+atagra&ac>et p)
#e& etwor5i$g
: rovides co&&%nication ro& a &achine to ey5 3"$ect val%e)
3"$ect get(String >ey)
En%&erator >eys()
o En%&eration interace
"oolean has!oreEle&ents()
3"$ect netEle&ent()
7/22/2019 25305511-Core-Java-Notes.doc
36/39
ote# %se instanceof operator to >no< the type o an ele&ent in collection
26-8-2008
#or5i$g wit ollectio$ Fra"ewor5
: set o interaces and classes that %nder so&e r%les called 9ollection
Dra&e: They designed o%r &ain interaces
o 9ollection interace
Top level interaces or all collections
o Bist : an interace to ey
7/22/2019 25305511-Core-Java-Notes.doc
37/39
: Gse static>eyage
: Gse start() ðod to start the process
2;/7/2007
ens: 9onstr%ctor
o StringTo>eniUer(String s5 String to>ens)
: !ethods
o "oolean has!oreTo>ens()
o String netTo>en()
7/22/2019 25305511-Core-Java-Notes.doc
38/39
Ti"er &ased %pplicatio$s wit Ti"er a$d Ti"erTas5 class
: Ti&erTas> provides a r%n() to provide the ing: Ti&er class provides a sched%le() ðod
o sched%le(Ti&erTas> t5 int delayti&ein&s5 int repeatti&ein&s)
o cancel()o restart()
e"ote >etod I$vocatio$ '>I+
: ðod to place all the logic and data on centraliUed server and call the&
ro& any age
". ll ðods &%st throage. 9reate a server progra& to reside on a &achine and server the clients
a. Gse a&ing.re"ind() to provide so&e server na&e and ret%rn the o"$ect o
"%siness logic class to the client
&. a"i$g.re&i$d'server$a"eB$ew &sclass$a"e'++4. 9reate a client progra& to contact the server and get reerence o "%siness logic
ile into an interace
a. Gse a&ing.loo>%p() ðod connect
7/22/2019 25305511-Core-Java-Notes.doc
39/39
*. 9reate STG and S,EBT3 or the "%siness ðod to &arshalling and %n:
&arshaling the ðods
a. Gse !9 tool ro& J+, to create the&i. !9 i&ple&entclass
". o< in J+, 1.* S,EBT3 is optional
c. STG is or client and S>elton is or Server
Files placed o$ clie$t
nterace
St%"
9lient rogra&
(erverside
nterace
Server rogra&&ple&entation progra&
E;ecti$g te applicatio$
Server Side
Start the !E8ST^
STT !E8ST^
Start the Server progra&9lient
Start the client progra&
ote# The c%rrent older &%st "e in classspath