J.E.D.I.
Figure 4.1: The opened registry editor
4.6 The System Class
The System class provides many useful fields and methods such as the standard input, the standard output and a utility method for fast copying of a part of an array. Here are
some interesting methods from the class. Note that all the classs methods are static.
System Methods
public static void arraycopyObject src, int srcPos, Object dest, int destPos, int length
Copies length items from the source array src starting at srcPos to dest starting at index destPos. Faster than manually programming the code for this yourself.
public static long currentTimeMillis Returns the difference between the current time and January 1, 1970 UTC. Time
returned is measured in milliseconds. public static void exitint status
Kills the Java Virtual Machine JVM running currently. A non-zero value for status by convention indicates an abnormal exit.
public static void gc Runs the garbage collector, which reclaims unused memory space for recycling.
public static void setInInputStream in Changes the stream associated with System.in, which by default refers to the keyboard.
public static void setOutPrintStream out Changes the stream associated with System.out, which by default refers to the console.
Table 13: Some methods of class System
Introduction to Programming II Page 67
J.E.D.I.
Heres a demo on some of these methods. import java.io.;
class SystemDemo { public static void mainString args[] throws IOException {
int arr1[] = new int[5000000]; int arr2[] = new int[5000000];
long startTime, endTime; initialize arr1
for int i = 0; i arr1.length; i++ { arr1[i] = i + 1;
} copying manually
startTime = System.currentTimeMillis; for int i = 0; i arr1.length; i++ {
arr2[i] = arr1[i]; }
endTime = System.currentTimeMillis; System.out.printlnTime for manual copy: +
endTime-startTime + ms.; using the copy utility provided by java – the
arraycopy method startTime = System.currentTimeMillis;
System.arraycopyarr1, 0, arr2, 0, arr1.length; endTime = System.currentTimeMillis;
System.out.printlnTime wthe use of arraycopy: +
endTime-startTime + ms.; System.gc;
force garbage collector to work System.setInnew FileInputStreamtemp.txt;
in NetBeans, temp.txt should be located in the project folder
System.exit0; }
}
The program gives varying output. It may generate the following sample output: Time for manual copy: 32 ms.
Time wthe use of arraycopy: 31 ms.
Introduction to Programming II Page 68
J.E.D.I.
5 Text-Based Applications
5.1 Objectives