Chap 02

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Programming Java

Classes & Methods

Incheon Paik

1 Computer Industry lab.


Java
Contents
 The Structure of Method
 Static Methods & Static Variables
 Instance Methods & Variables
 The Integer Class
 The New Operator
 Garbage Collection
 Other Wrapper Classes
 The StringBuffer Class
 Arrays of Objects
 Command-Line Arguments
 The System Class

2 Computer Industry lab.


Java
The Structure of Method

Method

retType mthName(paramList) { void displayInts (int array[]) {


}
}

Exception Definition of Method

retType mthName(paramList) throws exceptionList { int parse(String s) throws


ExceptionX { }
}

Method Invocation

mthName(args);

3 Computer Industry lab.


Java
Static Methods & Variables

Invocation of Static Method

Clsname.mthName(args); double x = Math.sin(3.12);

Access to Static Variable

clsName.varName

Class Aclass {
Object x Object y
static int a;
a same a
int b;
…….. b different b

} x.a = 10; x.b = 20;


Class Aclass x = new Aclass(); y.a = 30; y.b = 40;
Class Aclass y = new Aclass();
Now, x.a = ? x.b = ?

4 Computer Industry lab.


Java
Static Methods & Variables
class AreaCircumference {
public static void main(String args[]) {
System.out.println(“Max of –8 and –4 is “ + Math.max(-8, -4));
System.out.println(“Min of –8 and –4 is “ + Math.min(-8, -4));
double radius = 5;
double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;
System.out.println(“Radius is “ + radius);
System.out.println(“Area is “ + area);
System.out.println(“Circumference is “ + circumference);
}
}

5 Computer Industry lab.


Java
Instance Methods & Variables

Invocation of Instance Method

objRef.mthName.mthName(args); double x = myObjA.somemth();

class DollarAmount {
public static void main(String args[]) {
String s1 = “The total cost is $45.67”;
int i1 = s1.indexOf(‘$’); Find out this position

String s2 = s1.substring(i1); Get substring after ‘$’ character

System.out.println(s2);
} // end of main
} // end of class

6 Computer Industry lab.


Java
The Integer Wrapper Class
Wrapper Class : Corresponding class to the basic type: Number, Integer,
Float, Double,Boolean, Character, Byte, Short, Long

class StringToInt {
public static void main(String args[]) {
String s = “125”;
Integer obj = Integer.valueOf(s);
int i = obj.intValue();
i += 10;
System.out.println(i); 135
} // end of main
} // end of class

7 Computer Industry lab.


Java
The New Operator

new Operator

clsName objRef = new clsName(args); MyClass x = new MyClass();

class IntegerTest {
public static void main(String args[]) {
Integer iobj1 = new Integer(5);
int i1 = iobj1.intValue();
System.out.println(“i1 = “ + i1); i1 = 5

System.out.println(iobj1); 5

} // end of main
} // end of class

8 Computer Industry lab.


Java
Garbage Collection
 Garbage Collector
 Automatic Memory Management

 finalize Method
 Call the finalize method before the garbage collector reclaim

the memory
 Provide the method to release the resources
 Programmer can remove the resources(ex:open files) directly

using finalize method which garbage collector cannot


reclaim.

9 Computer Industry lab.


Java
Garbage Collection
protected void finalize() throws Throwable {
// ...
}

public class GcTest{


static {
System.out.println("Initialize");
}

protected void finalize() throws Throwable {


System.out.println("Clean Up");
}

public static void main(String args[]) { How can we display Clean Up


System.out.println("In main .... "); message? => In Exercise
GcTest x = new GcTest();
x = null;
System.gc(); // We need to call this point to see the "Clean Up"
}
}

10 Computer Industry lab.


Java
The Double Wrapper Class
class DoubleDemo {
public static void main(String args[]) {
// variable initialize
double d1 = 0;
double d2 = 0;
double d3 = d1 / d2;
Result :
System.out.println(d3); NaN
System.out.println(Double.isInfinite(d3)); False
System.out.println(Double.isNaN(d3)); True

System.out.println(Double.MAX_VALUE); 1.797E308
4.9E-324
System.out.println(Double.MIN_VALUE);
Infinity
System.out.println(Double.POSITIVE_INFINITY);
-Infinity
System.out.println(Double.NEGATIVE_INFINITY);
}
}

11 Computer Industry lab.


Java
The StringBuffer Class
StringBuffer Constructor

StringBuffer()
StringBuffer(int size)
StringBuffer(String s)
class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer(30);
StringBuffer sb3 = new StringBuffer(“abcde”);
System.out.println(“sb1.capacity = “ + sb1.capacity()); 16
System.out.println(“sb1.length = “ + sb1. length()); 0
System.out.println(“sb2.capacity = “ + sb1.capacity()); 30
System.out.println(“sb2.length = “ + sb1. length()); 0 What will be append()
System.out.println(“sb3.capacity = “ + sb1.capacity()); 21 and reverse() ?
System.out.println(“sb3.length = “ + sb1. length()); 5
} // end of main
} // end of class
12 Computer Industry lab.
Java
Arrays of Objects
Declaration of Array
Make the Space for Array Element

class StringArray {
public static void main(String args[]) {
String array[] = new String[5];
array[0] = “String 0”;
array[1] = “String 1”;
System.out.println(array.length);
System.out.println(array[0]);
System.out.println(array[1]);
} // end of main
} // end of class

13 Computer Industry lab.


Java
Command-Line Argument
Declaration of Array
Make the Space for Array Element

java CommandLineArguments 1 2 abcde


class CommandLineArguments {
public static void main(String args[]) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
} // end of main
} // end of class

14 Computer Industry lab.


Java
The System Class

Runtime Environment System.out.println( ****)


Static variable of PrintStream

Exit() Method

void exit(int code)

currentTimeMillis() Method

long currentTimeMillis()

Method Invocation

Void arraycopy(Object source, int sourceIndex,


Object destination, int destinationIndex, int size)

15 Computer Industry lab.


Java
Exercise
 Step 1 (Read Input)
 C Language
scanf(“%s”, name);
 Java
BufferedReader charStream = New BufferedReader(new InputStreamReader(Syst
em.in));
String data = charStream.readLine();
 Step 2 (Read Input Double)
import java.io.*;  You need to write this statement at top of your program
double d = Double.valueOf(charStream.readLine().trim()).doubleValue();

 Step 3 Using System.in.read() and StringBuffer


StringBuffer buf = new StringBuffer();
String
while ((c = (char) System.in.read()) != '\r')
buf.append(c);
RandomAccessFile file = new RandomAccessFile(buf.toString(), "rw");

16 Computer Industry lab.


Java
Exercise
 Step 4 (OptionPane of Swing)
 Show Dialog and Get Input from GUI

 Step 5 (Garbage Collection)


 Refer to this slide #9,10

17 Computer Industry lab.


Java

You might also like