Chap 02
Chap 02
Chap 02
Incheon Paik
Method
Method Invocation
mthName(args);
clsName.varName
Class Aclass {
Object x Object y
static int a;
a same a
int b;
…….. b different b
class DollarAmount {
public static void main(String args[]) {
String s1 = “The total cost is $45.67”;
int i1 = s1.indexOf(‘$’); Find out this position
System.out.println(s2);
} // end of main
} // end of class
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
new Operator
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
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
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);
}
}
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
Exit() Method
currentTimeMillis() Method
long currentTimeMillis()
Method Invocation