Vous êtes sur la page 1sur 25

Session 4

Module 5: Arrays/Strings/Other string classes


Module 6: Packages , Access Modifiers

Module 4 - Review

Class = data + methods Object = an instance of a class Instance variable: a variable contains a reference to an object. Method: a behavior of a class (code) Constructor: a method which is executed when an object is created. Initializer: Codes which will be executed just after an object is created to set the initial values to data of object.

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

Module 5 Objectives
Arrays String StringBuilder StringTokenizer

Arrays & String / Package & Modifiers / Session4 / 3 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.

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

Types of arrays
for (int i=0; i<ar.length; i++) // for (Type var : ar)// JDK 1.5 { // if (condition) process a[i]; } Type [] ar;

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<m.length;i++){ for (int j=0; j<m[i].length;j++){ // if (condition) process m[i][j]; } }
Arrays & String / Package & Modifiers / Session4 / 5 of 25

String class

(in java.lang package)

Class for immutable text data. Strings are constant, which means their values cannot be changed after they are created.
Declare: String S1 = new String(); String s2 = "Hello";
1000

o l l

e
H

S2

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

String class Common-used methods (1)

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

String class Common-used methods (2)

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

String Arrays
5000

SQL DWMX EPC HDJ

String[] list = { "EPC", "HDJ", 4000 "DWMX", "SQL",}; for (int i=0;i< list.length;i++) 3000 System.out.println(list[i]);
2000

1000

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

Passing command line arguments


class A{ .. public static void main (String[] args){ // manipulate with args[i] } } // run program java A arg0 arg1 arg2

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

StringBuilder class

(in java.lang package)

Store 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

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

StringBuilder class Common-used methods

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

StringTokenizer class (in java.util package)


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.

delimiter = " " Tom persues Jerry Tom persues Jerry break a string (tokenizer function)
Arrays & String / Package & Modifiers / Session4 / 13 of 25

subunits (called tokens)

StringTokenizer class Common-used methods

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

Module 6 - Objectives
Packages Access Modifiers Field and method Modifiers

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

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

Predefined Packages

You can view its content with WINZAR/ WINZIP

import java.util.*; import java.io.*; class A { }


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

User-defined Packages

Create a package

package my_package; class A { . must be the first line }

fully qualified name of the class (the name of the class including its package name.)

Use the package

import my_package.A; class B{ . void method3() { A obj = new A(); .. } } Arrays & String / Package & Modifiers / Session4 / 18 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 be invoked by other classes or interfaces. Accessibility affects inheritance and how members are inherited by the subclass.

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

Access control keyword


Access level keywords Applied to

Free-accessing (even outside its package)


package/class/ subclass

public
interface protected class

outside cannot access (only within its own class)

private

package

no specifier (default or package)

member of interface/class

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

Rules for access control

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

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

Access control keywords


Elements Visible in Access modifier Class Package Subclass Outside package yes no no no

public protected private no modifier

yes yes yes yes

yes yes no yes

yes yes no no

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

Field and method Modifiers


Keywords for specifying changing permission

volatile (d bin i) 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 (cc b) modifier

Used only with methods and indicates that the implementation of the method is in a language other than in Java. Is used to declare fields that are not saved or restored as a part of the state of the object.

transient (tm thi) modifier

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

Rules for using Fields and Methods modifier

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

Module 5, 6 - Summary
Arrays String StringBuilder StringTokenizer

Packages Access Modifiers Field and method Modifiers

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

Vous aimerez peut-être aussi