Académique Documents
Professionnel Documents
Culture Documents
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.
Module 5 Objectives
Arrays String StringBuilder StringTokenizer
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.
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
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
String Arrays
5000
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
StringBuilder 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.
delimiter = " " Tom persues Jerry Tom persues Jerry break a string (tokenizer function)
Arrays & String / Package & Modifiers / Session4 / 13 of 25
Module 6 - Objectives
Packages Access Modifiers Field and method Modifiers
Predefined Packages
User-defined Packages
Create a package
fully qualified name of the class (the name of the class including its package name.)
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.
public
interface protected class
private
package
member of interface/class
yes yes no no
only to fields)
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.
Module 5, 6 - Summary
Arrays String StringBuilder StringTokenizer