Oop - Java - Pg-Desd - Session- 5v2

Embed Size (px)

Citation preview

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    1/76

    J2SE Core Java

    PG-DESD

    Aug -2013

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    2/76

    Session 5:

    Learning Objectives

    Explain Packages

    Explain Wrapper Classes

    Describe Exception Handling Mechanism in Java

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    3/76

    Packages

    A package is a grouping of related types (class, interface)providing access protection and name space management.

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    4/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    5/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    6/76

    Creating and Using Packages

    Defining a package :

    package package-name;

    Importing a package :

    import packagename.classname;

    or

    import packagename.*;

    Naming convention :

    double y = java.lang.Math.sqrt(x);

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    7/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    8/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    9/76

    package sadhu;

    public class S{public void msg (){System.out.println("Hello i am from sadhu package!");

    }

    public static void main(String args[]){

    S s=new S();s.msg();

    }}

    //compile // javac -d . S.java// run // java sadhu.S

    Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    10/76

    package cdac;import sadhu.*;class R{

    public void fun(){

    System.out.println(" Hello i am from cdac package");}

    public static void main(String[] args){

    S s1=new S();s1.msg();R r1=new R();r1.fun();

    }}

    // javac -d . R.java---java cdac.R

    Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    11/76

    package sadhu.sreenivas;public class X{public void method () {

    System.out.println("Hello i am from sub package!");}public static void main(String args[]){

    X x=new X();x.method();

    }}

    //compile // javac -d . X.java// run // java sadhu.sreenivas.X

    Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    12/76

    Example

    package cdac;import sadhu.*;

    class SR{

    public void fun(){

    System.out.println(" Hello i am from cdac package");}

    public static void main(String[] args)

    {S s1=new S();s1.msg();SR sr1=new SR();sr1.fun();sadhu.sreenivas.X x1=new sadhu.sreenivas.X();x1.method();

    }}

    // javac -d . R.java---java cdac.R

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    13/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    14/76

    Access Modifiers

    Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    15/76

    15

    Wrapper Classes

    An object of a wrapper class can be used in any situation where aprimitive value will not suffice

    For example, some objects serve as containers of other objects.Primitive values could not be stored in such containers, butwrapper objects could be.

    Another example is that some methods take an Object as aparameter. So a primitive cannot be used, but an object of anyclass can be used.

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    16/76

    16

    Instantiating Wrapper Classes

    All wrapper classes can be instantiated using the corresponding

    primitive type as an argument.

    Integer age = new Integer(40);Double num = new Double(8.2);Character ampersand = new Character(&);

    Boolean isDone = new Boolean(false);

    All wrapper classes EXCEPT Character and Void can beinstantiated using a String argument.

    Integer age = new Integer(40);

    Double num = new Double(8.2);Boolean isDone = new Boolean(true);

    The Void wrapper class cannot be instantiated.

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    17/76

    17

    Wrapper Class Methods

    Wrapper classes all contain various methods that help convertfrom the associated type to another type.

    Integer num = new Integer(4);float flt = num.floatValue();//stores 4.0 in flt

    Double dbl = new Double(8.2);int val = dbl.intValue(); //stores 8 in val

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    18/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    19/76

    19

    Wrapper Class Methods

    Wrapper classes also contain static methodsthat help managethe associated type

    Each numeric class contains a method to convert arepresentation in a String to the associated primitive:

    int num1 = Integer.parseInt(3);double num2 = Double.parseDouble(4.7);long num3 = Long.parseLong(123456789);

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    20/76

    20

    Wrapper Class Constants

    The wrapper classes often contain useful constants as well

    The Integer class contains MIN_VALUEand MAX_VALUEwhichhold the smallest and largest int values

    Other numeric classes will also have MIN_VALUEand MAX_VALUEwhich hold the smallest and largest value for their correspondingtypes.

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    21/76

    21

    Autoboxing

    Autoboxingis the automatic conversionof a primitive value to acorresponding wrapper object:

    Integer obj;int num = 42;obj = num;

    The assignment creates the appropriate Integer object

    The reverse conversion (called unboxing) also occursautomatically as needed

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    22/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    23/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    24/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    25/76

    Exception Handling

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    26/76

    1/24/2014 26

    Kinds of Errors

    Compile time errors

    Run time errors

    Source of Errors

    Input Errors

    Device ErrorsPhysical Limitations

    Code Errors

    Exception Handling

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    27/76

    1/24/2014 27

    What should be done when an error occurred?

    Notify the user if an error occurs

    Save all work

    Allow users to exit from the program

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    28/76

    1/24/2014 28

    What is Exception Handling?

    An exception signifies an illegal, invalid or unexpected issue

    during program execution.

    Since exceptions are almost always assumed to beanticipated, you need to provide appropriate exceptionhandling.

    An exception is an event that occurs during the execution ofa program that disrupts the normal flow of instructions.

    Approaches for dealing with error conditions

    Using Conditional statements and return values

    Use Javas exception handling mechanism

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    29/76

    1/24/2014 29

    Class division by zero

    class DivisionByZeroHandled{

    int c;

    public int compute(int a, int b){

    if(b==0) return 0;elsec=a/b;

    return c;}

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    30/76

    1/24/2014 30

    Handling Exceptions - Java

    Format:

    try

    {

    // Code that may cause an error/exception to occur

    }

    catch (ExceptionType identifier)

    {

    // Code to handle the exception

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    31/76

    1/24/2014 31

    Handling Exceptions: DivisionByZero

    class DivisionByZeroHandled{public static void main(String[] args){

    int a=5,b=0,c=0;try{

    c=a/b;}catch(Exception e){

    System.out.println(e);}System.out.println(Handled Exception");

    }}

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    32/76

    1/24/2014 32

    Handling Exceptions: Result Of Calling ReadLine ()

    try

    {

    System.out.print("Type an integer: ");

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    String s =br.readLine();

    System.out.println("You typed in..." + s);int num = Integer.parseInt (s);

    System.out.println("Converted to an integer..." + num);

    }

    The exception

    can occur here

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    33/76

    1/24/2014 33

    Handling Exceptions: Result Of Calling ParseInt ()

    try

    {

    System.out.print("Type an integer: ");

    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    String s =br.readLine();

    System.out.println("You typed in..." + s);

    num = Integer.parseInt (s);System.out.println("Converted to an integer..." + num);

    }

    The second exception

    can occur here

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    34/76

    1/24/2014 34

    Where The Exceptions Occur In Class Integer

    class Integer

    {

    public Integer (int value);

    public Integer (String s) throwsNumberFormatException;

    public static int parseInt (String s) throwsNumberFormatException;

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    35/76

    1/24/2014 35

    Handling Exceptions: Tracing The Example

    Integer.parseInt (String s)

    {

    :

    :

    }main ()try

    {

    num = Integer.parseInt(s);

    }

    :

    catch (NumberFormatException e)

    {

    :

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    36/76

    1/24/2014 36

    Integer.parseInt (String s)

    {

    }main ()try

    {

    num = Integer.parseInt (s);

    }

    :

    catch (NumberFormatException e)

    {

    :

    }

    Oops!

    The user didnt enter an integer

    Handling Exceptions: Tracing The Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    37/76

    1/24/2014 37

    Integer.parseInt (String s)

    {

    }main ()try

    {

    num = Integer.parseInt (s);

    }

    :

    catch (NumberFormatException e)

    {

    :

    }

    NumberFormatException e =

    new NumberFormatException ();

    Handling Exceptions: Tracing The Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    38/76

    1/24/2014 38

    Integer.parseInt (String s)

    {

    }main ()try

    {

    num = Integer.parseInt (s);

    }

    :

    catch (NumberFormatException e)

    {

    :

    }

    NumberFormatException e =

    new NumberFormatException ();

    Handling Exceptions: Tracing The Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    39/76

    1/24/2014 39

    Integer.parseInt (String s)

    {

    }main ()try

    {

    num = Integer.parseInt (s);

    }

    :

    catch (NumberFormatException e)

    {

    }

    Exception must be dealt with here

    Handling Exceptions: Tracing The Example

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    40/76

    1/24/2014 40

    Handling Exceptions: Catching The Exception

    catch (NumberFormatException e)

    {

    System.out.println(e);

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    41/76

    1/24/2014 41

    Catching The Exception: Error Messages

    catch (NumberFormatException e)

    {

    System.out.println(e.getMessage());

    System.out.println(e);

    e.printStackTrace();

    }

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    42/76

    1/24/2014 42

    catch (NumberFormatException e)

    {

    System.out.println(e.getMessage());

    System.out.println(e);

    e.printStackTrace();

    }}

    }

    For input string: cdac"

    java.lang.NumberFormatExceptio

    For input string: cdac"

    java.lang.NumberFormatException: For input string: cdac"

    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

    at java.lang.Integer.parseInt(Integer.java:426)

    at java.lang.Integer.parseInt(Integer.java:476)

    at Myclass.main(Myclass.java:39)

    Catching The Exception: Error Messages

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    43/76

    R Ti E ti

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    44/76

    Run Time Exceptions

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    45/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    46/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    47/76

    1/24/2014 47

    Checked Exceptions

    Must be handled if the potential for an error exists

    must use a try-catchblock

    Deal with problems that occur in a specific place

    When a particular method invoked enclose it within a try-catch

    block

    Example:

    SQLException, IOException

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    48/76

    1/24/2014 48

    Characteristics Of Unchecked Exceptions

    The compiler doesnt require you to handle them if they arethrown.

    No try-catch block required by the compiler

    They can occur at any time in the program (not just for a specificmethod)

    Typically they are fatal runtime errors that are beyond theprogrammerscontrol

    Use conditional statements rather than the exception handlingmodel.

    Examples:

    NullPointerException,IndexOutOfBoundsException,ArithmeticException

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    49/76

    1/24/2014 49

    Common Unchecked Exceptions: NullPointerException

    int [] arr = null;

    arr[0] = 1;

    arr = new int [4];

    int i;

    for (i = 0; i

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    50/76

    1/24/2014 50

    Common Unchecked Exceptions: ArrayIndexOutOfBoundsException

    int [] arr = null;

    arr[0] = 1;

    arr = new int [4];

    int i;

    for (i = 0; i

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    51/76

    1/24/2014 51

    Common Unchecked Exceptions: ArithmeticExceptions

    int [] arr = null;

    arr[0] = 1;

    arr = new int [4];

    int i;

    for (i = 0; i

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    52/76

    KeywordsException Handling in Java

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    53/76

    y p g

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    54/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    55/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    56/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    57/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    58/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    59/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    60/76

    Th Fi ll Cl

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    61/76

    1/24/2014 61

    The Finally Clause

    An additional part of Javasexception handling model (try-catch-finally).

    Used to enclose statements that must always be executed whether ornot an exception occurs.

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    62/76

    62

    The Finally Clause: Exception Thrown

    try

    {

    f.method();

    }

    catch

    {

    }

    finally

    {

    }

    4) A the end of the catch

    block control transfers

    to the finally clause

    Example1.method ()

    {

    }

    2) Exception thrown here

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    63/76

    63

    The Finally Clause: No Exception Thrown

    try

    {

    f.method();

    }

    catch

    {

    }

    finally

    {

    }

    Example1.method ()

    {

    }

    2) Code runs okay here

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    64/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    65/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    66/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    67/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    68/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    69/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    70/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    71/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    72/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    73/76

    Compile:javac A.java

    Run:java A cdachyd

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    74/76

    Compile :javac A.java

    Run: java A CDAC PG-DAC AUG 2013 Batch

    Output:

    CDAC

    PG-DAC

    AUG

    2013

    Batch

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    75/76

  • 8/13/2019 Oop - Java - Pg-Desd - Session- 5v2

    76/76

    Next .

    Arrays and String