session4 module5-6 array string package accessmodifiers

Upload: tuan-nguyen

Post on 03-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    1/25

    Session 4

    Module 5: Arrays/Strings/Otherstring classes

    Module 6: Packages , Access

    Modifiers

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    2/25

    Arrays & String / Package & Modifiers / Session4 / 2 of 25

    Module 4 - Review

    Class = data + methods

    Object = an instance of a class

    Instance variable: a variable contains a reference toan object.

    Method: a behavior of a class (code)

    Constructor: a method which is executed when anobject is created.

    Initializer: Codes which will be executed just after anobject is created to set the initial values to data ofobject.

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    3/25

    Arrays & String / Package & Modifiers / Session4 / 3 of 25

    Module 5 Objectives

    Arrays

    String

    StringBuilder StringTokenizer

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    4/25

    Arrays & String / Package & Modifiers / Session4 / 4 of 25

    Definition

    An array is a special data store that can hold several

    items of a single data type in contiguous memory

    locations.

    Benefits Arrays are the best means of operating on multiple data

    elements of the same type at the same time.

    Arrays make optimum use of memory resource as

    compared to variables. Memory can be assigned to an array only at the time when

    the array is actually used.

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    5/25

    Arrays & String / Package & Modifiers / Session4 / 5 of 25

    Declaring:

    Type[][] m;// or Type m[][];

    Allocating memory:

    m = new Type[RowNo][ColNo];

    Element accessing:

    m[r_index][c_index] // index >= 0

    Traversing a 2D array(Type[][] m)

    for (int i=0; i

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    6/25

    Arrays & String / Package & Modifiers / Session4 / 6 of 25

    String class

    Class forimmutable text data.

    Strings are constant, which means

    their values cannot be changed after

    they are created. Declare:

    String S1 = new String();

    String s2 = "Hello";

    S2

    1000

    e

    l

    l

    o

    H

    1000

    (in java.lang package)

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    7/25

    Arrays & String / Package & Modifiers / Session4 / 7 of 25

    String class

    Common-used methods (1)

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    8/25

    Arrays & String / Package & Modifiers / Session4 / 8 of 25

    String class

    Common-used methods (2)

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    9/25

    Arrays & String / Package & Modifiers / Session4 / 9 of 25

    String Arrays

    String[] list = { "EPC", "HDJ",

    "DWMX", "SQL",};

    for (int i=0;i< list.length;i++)

    System.out.println(list[i]);

    HDJ

    EPC

    DWMX

    SQL

    list

    2000

    4000

    5000

    3000

    1000

    3000

    2000

    4000

    5000

    1000

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    10/25

    Arrays & String / Package & Modifiers / Session4 / 10 of 25

    Passing command line

    arguments

    classA{ ..

    publicstaticvoid main (String[] args){

    // manipulate with args[i]

    }}

    // run program

    java A arg0 arg1 arg2

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    11/25

    Arrays & String / Package & Modifiers / Session4 / 11 of 25

    StringBuilder classStore a growable and flexible string.

    Characters or strings can be inserted in the stringBuilder object and they

    can also be appended at the end.

    Constructors

    (in java.lang package)

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    12/25

    Arrays & String / Package & Modifiers / Session4 / 12 of 25

    StringBuilder class

    Common-used methods

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    13/25

    Arrays & String / Package & Modifiers / Session4 / 13 of 25

    StringTokenizer class Class for break a string (tokenizer function) into subunits (called tokens)

    based on a specific delimiter. The most common delimiter is whitespace which yields words as the

    tokens.

    Tom persues JerryTom

    persues

    Jerry

    delimiter = " "

    subunits(called tokens)

    break a string

    (tokenizer function)

    (in java.util package)

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    14/25

    Arrays & String / Package & Modifiers / Session4 / 14 of 25

    StringTokenizer class

    Common-used methods

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    15/25

    Arrays & String / Package & Modifiers / Session4 / 15 of 25

    Module 6 - Objectives

    Packages

    Access Modifiers

    Field and method Modifiers

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    16/25

    Arrays & String / Package & Modifiers / Session4 / 16 of 25

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    17/25

    Arrays & String / Package & Modifiers / Session4 / 17 of 25

    Predefined Packages

    You can view its

    content with

    WINZAR/ WINZIP

    importjava.util.*;importjava.io.*;

    class A

    {

    }

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    18/25

    Arrays & String / Package & Modifiers / Session4 / 18 of 25

    User-defined Packages

    Create a packagepackage my_package;

    class A {

    .

    } Use the package

    import my_package.A;

    class B{

    .

    void method3() {A obj = new A();

    ..

    }

    }

    fully qualified name of the class(the name of the class including

    its package name.)

    must be the first line

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    19/25

    Arrays & String / Package & Modifiers / Session4 / 19 of 25

    Access Modifiers

    Access specifiers (modifiers) are used to control the

    access of classes and class members.

    The access specifiers also determine whether

    classes and the members of the classes can beinvoked by other classes or interfaces.

    Accessibility affects inheritance and how members

    are inherited by the subclass.

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    20/25

    Arrays & String / Package & Modifiers / Session4 / 20 of 25

    Access control keyword

    public

    protected

    private

    no specifier

    (default

    or package)

    interface

    class

    member of

    interface/class

    Free-accessing

    (even outside its package)

    package/class/ subclass

    outside cannot access

    (only within its own class)

    package

    Access level Applied tokeywords

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    21/25

    Arrays & String / Package & Modifiers / Session4 / 21 of 25

    Rules for access control

    Constructor is a special method, so it is certainly a member of class

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    22/25

    Arrays & String / Package & Modifiers / Session4 / 22 of 25

    Access control keywords

    Access

    modifier

    Elements Visible in

    Class Package Subclass Outside

    package

    public yes yes yes yes

    protected yes yes yes no

    private yes no no no

    no modifier yes yes no no

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    23/25

    Arrays & String / Package & Modifiers / Session4 / 23 of 25

    Field and method Modifiers

    Keywords for specifying changing permission

    volatile(dbini) modifier (applied only to fields) Allows the content of a variable to be synchronized across all running

    threads. This modifier is not frequently used

    final(cui cng) modifier

    native(ccb) modifier Used only with methods and indicates that the implementation of the method

    is in a language other than in Java.

    transient(tmthi) modifier Is used to declare fields that are not saved or restored as a part of the state

    of the object.

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    24/25

    Arrays & String / Package & Modifiers / Session4 / 24 of 25

    Rules for using Fields and

    Methods modifier

  • 7/28/2019 Session4 Module5-6 Array String Package AccessModifiers

    25/25

    Arrays & String / Package & Modifiers / Session4 / 25 of 25

    Module 5, 6 - Summary

    Arrays

    String

    StringBuilder StringTokenizer

    Packages

    Access Modifiers

    Field and methodModifiers