1. core java
Post on 06-Jul-2018
247 Views
Preview:
TRANSCRIPT
-
8/17/2019 1. Core Java
1/177
CORE JAVA
Rajeev Gupta
MTech CS
-
8/17/2019 1. Core Java
2/177
Contents at a glance
rgupta.mtech@gmail.com
Java An Introduction
Basic Java Constructs and ata T!pes and arra!
OO Introduction
Class o"ject and related topics
Inheritance
#inal $A"stract Classes and inter%ace
String class$ i&&uta"ilit!$ inner classes
E'ceptions (andling$ assertions
Strea&s and Input)output #iles$ seriali*ation Java Thread
Java Collections
-
8/17/2019 1. Core Java
3/177
a!+,
rgupta.mtech@gmail.com
Session-1
Introduction to Java
What is Java,Why Java?
JDK,JRE and JVM discussion Installing JDK, eclipse
Hello World application
Procedural Prograing
I!""else,s#itch, looping
$rray
E%aples
Session-2
&'(ect &riented
&'(ect, class, Encapsulation,
$'straction,
Inheritence,
Polyorphis,essage
passing
) concepts only, ipleentation on day *+
-
8/17/2019 1. Core Java
4/177
a!+.
rgupta.mtech@gmail.com
Session-1
lass and o'(ects
reating lasses
Ipleenting Methods
&'(ect- state, identity and 'ehaviour
onstructors- de!ault, paraeteri.ed and copy
Instance varia'le, static varia'le and local
varia'le
Initiali.ation 'loc/
0tatic ethod and applications
/ac0ages and visi"ilit! &odi1er2 pu"lic$protected$ private and de%ault
Session-2Inheritance
1ype o! inheritance, diaond pro'le
Instance&! operator
2inal
2inal varia'le, !inal ethod , !inal class
$cess control- pu'lic,
private,protected and de!ault
Pac/ages
$'stract class
Inter!ace
Polyorphis&verloading
&verriding
1ype o! relationship '# o'(ects
I03$, H$03$, 40E3$
-
8/17/2019 1. Core Java
5/177
a!+3
rgupta.mtech@gmail.com
Session-1
0tring class- Iuta'ility and usages,
string'uilder and string'u!!er
Wrapper classes and usages
Java 5 6anguage 2eatures )I+-
$uto7o%ing and 4n'o%ing, Enhanced 2or
loop, Varargs, 0tatic Iport, Enus
Inner classes
Regular inner class, ethod local inner class,
annonyous inner class
Session-2
E%ception handling
1ry, catch, thro#, thro#s,
!inally
hec/ed and
4nchec/ed e%ception
4ser de!ined e%ceptions
Rules !or &verloading8&verriding8onstructor
thro#ing e%ceptions
Rethro#ing e%ceptions
E%ception haining
assertions
-
8/17/2019 1. Core Java
6/177
-
8/17/2019 1. Core Java
7/177
a!+5
rgupta.mtech@gmail.com
Session-1
ollections 2rae#or/
6ist, Map, 0et usages introduction
4sages $rray6ist, 6in/ed6ist, HashMap,
1reeMap, Hash0et
opara'le, oparator inter!ace
ipleentation
4ser de!ine /ey in HashMap
Session-2
Java 5 6anguage 2eatures II
;enerics$nnotations
ase study using collection, 2ile
-
8/17/2019 1. Core Java
8/177
A6+,
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
9/177
Session+,
rgupta.mtech@gmail.com
Introduction to Java What is Java,Why Java?
JDK,JRE and JVM discussion
Installing JDK, eclipse Hello World application
Procedural Prograing I!""else,s#itch, looping
$rray
E%aples
-
8/17/2019 1. Core Java
10/177
7hat is Java8
rgupta.mtech@gmail.com
Java
-
8/17/2019 1. Core Java
11/177
(o9 Sun de1ne Java8
rgupta.mtech@gmail.com
0iple
&'(ect &riented
Porta'le
$rchitecture >eutral
Distri'uted Multi3threaded
Ro'ust, 0ecure80a!e
Interpreted
High Per!orance
Dynaic prograing language8plat!or"
-
8/17/2019 1. Core Java
12/177
Java Platform
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
13/177
-
8/17/2019 1. Core Java
14/177
So&e co&&on "u**9ords
rgupta.mtech@gmail.com
JDK
Java developent /it, developer need it
7yte ode
Highly optii.e instruction set, designed to run on JVM
Plat!or independent JRE
Java Runtie environent, reate an instance JVM, ust 'e there on
deployent achine"
Plat!or dependent
JVM Java Virtual Machine JVM
Pic/ 'yte code and e%ecute on client achine
Plat!or dependentG
-
8/17/2019 1. Core Java
15/177
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
16/177
:nderstanding JVM
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
17/177
(ello 7orld
rgupta.mtech@gmail.com
;a" set+up Java ,
-
8/17/2019 1. Core Java
18/177
Anal!sis o% (ello 7orld
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
19/177
Java /rogra& Structure
rgupta.mtech@gmail.com
ocu&entation Section
/ac0age State&ent
I&port State&ents
Inter%ace State&ents
Class eclarations
Main Method Class
-
8/17/2019 1. Core Java
20/177
Java ata T!pe
rgupta.mtech@gmail.com
. t!pe /ri&itive data t!pe
Re%erence data t!pe
-
8/17/2019 1. Core Java
21/177
/ri&itive data t!pe
rgupta.mtech@gmail.com
"oolean either true o% %alse
char ,> "it :nicode ,+"it integer ?signed@
int 3.+"it integer ?signed@
long >4+"it integer ?singed@
oat 3.+"it oating point ?IEEE =54+,FD5@
dou"le >4+"it oating point ?IEEE =54+,FD5@
Java uses :nicode to represent characters internall!
-
8/17/2019 1. Core Java
22/177
/rocedural /rogra&&ing
rgupta.mtech@gmail.com
i%
-
8/17/2019 1. Core Java
23/177
Arra!
rgupta.mtech@gmail.com
One i&ensional arra!
int 'KLne9 intK5L
(o9 Java arra! diNerent %ro&
C)CHH arra!8
-
8/17/2019 1. Core Java
24/177
Session+.
rgupta.mtech@gmail.com
O"ject Oriented Concepts
O"ject$ class
Basic principles o% OO Encapsulation
A"straction
&odularit!
Inheritance) (ierarch! /ol!&orphis&$ &essage passing
-
8/17/2019 1. Core Java
25/177
O"ject technologies
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
26/177
7hat is an O"ject8
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
27/177
Class
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
28/177
Classes and O"ject
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
29/177
Basic principles o% OO
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
30/177
A"straction
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
31/177
Encapsulation
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
32/177
Modularit!
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
33/177
(ierarch!
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
34/177
A "it a"out :M; diagra&
rgupta.mtech@gmail.com
:M; .
-
8/17/2019 1. Core Java
35/177
A6+.
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
36/177
Session+,
rgupta.mtech@gmail.com
Class and o"jects Creating Classes and o"ject
O"ject2 state$ identit! and "ehaviour
Constructors2 de%ault$ para&eteri*ed and cop!
eed o% this $ Constructor chaining
Instance varia"le$ static varia"le and localvaria"le
Initiali*ation "loc0
Scanner and print%
/ara&eter passing in Java Call "! value ) call "! re%erence
7h t i id
-
8/17/2019 1. Core Java
37/177
7hat can goes inside anclass8
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
38/177
Creating Classes and object
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
39/177
Correct 9a!8
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
40/177
Constructors: default, parameterized and cop
rgupta.mtech@gmail.com
Initiali*e state o% the o"ject Special ðod have sa&e
na&e as that o% class
Cant return an!thing
Can onl! "e called once %or an
o"ject
Can "e private
Cant "e static
Can overloaded "ut cantoverridden
Three t!pe o% constructors
e%ault$
/ara&eteri*ed and
Cop! constructor
-
8/17/2019 1. Core Java
41/177
eed o% this 8
rgupta.mtech@gmail.com
7hich id assigned to9hich id8
this is an re%erence tothe current o"ject reQuiredto diNerentiate local
varia"les 9ith instancevaria"les
Re%er ne't slide
this used to resolve
-
8/17/2019 1. Core Java
42/177
this used to resolvecon%usion
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
43/177
this 2 Constructor chaining8
rgupta.mtech@gmail.com
Calling oneconstructor %ro&another 8
-
8/17/2019 1. Core Java
44/177
Static ðod)varia"le
rgupta.mtech@gmail.com
Instance varia"le Uper o"ject9hile static varia"le are per class
Initiali*e and de1ne "e%ore an!o"jects
Most suita"le %or counter %oro"ject
Static ðod can onl! accessstatic data o% the class
#or calling static ðod 9e dontneed an o"ject o% that class
o9 guess 9h! &ain 9as static8
-
8/17/2019 1. Core Java
45/177
:sing static data
-
8/17/2019 1. Core Java
46/177
!nitialization bloc"
rgupta.mtech@gmail.com
7e can putrepeatedconstructor code
in an Initiali*ation"loc0
Static Initiali*ation"loc0 runs "e%orean! constructorand runs onl!
once
-
8/17/2019 1. Core Java
47/177
Initiali*ation "loc0
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
48/177
-
8/17/2019 1. Core Java
49/177
Java #oundation /ac0ages
rgupta.mtech@gmail.com
Java provides a large nu&"er o% classes groped intodiNerent pac0ages "ased on their %unctionalit!<
The si' %oundation Java pac0ages are2 java
-
8/17/2019 1. Core Java
50/177
Visi"ilit! Modi1ers
rgupta.mtech@gmail.com
#or instance varia"le and ðods /u"lic
/rotected
e%ault ?pac0age level@ /rivate
#or classes
/u"lic and de%ault
-
8/17/2019 1. Core Java
51/177
Visi"ilit! Modi1ers
rgupta.mtech@gmail.com
class A has de%ault visi"ilit!
hence can access in the sa&epac0age onl!<
Ma0e class A pu"lic$ thenaccess it<
/rotected data can access in thesa&e pac0age and all thesu"classes in other pac0agesprovide class itse% is pu"lic
-
8/17/2019 1. Core Java
52/177
Visi"ilit! Modi1ers
rgupta.mtech@gmail.com
Accessible to: public protected Package
(default)
private
Same Class Yes Yes Yes Yes
Class in package Yes Yes Yes No
Subclass in
different package
Yes Yes No No
Non-subclassdifferent package
Yes No No No
-
8/17/2019 1. Core Java
53/177
7ant to accept para&eter %ro& user8
rgupta.mtech@gmail.com
java
-
8/17/2019 1. Core Java
54/177
Call "! value
rgupta.mtech@gmail.com
Java support call "! value
The value changes in %unction is not going to reected inthe &ain<
-
8/17/2019 1. Core Java
55/177
Call "! re%erence
rgupta.mtech@gmail.com
Java dont support call "! re%erence<
7hen !ou pass an o"ject in an ðod cop! o% re%erence is passed so that 9e can &utate thestate o% the o"ject "ut cant delete original o"ject itsel%
-
8/17/2019 1. Core Java
56/177
Session+.
rgupta.mtech@gmail.com
Inheritance
T!pe o% inheritance$ dia&ond pro"le&
InstanceO% operator
#inal
#inal varia"le$ 1nal ðod $ 1nal class Acess control2 pu"lic$ private$protected and de%ault
/ac0ages
A"stract class
Inter%ace
/ol!&orphis&
Overloading
Overriding
-
8/17/2019 1. Core Java
57/177
Inheritance
rgupta.mtech@gmail.com
Inheritance is theinclusion ofbehaviour (i.e.methods) and state(i.e. variables) of a
base class in aderived class so thatthe! are accessi"le inthat derived class<
code reusability.
Subclass and Superclass concept
-
8/17/2019 1. Core Java
58/177
ia&ond /ro"le&8
rgupta.mtech@gmail.com
(ierarch! inheritance can leads to poor design
-
8/17/2019 1. Core Java
59/177
Inheritance e'a&ple
rgupta.mtech@gmail.com
:se e'tends 0e!9ord
:se super to pass valuesto "ase class constructor<
-
8/17/2019 1. Core Java
60/177
Overloading
rgupta.mtech@gmail.com
Overloading deals 9ith &ultiple ðods in the sa&e class 9ith the sa&e na&e "utdiNerent ðod signatures<
Both the a"ove ðods have the sa&e ðod na&es "ut diNerent ðodsignatures$ 9hich &ean the ðods are overloaded<
Overloading lets you defne the same operation in
dierent ways or dierent data.
Constructor can be overloaded
Be careul o overloading ambiguity
*Overloading in case o var-arg and rapper ob!ects"
-
8/17/2019 1. Core Java
61/177
Overriding
rgupta.mtech@gmail.com
Overriding deals 9ith t9o ðods$ one in the parent class andthe other one in the child class and has the sa&e na&e andsignatures<
Both the a"ove ðods have the sa&e ðod na&es and the
signatures "ut the ðod in the su"class MyClass overrides theðod in the superclass BaseClass
Overriding lets !ou de1ne the same operation in dierentways or dierent ob!ect types.
-
8/17/2019 1. Core Java
62/177
/ol!&orphis&
rgupta.mtech@gmail.com
/ol!&orphis&&an! %or&s o% onethings
Substitutability
Overriding Polymorphism means the ability of a
single variable of a given type to be
used to reference objects of diNerentt!pes$ and auto&aticall! call the ðodthat is speci1c to the t!pe o% o"ject thevaria"le re%erences<
l hi
-
8/17/2019 1. Core Java
63/177
/ol!&orphis&
rgupta.mtech@gmail.com
Ever! ani&al sound "utdiNerentl!
7e 9ant to have /ointer o% Ani&alclass assigned "! o"ject o%
derived class
l
-
8/17/2019 1. Core Java
64/177
E'a&ple
rgupta.mtech@gmail.com
d % " l 8
-
8/17/2019 1. Core Java
65/177
eed o% a"stract class8
rgupta.mtech@gmail.com
Sound? @ ðod o%Ani&al class dont &a0ean! sense i
-
8/17/2019 1. Core Java
66/177
A"stract class
rgupta.mtech@gmail.com
I% an class have atleast one a"stractðod it should "edeclare a"stract class<
So&e ti&e i% 9e 9antto stop a progra&&erto create o"ject o%so&e class
Class has so&e de%ault%unctionalit! that can"e used as it is<
Can e'tends onl! one
a"stract class
A" t t l
-
8/17/2019 1. Core Java
67/177
A"stract class use cases
rgupta.mtech@gmail.com
7ant to have so&e de%ault %unctionalit! %ro&"ase class and class have so&e a"stract%unctionalit! that cant "e de1ne at that &o&ent<
ont 9ant to allo9 a progra&&er to createo"ject o% an class as it is too generic
Inter%ace vs< a"stract class
M l
-
8/17/2019 1. Core Java
68/177
More e'a&ple
rgupta.mtech@gmail.com
#i l
-
8/17/2019 1. Core Java
69/177
#inal
rgupta.mtech@gmail.com
7hat is the &eaning o% 1nal Soðing that can not "e change
1nal #inal ðod argu&ents
Cant "e change inside the ðod
#inal varia"le Beco&e constant$ once assigned then cant "e changed
#inal ðod Cant overridden
#inal class Can not inherited ?illing e'tendi"ilit! @
Can "e reuse
So&e e'a&ples<
#i l l
-
8/17/2019 1. Core Java
70/177
#inal class
rgupta.mtech@gmail.com
#inal class cant "e su"class i
-
8/17/2019 1. Core Java
71/177
#inal Method
rgupta.mtech@gmail.com
#inal Method Cant overridden Class containing 1nal ðod can "e
e'tended
illing su"stituta"ilit!
I t % 8
-
8/17/2019 1. Core Java
72/177
Inter%ace8
rgupta.mtech@gmail.com
Inter%ace 2 Contract "9 t9o parties Inter%ace ðod
Onl! declaration
o ðod de1nition
Inter%ace varia"le /u"lic static and 1nal constant
Its ho9 java support glo"al constant
Brea0 the hierarch!
Solve dia&ond pro"le&
Call"ac0 in Java
So&e E'a&ple <
I t % 8
-
8/17/2019 1. Core Java
73/177
Inter%ace8
rgupta.mtech@gmail.com
Rules All inter%ace ðods are al9a!s pu"lic and a"stract$ 9hether 9e sa! it
or not<
Varia"le declared inside inter%ace are al9a!s pu"lic static and 1nal
Inter%ace ðod cant "e static or 1nal
Inter%ace cant have constructor An inter%ace can e'tends other inter%ace
Can "e used pol!&orphicall!
An class i&ple&enting an inter%ace &ust i&ple&ent all o% its ðodother9ise it need to declare itsel% as an a"stract class
I&ple&enting an
-
8/17/2019 1. Core Java
74/177
p e e g ainter%ace
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
75/177
i Ri idit i i #l i"ilit
-
8/17/2019 1. Core Java
76/177
ecreasing Rigidit!
-
8/17/2019 1. Core Java
77/177
!p po"jects
rgupta.mtech@gmail.com
:SE+A (AS+A
IS+A ?Most costl! 8 @
A;7A6S GO #OR ;OOSE CO:/;IG A
(IG( CO(ESIO
But (O78
IS A VS (AS A
-
8/17/2019 1. Core Java
78/177
IS+A VS (AS+A
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
79/177
A6+3
rgupta.mtech@gmail.com
0 i
-
8/17/2019 1. Core Java
80/177
0ession3
rgupta.mtech@gmail.com
0tring class Iuta'ility and usages,
string'uilder and string'u!!er
Wrapper classes and usages
Java 5 6anguage 2eatures )I+-
$uto7o%ing and 4n'o%ing Enhanced 2or loop
Varargs
0tatic Iport
Enus
Inner classes
Regular inner class
ethod local inner class
anonyous inner class
String
-
8/17/2019 1. Core Java
81/177
String
rgupta.mtech@gmail.com
I&&uta"le i
-
8/17/2019 1. Core Java
82/177
I&&uta"ilit!
rgupta.mtech@gmail.com
!mmutabilit means something that cannot be changed.
#trings are immutable in Java. $hat does this mean%
String literals are ver! heavil! used in applications andthe! also occup! a lot o% &e&or!<
There%ore %or eWcient &e&or! &anage&ent$ all the stringsare created and 0ept "! the JVM in a place called stringpool ?9hich is part o% Method Area@<
Gar"age collector does not co&e into string pool<
(o9 does this save &e&or!8
So&e co&&on string
-
8/17/2019 1. Core Java
83/177
gðods
rgupta.mtech@gmail.com
charAt?@ Returns the character located at the speci1ed inde'
concat?@ Appends one String to the end o% another ? XHX also 9or0s@
eQualsIgnoreCase?@ eter&ines the eQualit! o% t9o Strings$ ignoring case
length?@
Returns the nu&"er o% characters in a String
replace?@ Replaces occurrences o% a character 9ith a ne9 character
su"string?@
Returns a part o% a String
to;o9erCase?@ Returns a String 9ith uppercase characters converted
toString?@ Returns the value o% a String
to:pperCase?@ Returns a String 9ith lo9ercase characters converted
tri&?@ Re&oves 9hitespace %ro& the ends o% a String
String co&parison
-
8/17/2019 1. Core Java
84/177
String co&parison
rgupta.mtech@gmail.com
T9o string should never "e chec0ed %or eQualit! using operator 7(68
Al9a!s use eQuals? @ ðod<
String s,india
String s.pa0i
i%?s,
-
8/17/2019 1. Core Java
85/177
7rapper classes
rgupta.mtech@gmail.com
(elps treating pri&itive data as an o"ject But 9h! 9e should convert pri&itive to o"jects8
7e cant store pri&itive in java collections
O"ject have properties and ðods
(ave diNerent "ehavior 9hen passing as ðodargu&ent
Eight 9rapper %or eight pri&itive Integer$ #loat$ ou"le$ Character$ Boolean etc
Integer itne9 Integer?33@
int te&pit
-
8/17/2019 1. Core Java
86/177
Bo'ing ) :n"o'ing Java ,
-
8/17/2019 1. Core Java
87/177
Enhanced #or loop
-
8/17/2019 1. Core Java
88/177
rgupta.mtech@gmail.com
/rovide eas! looping construct to loop through arra!and collections
-
8/17/2019 1. Core Java
89/177
Static I&port
-
8/17/2019 1. Core Java
90/177
Static I&port
rgupta.mtech@gmail.com
(and! %eature in Java ,
-
8/17/2019 1. Core Java
91/177
Enu&s
rgupta.mtech@gmail.com
Enu& is a special t!pe o% classes< enu& t!pe used to put restriction on the instance values
Inner classes
-
8/17/2019 1. Core Java
92/177
Inner classes
rgupta.mtech@gmail.com
Inner class8 A class de1ned inside
another class<
7h! to de1ne inner classes
Inner classes
Top level inner class Method local inner
class
Anon!&ous Innerclass
Method local
argu&ent inner class
Static inner classes
Top level inner class
-
8/17/2019 1. Core Java
93/177
Top level inner class
rgupta.mtech@gmail.com
on static inner class o"ject canYt "e created 9ithout outerclass instance
All the private data o% outer class is availa"le to inner class
on static inner class canYt have static &e&"ers
Method local inner class
-
8/17/2019 1. Core Java
94/177
Method local inner class
rgupta.mtech@gmail.com
Class de1ne inside an ðod Can not access local data de1ne inside ðod
eclare local data 1nal to access it
Anon!&ous Inner class
-
8/17/2019 1. Core Java
95/177
Anon!&ous Inner class
rgupta.mtech@gmail.com
A 9a! to i&ple&ent pol!&orphis& On the !
Session+.
-
8/17/2019 1. Core Java
96/177
Session+.
rgupta.mtech@gmail.com
E'ception handling Tr!$ catch$ thro9$ thro9s$ 1nall!
Chec0ed and :nchec0ed e'ception
:ser de1ned e'ceptions Rule %or Overloading) Overriding in case o%
ðod thro9ing e'ceptions
Assertions
7hat is E'ception8
-
8/17/2019 1. Core Java
97/177
7hat is E'ception8
rgupta.mtech@gmail.com
An e'ception is an a"nor&al condition that arises 9hilerunning a progra&<
E'a&ples2
Atte&pt to divide an integer "! *ero causes an e'ception to "e thro9n at run ti&e<
Atte&pt to call a ðod using a re%erence that is null< Atte&pting to open a none'istent 1le %or reading<
JVM running out o% &e&or!<
E'ception handling do not correct a"nor&al condition
rather it &a0e our progra& ro"ust i
-
8/17/2019 1. Core Java
98/177
T!pe o% e'ceptions
rgupta.mtech@gmail.com
,< :nchec0ed E'ception
Also called Runti&e E'ceptions.< Chec0ed E'ception
Also called Co&pile ti&eE'ceptions
3< Error Should not to "e handled "!
progra&&er
-
8/17/2019 1. Core Java
99/177
E'ceptions and their
-
8/17/2019 1. Core Java
100/177
(andling
rgupta.mtech@gmail.com
7hen the JVM encounters an error such as divide "!*ero$ it creates an e'ception o"ject and thro9s it U asa noti1cation that an error has occurred<
I% the e'ception o"ject is not caught and handledproperl!$ the interpreter 9ill displa! an error andter&inate the progra&<
I% 9e 9ant the progra& to continue 9ith e'ecution o%the re&aining code$ then 9e should tr! to catch thee'ception o"ject thro9n "! the error condition andthen ta0e appropriate corrective actions< This tas0 is0no9n as exception handling<
Co&&on Java E'ceptions
-
8/17/2019 1. Core Java
101/177
Co&&on Java E'ceptions
rgupta.mtech@gmail.com
Arith&eticE'ception Arra!Inde'OutO%BoundE'ception Arra!StoreE'ception #ileot#oundE'ception
IOE'ception U general I)O %ailure ull/ointerE'ception U re%erencing a null o"ject OutO%Me&or!Error Securit!E'ception U 9hen applet tries to per%or& an
action not allo9ed "! the "ro9sers securit! setting< Stac0Overo9Error StringInde'OutO%BoundE'ception
Rules %or thro9ing e'ception %ro&idd th d
-
8/17/2019 1. Core Java
102/177
overridden ðods
rgupta.mtech@gmail.com
Rules %or thro9ing e'ception %ro&t t
-
8/17/2019 1. Core Java
103/177
constructors
rgupta.mtech@gmail.com
Rules %or thro9ing e'ception %ro&l d d d th th d
-
8/17/2019 1. Core Java
104/177
overloaded and other ðods
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
105/177
A6+4
rgupta.mtech@gmail.com
Session+,
-
8/17/2019 1. Core Java
106/177
Session ,
rgupta.mtech@gmail.com
Char and "!te oriented strea&s BuNeredReader and BuNered7riter
#ile handling
O"ject Seriali*ation
KO"jectInputStrea& ) O"jectOutputStrea&L
Strea&
-
8/17/2019 1. Core Java
107/177
Strea&
rgupta.mtech@gmail.com
Strea& is an a"straction that either produces or consu&es in%or&ation< A strea& is lin0ed to a ph!sical device "! the Java I)O s!ste&<
All strea&s "ehave in the sa&e &anner$ even i% the actual ph!sicaldevices to 9hich the! are lin0ed diNer<
. t!pes o% strea&s2
"!te 2 %or "inra! data All "!te strea& class are li0e ZZZZZZZZStrea&
character2 %or te't data
All char strea& class are li0e ZZZZZZZZReader) ZZZZZZ7riter
-
8/17/2019 1. Core Java
108/177
rgupta.mtech@gmail.com
So&e i&portant classes% j i
-
8/17/2019 1. Core Java
109/177
%or& java
-
8/17/2019 1. Core Java
110/177
S!ste& class in java
rgupta.mtech@gmail.com
S!ste& class de1ned in java
-
8/17/2019 1. Core Java
111/177
rgupta.mtech@gmail.com
Reading %or& console BuNeredReader "rne9 BuNeredReader?ne9 InputStrea&Reader?S!ste&
-
8/17/2019 1. Core Java
112/177
e
rgupta.mtech@gmail.com
#ile a"straction thatrepresent 1le anddirectories #ile %ne9 #ile?X
-
8/17/2019 1. Core Java
113/177
rgupta.mtech@gmail.com
Storing the state o% the o"ject on a 1le along so&e &etadata
-
8/17/2019 1. Core Java
114/177
Seriali*ation
rgupta.mtech@gmail.com
The state o% the o"ject %ro& heap is suc0ed and 9rittenalong 9ith so&e &eta data in an 1le<
e+ Seriali*ation
-
8/17/2019 1. Core Java
115/177
rgupta.mtech@gmail.com
7hen an o"ject is de+seriali*ed$ the JVM atte&pts to "ringo"ject "ac0 to the li%e "! &a0ing an ne9 o"ject on theheap that have the sa&e state as original o"ject
Transient varia"le dont get saved during seriali*ationhence co&e 9ith null
(ello 9orld E'a&ple
-
8/17/2019 1. Core Java
116/177
p
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
117/177
7hat is threads8 ;7/
-
8/17/2019 1. Core Java
118/177
rgupta.mtech@gmail.com
Basic %unda&entals
-
8/17/2019 1. Core Java
119/177
rgupta.mtech@gmail.com
Thread2 class in java
-
8/17/2019 1. Core Java
120/177
Java8
rgupta.mtech@gmail.com
I&ple&ents Runna"le inter%ace E'tending Thread class
-
8/17/2019 1. Core Java
121/177
!
rgupta.mtech@gmail.com
I% a thread is "loc0ed? put to sleep@ speci1edno o% &s should e'pire
I% thread 9aiting %or IO
that &ust "e over
-
8/17/2019 1. Core Java
122/177
J g
rgupta.mtech@gmail.com
Thread?@ construct ne9 thread
void run?@ &ust "e overriden
void start?@
start thread call run ðod static void sleep?long &s@
put currentl! e'ecuting thread to sleep %or speci1ed no o% &illisecond
"oolean isAlive?@ return true i% thread is started "ut not e'pired
void stop?@ void suspend?@ and void resu&e?@
Suspend thread e'ecution<
Java
-
8/17/2019 1. Core Java
123/177
rgupta.mtech@gmail.com
void join?long &s@
Main thread 7ait %or speci1ed thread to co&plete ortill speci1ed ti&e is not over
void join?@ Main thread 7ait %or speci1ed thread to co&plete
static "oolean interrupted?@
"oolean isInterrupted?@
void interrupt?@ Send interrupt reQuest to a thread
-
8/17/2019 1. Core Java
124/177
:nderstanding join?@ðod
-
8/17/2019 1. Core Java
125/177
ðod
rgupta.mtech@gmail.com
Chec,ing thread priorities
-
8/17/2019 1. Core Java
126/177
rgupta.mtech@gmail.com
5nderstanding thread snchronization
-
8/17/2019 1. Core Java
127/177
rgupta.mtech@gmail.com
S!nchroni*ation
-
8/17/2019 1. Core Java
128/177
S!nchroni*ation Mechanis& to controls the order in 9hich threads
e'ecute Co&petition vs< cooperative s!nchroni*ation
Mutual e'clusion o% threads Each s!nchroni*ed ðod or state&ent is guarded
"! an o"ject< 7hen entering a s!nchroni*ed ðod or
state&ent$ the o"ject 9ill "e loc0ed until the
ðod is 1nished< 7hen the o"ject is loc0ed "! another thread$ the
current thread &ust 9ait<
rgupta.mtech@gmail.com
:sing thread s!nchroni*ation
-
8/17/2019 1. Core Java
129/177
rgupta.mtech@gmail.com
Inter thread co&&unication
-
8/17/2019 1. Core Java
130/177
rgupta.mtech@gmail.com
Java have elegant Interprocessco&&unication using 9ait?@ noti%!?@ andnoti%!All?@ ðods
All these ðod de1ned 1nal in theO"ject class
Can "e onl! called %ro& a s!nchroni*edconte't
9ait?@ and noti%!?@$ noti%!All?@
-
8/17/2019 1. Core Java
131/177
rgupta.mtech@gmail.com
9ait?@ Tells the calling thread to give up the &onitor and go
to the sleep until so&e other thread enter the sa&e&onitor and call noti%!?@
noti%!?@ 7a0es up the 1rst thread that called 9ait?@ on sa&e
o"ject
noti%!All?@ 7a0es up all the thread that called 9ait?@ on sa&e
o"ject$ highest priorit! thread is going to run 1rst
Incorrect i&ple&entation o% produce consu&er
-
8/17/2019 1. Core Java
132/177
rgupta.mtech@gmail.com
Correct i&ple&entation o% produceconsu&er
-
8/17/2019 1. Core Java
133/177
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
134/177
A6+5
rgupta.mtech@gmail.com
Session+,
-
8/17/2019 1. Core Java
135/177
rgupta.mtech@gmail.com
Session+, O"ject class in Java
Collections #ra&e9or0
;ist$ Map$ Set usages introduction :sages
Arra!;ist$ ;in0ed;ist$ (ashMap$ TreeMap$(ashSet
Co¶"le$ Co¶tor inter%acei&ple&entation
:ser de1ne 0e! in (ashMap
O"ject
-
8/17/2019 1. Core Java
136/177
rgupta.mtech@gmail.com
O"ject is an special class in java de1ned in java
-
8/17/2019 1. Core Java
137/177
toString? @
-
8/17/2019 1. Core Java
138/177
rgupta.mtech@gmail.com
I% 9e do not overridetoString?@ ðod o% O"jectclass it print O"jectIdenti1cation nu&"er "!de%ault
7e can override it to printso&e use%ul in%or&ation<
toString?@
-
8/17/2019 1. Core Java
139/177
rgupta.mtech@gmail.com
eQuals
-
8/17/2019 1. Core Java
140/177
rgupta.mtech@gmail.com
7hat O)/ do !ou e'pect inthis case
O)/ 9ould "e t9o e&plo!ees are noteQuals
-
8/17/2019 1. Core Java
141/177
rgupta.mtech@gmail.com
ont %orget R6 run
-
8/17/2019 1. Core Java
142/177
rgupta.mtech@gmail.com
7henever !ou override eQuals?@%or an t!pe
dont %orget to override hashCode?@ ðod hashCode?@ &a0e S eWcient
7hat hashCode does
(ashCode divide data into "uc0ets EQuals search data %ro& that "uc0et
clone?@
-
8/17/2019 1. Core Java
143/177
rgupta.mtech@gmail.com
;ets consider an o"ject that creation is ver! co&plicated$9hat 9e can do 9e can &a0e an clone o% that o"ject anduse that
Costl! $ avoid using cloning i% possi"le$ internall! dependson seriali*ation
Must &a0e class supporting cloning "! i&ple&enting an&ar0er inter%ace ie Clonea"le
1nali*e?@
-
8/17/2019 1. Core Java
144/177
rgupta.mtech@gmail.com
As !ou are a9are
-
8/17/2019 1. Core Java
145/177
rgupta.mtech@gmail.com
Java collections can "e considered a 0ind o%read!&ade data structure$ 9e should onl!need to 0no9 ho9 to use the& and ho9 the!9or0<
collection a&e o% topic
Collection
Base inter%ace Collections
Static utilit! class provide various use%ul algorith&
collection
-
8/17/2019 1. Core Java
146/177
rgupta.mtech@gmail.com
collection
7h! it is called an %ra&e9or08
Read!&ade ata structure in Java
-
8/17/2019 1. Core Java
147/177
Arra!;ist2 a0a gro9a"le arra!
-
8/17/2019 1. Core Java
148/177
rgupta.mtech@gmail.com
Arra!;ist o% user de1nedo"ject
-
8/17/2019 1. Core Java
149/177
o"ject
rgupta.mtech@gmail.com
Co¶"le and Co¶tor inter%ace
-
8/17/2019 1. Core Java
150/177
rgupta.mtech@gmail.com
7e need to teach Java ho9 to sort userde1ne o"ject
Co¶"le and Co¶tor inter%acehelp us to tell java ho9 to sort userde1ne o"ject<
I&ple&enting Co¶"le
-
8/17/2019 1. Core Java
151/177
rgupta.mtech@gmail.com
Co¶tor
-
8/17/2019 1. Core Java
152/177
rgupta.mtech@gmail.com
ont need to change E&plo!ee class
:se%ul stuN
-
8/17/2019 1. Core Java
153/177
rgupta.mtech@gmail.com
:se%ul e'a&ples
-
8/17/2019 1. Core Java
154/177
rgupta.mtech@gmail.com
:se%ul e'a&ples
-
8/17/2019 1. Core Java
155/177
rgupta.mtech@gmail.com
;in0ed;ist 2 AA ou"l! ;in0 list
-
8/17/2019 1. Core Java
156/177
rgupta.mtech@gmail.com
-
8/17/2019 1. Core Java
157/177
%unda&ental diN "9 Arra!;ist and;in0ed;ist
-
8/17/2019 1. Core Java
158/177
rgupta.mtech@gmail.com
Arra!;ists &anage arra!s internall!< KPLK,LK.LK3LK4LK5L
-
8/17/2019 1. Core Java
159/177
rgupta.mtech@gmail.com
Java i&ple&ents Arra!;ist as arra! internall! (ence good to provide starting si*e
i
-
8/17/2019 1. Core Java
160/177
rgupta.mtech@gmail.com
(ashMap
-
8/17/2019 1. Core Java
161/177
rgupta.mtech@gmail.com
e! ++++]Value declaring an hash&ap
(ashMap\Integer$ String] &ap ne9 (ashMap\Integer$String]?@
/opulating values&ap
-
8/17/2019 1. Core Java
162/177
rgupta.mtech@gmail.com
%or?Integer 0e!2 &ap
-
8/17/2019 1. Core Java
163/177
rgupta.mtech@gmail.com
;in0ed(ashMap A0a< ou"l! lin0 list
0e! and value are in sa&e order in 9hich !ou have inserted
-
8/17/2019 1. Core Java
164/177
rgupta.mtech@gmail.com
ont allo9 duplicate ele&ent
:ser de1ne 0e! in (ashMap
-
8/17/2019 1. Core Java
165/177
rgupta.mtech@gmail.com
I% !ou are using user de1ne 0e! in(ashMap do not %orget to overridehashcode %or that class
7h!8 7e &a! not 1nd that content again
(ashMap vs (ashta"le
-
8/17/2019 1. Core Java
166/177
rgupta.mtech@gmail.com
(ashta"le is threadsa%e$ slo9 asco&pared to (ashMap
Better to use (ashMap
So&e &ore intresting diNerence (ashta"le give runti&e e'ception i% 0e! is
null 9hile (ashMap dont
Session+.
-
8/17/2019 1. Core Java
167/177
rgupta.mtech@gmail.com
Java 5 6anguage 2eatures II
;enerics
$nnotations
;enerics
-
8/17/2019 1. Core Java
168/177
rgupta.mtech@gmail.com
Be%ore Java ,
-
8/17/2019 1. Core Java
169/177
/ol!&orphic "ehaviour
-
8/17/2019 1. Core Java
170/177
rgupta.mtech@gmail.com
\8 e'tends ZZZZZZ]
-
8/17/2019 1. Core Java
171/177
rgupta.mtech@gmail.com
\8 Super ZZZZZ]
-
8/17/2019 1. Core Java
172/177
rgupta.mtech@gmail.com
Generic class
-
8/17/2019 1. Core Java
173/177
rgupta.mtech@gmail.com
Generic ðod
-
8/17/2019 1. Core Java
174/177
rgupta.mtech@gmail.com
Annotation
-
8/17/2019 1. Core Java
175/177
rgupta.mtech@gmail.com
^ &agic
Annotations in Java is all a"out adding &eta+data %acilit! tothe Java Ele&ents<
;i0e Classes$ Inter%aces or Enu&s$ Annotations de1ne a
t!pe in Java and the! can "e applied to several JavaEle&ents<
Tools 9hich 9ill read and interpret the Annotations 9illi&ple&ent a lot o% %unctionalities %ro& the &eta+in%or&ation o"tained<
#or e'a&ple$ the! can ensure the consistenc! "et9eenclasses$ can chec0 the validit! o% the para&ters passed "!
the clients at run ti&e and can generate lot o% "ase code
Built+in Annotations in Java
http://www.flipkart.com/head-first-java-8173666024/p/itmczzj44rzzcpcp?pid=9788173666025&affid=suthukrishhttp://www.flipkart.com/head-first-java-8173666024/p/itmczzj44rzzcpcp?pid=9788173666025&affid=suthukrish
-
8/17/2019 1. Core Java
176/177
rgupta.mtech@gmail.com
There are so&e pre+de1ned annotationsavaila"le in the Java /rogra&&inglanguage< The! are$ Override
eprecated
Suppress7arnings
e&o user de1ne annotation and itsneed
Re%erences2+
-
8/17/2019 1. Core Java
177/177
top related