1. Core Java

Embed Size (px)

Citation preview

  • 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

    [email protected]

     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!+,

    [email protected]

    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!+.

    [email protected]

      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

    [email protected]

    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

    [email protected]

    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+,

    [email protected]

  • 8/17/2019 1. Core Java

    9/177

    Session+,

    [email protected]

    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

    [email protected]

    Java

  • 8/17/2019 1. Core Java

    11/177

    (o9 Sun de1ne Java8

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    13/177

  • 8/17/2019 1. Core Java

    14/177

    So&e co&&on "u**9ords

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    16/177

    :nderstanding JVM

    [email protected]

  • 8/17/2019 1. Core Java

    17/177

    (ello 7orld

    [email protected]

    ;a" set+up   Java ,

  • 8/17/2019 1. Core Java

    18/177

    Anal!sis o% (ello 7orld

    [email protected]

  • 8/17/2019 1. Core Java

    19/177

     Java /rogra& Structure

    [email protected]

    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

    [email protected]

    . 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

    [email protected]

      "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

    [email protected]

    i%

  • 8/17/2019 1. Core Java

    23/177

    Arra!

    [email protected]

    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+.

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    26/177

    7hat is an O"ject8

    [email protected]

  • 8/17/2019 1. Core Java

    27/177

    Class

    [email protected]

  • 8/17/2019 1. Core Java

    28/177

    Classes and O"ject

    [email protected]

  • 8/17/2019 1. Core Java

    29/177

    Basic principles o% OO

    [email protected]

  • 8/17/2019 1. Core Java

    30/177

    A"straction

    [email protected]

  • 8/17/2019 1. Core Java

    31/177

    Encapsulation

    [email protected]

  • 8/17/2019 1. Core Java

    32/177

    Modularit!

    [email protected]

  • 8/17/2019 1. Core Java

    33/177

    (ierarch!

    [email protected]

  • 8/17/2019 1. Core Java

    34/177

    A "it a"out :M; diagra&

    [email protected]

    :M; .

  • 8/17/2019 1. Core Java

    35/177

    A6+.

    [email protected]

  • 8/17/2019 1. Core Java

    36/177

    Session+,

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    38/177

    Creating Classes and object

    [email protected]

  • 8/17/2019 1. Core Java

    39/177

    Correct 9a!8

    [email protected]

  • 8/17/2019 1. Core Java

    40/177

    Constructors: default, parameterized and cop

    [email protected]

    Initiali*e state o% the o"ject Special &ethod 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

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    43/177

    this 2 Constructor chaining8

    [email protected]

    Calling oneconstructor %ro&another 8

  • 8/17/2019 1. Core Java

    44/177

    Static &ethod)varia"le

    [email protected]

    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 &ethod can onl! accessstatic data o% the class

    #or calling static &ethod 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"

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    48/177

  • 8/17/2019 1. Core Java

    49/177

     Java #oundation /ac0ages

    [email protected]

     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

    [email protected]

    #or instance varia"le and &ethods  /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

    [email protected]

    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

    [email protected]

    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

    [email protected]

     java

  • 8/17/2019 1. Core Java

    54/177

    Call "! value

    [email protected]

     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

    [email protected]

     Java dont support call "! re%erence<

    7hen !ou pass an o"ject in an &ethod 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+.

    [email protected]

    Inheritance

     T!pe o% inheritance$ dia&ond pro"le&

    InstanceO% operator

    #inal

    #inal varia"le$ 1nal &ethod $ 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

    [email protected]

    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

    [email protected]

    (ierarch! inheritance can leads to poor design

  • 8/17/2019 1. Core Java

    59/177

    Inheritance e'a&ple

    [email protected]

    :se e'tends 0e!9ord

    :se super to pass valuesto "ase class constructor<

  • 8/17/2019 1. Core Java

    60/177

    Overloading

    [email protected]

    Overloading deals 9ith &ultiple &ethods in the sa&e class 9ith the sa&e na&e "utdiNerent &ethod signatures<

    Both the a"ove &ethods have the sa&e &ethod na&es "ut diNerent &ethodsignatures$ 9hich &ean the &ethods 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

    [email protected]

    Overriding deals 9ith t9o &ethods$ one in the parent class andthe other one in the child class and has the sa&e na&e andsignatures<

    Both the a"ove &ethods have the sa&e &ethod na&es and the

    signatures "ut the &ethod in the su"class MyClass overrides the&ethod 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&

    [email protected]

    /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 &ethodthat 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&

    [email protected]

    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

    [email protected]

    d % " l 8

  • 8/17/2019 1. Core Java

    65/177

    eed o% a"stract class8

    [email protected]

    Sound? @ &ethod o%Ani&al class dont &a0ean! sense i

  • 8/17/2019 1. Core Java

    66/177

    A"stract class

    [email protected]

    I% an class have atleast one a"stract&ethod 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

    [email protected]

    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

    [email protected]

    #i l

  • 8/17/2019 1. Core Java

    69/177

    #inal

    [email protected]

    7hat is the &eaning o% 1nal  So&ething that can not "e change

    1nal  #inal &ethod argu&ents

    Cant "e change inside the &ethod

      #inal varia"le Beco&e constant$ once assigned then cant "e changed

      #inal &ethod 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

    [email protected]

    #inal class cant "e su"class i

  • 8/17/2019 1. Core Java

    71/177

    #inal Method

    [email protected]

    #inal Method Cant overridden Class containing 1nal &ethod can "e

    e'tended

    illing su"stituta"ilit!

    I t % 8

  • 8/17/2019 1. Core Java

    72/177

    Inter%ace8

    [email protected]

    Inter%ace 2 Contract "9 t9o parties Inter%ace &ethod

    Onl! declaration

    o &ethod 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

    [email protected]

    Rules  All inter%ace &ethods 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 &ethod 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 &ethodother9ise 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

    [email protected]

  • 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

    [email protected]

    :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

    [email protected]

  • 8/17/2019 1. Core Java

    79/177

    A6+3

    [email protected]

    0 i

  • 8/17/2019 1. Core Java

    80/177

    0ession3

    [email protected]

    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

    [email protected]

    I&&uta"le i

  • 8/17/2019 1. Core Java

    82/177

    I&&uta"ilit!

    [email protected]

    !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&ethods

    [email protected]

    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

    [email protected]

     T9o string should never "e chec0ed %or eQualit! using operator 7(68

    Al9a!s use eQuals? @ &ethod<

    String s,india

    String s.pa0i

    i%?s,

  • 8/17/2019 1. Core Java

    85/177

    7rapper classes

    [email protected]

    (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 &ethods

    (ave diNerent "ehavior 9hen passing as &ethodargu&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

    [email protected]

    /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

    [email protected]

    (and! %eature in Java ,

  • 8/17/2019 1. Core Java

    91/177

    Enu&s

    [email protected]

    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

    [email protected]

    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

    [email protected]

    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

    [email protected]

    Class de1ne inside an &ethod Can not access local data de1ne inside &ethod

    eclare local data 1nal to access it

    Anon!&ous Inner class

  • 8/17/2019 1. Core Java

    95/177

    Anon!&ous Inner class

    [email protected]

    A 9a! to i&ple&ent pol!&orphis& On the !

    Session+.

  • 8/17/2019 1. Core Java

    96/177

    Session+.

    [email protected]

    E'ception handling   Tr!$ catch$ thro9$ thro9s$ 1nall!

      Chec0ed and :nchec0ed e'ception

     

    :ser de1ned e'ceptions  Rule %or Overloading) Overriding in case o%

    &ethod thro9ing e'ceptions

      Assertions

    7hat is E'ception8

  • 8/17/2019 1. Core Java

    97/177

    7hat is E'ception8

    [email protected]

    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 &ethod 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

    [email protected]

    ,< :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

    [email protected]

    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

    [email protected]

    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 &ethods

    [email protected]

    Rules %or thro9ing e'ception %ro&t t

  • 8/17/2019 1. Core Java

    103/177

    constructors

    [email protected]

    Rules %or thro9ing e'ception %ro&l d d d th th d

  • 8/17/2019 1. Core Java

    104/177

    overloaded and other &ethods

    [email protected]

  • 8/17/2019 1. Core Java

    105/177

    A6+4

    [email protected]

    Session+,

  • 8/17/2019 1. Core Java

    106/177

    Session ,

    [email protected]

    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&

    [email protected]

    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

    [email protected]

    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

    [email protected]

    S!ste& class de1ned in java

  • 8/17/2019 1. Core Java

    111/177

    [email protected]

    Reading %or& console BuNeredReader "rne9 BuNeredReader?ne9 InputStrea&Reader?S!ste&

  • 8/17/2019 1. Core Java

    112/177

    e

    [email protected]

    #ile a"straction thatrepresent 1le anddirectories  #ile %ne9 #ile?X

  • 8/17/2019 1. Core Java

    113/177

    [email protected]

    Storing the state o% the o"ject on a 1le along so&e &etadata

  • 8/17/2019 1. Core Java

    114/177

    Seriali*ation

    [email protected]

     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

    [email protected]

    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

    [email protected]

  • 8/17/2019 1. Core Java

    117/177

    7hat is threads8 ;7/

  • 8/17/2019 1. Core Java

    118/177

    [email protected]

    Basic %unda&entals

  • 8/17/2019 1. Core Java

    119/177

    [email protected]

     Thread2 class in java

  • 8/17/2019 1. Core Java

    120/177

     Java8

    [email protected]

    I&ple&ents Runna"le inter%ace E'tending Thread class

  • 8/17/2019 1. Core Java

    121/177

    !

    [email protected]

    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

    [email protected]

     Thread?@ construct ne9 thread

    void run?@ &ust "e overriden

    void start?@

     start thread call run &ethod 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

    [email protected]

    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?@&ethod

  • 8/17/2019 1. Core Java

    125/177

    &ethod

    [email protected]

    Chec,ing thread priorities

  • 8/17/2019 1. Core Java

    126/177

    [email protected]

    5nderstanding thread snchronization

  • 8/17/2019 1. Core Java

    127/177

    [email protected]

    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 &ethod or state&ent is guarded

    "! an o"ject< 7hen entering a s!nchroni*ed &ethod or

    state&ent$ the o"ject 9ill "e loc0ed until the

    &ethod is 1nished< 7hen the o"ject is loc0ed "! another thread$ the

    current thread &ust 9ait<

    [email protected]

    :sing thread s!nchroni*ation

  • 8/17/2019 1. Core Java

    129/177

    [email protected]

    Inter thread co&&unication

  • 8/17/2019 1. Core Java

    130/177

    [email protected]

     Java have elegant Interprocessco&&unication using 9ait?@ noti%!?@ andnoti%!All?@ &ethods

    All these &ethod 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

    [email protected]

    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

    [email protected]

    Correct i&ple&entation o% produceconsu&er

  • 8/17/2019 1. Core Java

    133/177

    [email protected]

  • 8/17/2019 1. Core Java

    134/177

    A6+5

    [email protected]

    Session+,

  • 8/17/2019 1. Core Java

    135/177

    [email protected]

    Session+,  O"ject class in Java

      Collections #ra&e9or0

     

    ;ist$ Map$ Set usages introduction  :sages

    Arra!;ist$ ;in0ed;ist$ (ashMap$ TreeMap$(ashSet

     

    Co&para"le$ Co&parator inter%acei&ple&entation

      :ser de1ne 0e! in (ashMap

    O"ject

  • 8/17/2019 1. Core Java

    136/177

    [email protected]

    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

    [email protected]

    I% 9e do not overridetoString?@ &ethod 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

    [email protected]

    eQuals

  • 8/17/2019 1. Core Java

    140/177

    [email protected]

    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

    [email protected]

    ont %orget R6 run

  • 8/17/2019 1. Core Java

    142/177

    [email protected]

    7henever !ou override eQuals?@%or an t!pe

    dont %orget to override hashCode?@ &ethod 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

    [email protected]

    ;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

    [email protected]

    As !ou are a9are

  • 8/17/2019 1. Core Java

    145/177

    [email protected]

     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

    [email protected]

    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

    [email protected]

    Arra!;ist o% user de1nedo"ject

  • 8/17/2019 1. Core Java

    149/177

    o"ject

    [email protected]

    Co&para"le and Co&parator inter%ace

  • 8/17/2019 1. Core Java

    150/177

    [email protected]

    7e need to teach Java ho9 to sort userde1ne o"ject

    Co&para"le and Co&parator inter%acehelp us to tell java ho9 to sort userde1ne o"ject<

    I&ple&enting Co&para"le

  • 8/17/2019 1. Core Java

    151/177

    [email protected]

    Co&parator

  • 8/17/2019 1. Core Java

    152/177

    [email protected]

    ont need to change E&plo!ee class

    :se%ul stuN 

  • 8/17/2019 1. Core Java

    153/177

    [email protected]

    :se%ul e'a&ples

  • 8/17/2019 1. Core Java

    154/177

    [email protected]

    :se%ul e'a&ples

  • 8/17/2019 1. Core Java

    155/177

    [email protected]

     ;in0ed;ist 2 AA ou"l! ;in0 list

  • 8/17/2019 1. Core Java

    156/177

    [email protected]

  • 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

    [email protected]

    Arra!;ists &anage arra!s internall!<  KPLK,LK.LK3LK4LK5L

  • 8/17/2019 1. Core Java

    159/177

    [email protected]

     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

    [email protected]

    (ashMap

  • 8/17/2019 1. Core Java

    161/177

    [email protected]

    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

    [email protected]

    %or?Integer 0e!2 &ap

  • 8/17/2019 1. Core Java

    163/177

    [email protected]

    ;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

    [email protected]

    ont allo9 duplicate ele&ent

    :ser de1ne 0e! in (ashMap

  • 8/17/2019 1. Core Java

    165/177

    [email protected]

    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

    [email protected]

    (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

    [email protected]

    Java 5 6anguage 2eatures II

    ;enerics

    $nnotations

    ;enerics

  • 8/17/2019 1. Core Java

    168/177

    [email protected]

    Be%ore Java ,

  • 8/17/2019 1. Core Java

    169/177

    /ol!&orphic "ehaviour

  • 8/17/2019 1. Core Java

    170/177

    [email protected]

    \8 e'tends ZZZZZZ]

  • 8/17/2019 1. Core Java

    171/177

    [email protected]

    \8 Super ZZZZZ]

  • 8/17/2019 1. Core Java

    172/177

    [email protected]

    Generic class

  • 8/17/2019 1. Core Java

    173/177

    [email protected]

    Generic &ethod

  • 8/17/2019 1. Core Java

    174/177

    [email protected]

    Annotation

  • 8/17/2019 1. Core Java

    175/177

    [email protected]

    ^ &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

    [email protected]

     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