Friday, 22 December 2006

SCJP Chapter 10

Development
After the wringer that is threads, this is a light read.

javac and java
The commands javac and java are used to compile and run java files. The command-line options that are available include -classpath (-cp), -d and -D. Multiple options and files must be separated by spaces.
The -classpath or -cp option are paths that will be searched for classes. The dot (.) indicates searching the current directory. Any subdirectories listed will be searched but not the directories above them. With packages, classpaths become trickier. The root of the package must be in a subdirectory that is in the classpath, making it visible. It is important to understand the relative and absolute paths. With JAR files, one must include the name at the end of the path.
The -d option tells the compiler where to put the class file that is generated. A compile error occurs if the destination directory does not exist. On the other hand, with a java file in a package, javac will build the package directories if none exist.
The -D option creates a system property with the syntax of name=value following immediately with no spaces in-between, eg -Dkey=property. Should the value have spaces, it should be enclosed with quotes. Java provides a java.util.Properties class to obtain information on the system as well as adding more user properties. Two methods, setProperty() and getProperty() facilitate setting and getting a property. setProperty() takes two strings for the name-value pair and getProperty() returns a string while either taking one string for the name value or two strings, a name value and a default value if the property does not exist (similar to setProperty()).
For java, exactly one class file is executed every time it is invoked, with no need for the extension. The argument declaration for the main method is no longer fixed to String[] args. The name of the string array can be changed and the var-args syntax may be used. String... x, String port[] and String[] ___ are all legal.

Static Imports
Static imports are a convenience-only feature that allows less typing. Beginning with the syntax: import static, which can only be used on static object references, constants (which are static and final) and static methods. Beware of ambiguously named static members, two classes may have a constant with the same name. If used, Java will not know which class to refer to.

No comments: