Define Classes:
Source File Declaration Rules:
A quick review of the rules associated with declaring classes, import statements, and package statements in a source file:
- There can be only one public class per source code file.
- Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here.
- If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.
- If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
- If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file.If there are no package or import statements, the class declaration must be the first line in the source code file.
- import and package statements apply to all classes within a source code file.
In other words, there's no way to declare multiple classes in a file and have
them in different packages or use different imports. - A file can have more than one nonpublic class.
- Files with no public classes can have a name that does not match any of the
classes in the file.
Although in the real world you'll probably use an integrated development environment (IDE) most of the time, you could see a few questions on the exam that use the command line instead, so we're going to review the basics.
Compiling with javac
The javac command is used to invoke Java's compiler. You can specify many options when running javac. For example, there are options to generate debugging information or compiler warnings. Here's the structural overview for javac:
javac [options] [source files]
There are additional command-line options called @argfiles, but they're rarely used, and you won't need to study them for the exam. Both the [options] and the [source files] are optional parts of the command, and both allow multiple entries.
The javac command is used to invoke Java's compiler. You can specify many options when running javac. For example, there are options to generate debugging information or compiler warnings. Here's the structural overview for javac:
javac [options] [source files]
There are additional command-line options called @argfiles, but they're rarely used, and you won't need to study them for the exam. Both the [options] and the [source files] are optional parts of the command, and both allow multiple entries.
The following are both legal javac commands:
javac -help
javac -version Foo.java Bar.java
javac -help
javac -version Foo.java Bar.java
Launching Applications with java
The java command is used to invoke the Java Virtual Machine (JVM). Here's the
basic structure of the command:
The java command is used to invoke the Java Virtual Machine (JVM). Here's the
basic structure of the command:
java [options] class [args]
The [options] and [args] parts of the java command are optional, and they can both have multiple values. (Of the two exams, only the OCP 7 will use [options].) You must specify exactly one class file to execute, and the java command assumes you're talking about a .class file, so you don't specify the class extension on the command line. Here's an example:
java -version MyClass x 1
This command can be interpreted as "Show me the version of the JVM being
used, and then launch the file named MyClass.class and send it two String
arguments whose values are x and 1." Let's look at the following code:
public class MyClass {
public static void main(String[] args) {
System.out.println(args[0] + " " + args[1]);
}
}
It's compiled and then invoked as follows:
java MyClass x 1
The output will be
x 1
java MyClass x 1
The output will be
x 1
Using public static void main(String[ ] args)
The use of the main() method is implied in most of the questions on the exam, and on the OCA exam it is specifically covered. For the .0001% of you who don't know, main() is the method that the JVM uses to start execution of a Java program. First off, it's important for you to know that naming a method main() doesn't give it the superpowers we normally associate with main(). As far as the compiler and the JVM are concerned, the only version of main() with superpowers is the main() with this signature:
The use of the main() method is implied in most of the questions on the exam, and on the OCA exam it is specifically covered. For the .0001% of you who don't know, main() is the method that the JVM uses to start execution of a Java program. First off, it's important for you to know that naming a method main() doesn't give it the superpowers we normally associate with main(). As far as the compiler and the JVM are concerned, the only version of main() with superpowers is the main() with this signature:
public static void main(String[] args)
Other versions of main() with other signatures are perfectly legal, but they're treated as normal methods. There is some flexibility in the declaration of the "special" main() method (the one used to start a Java application): the order of its modifiers can be altered a little, the String array doesn't have to be named args, and as of Java 5 it can be declared using var-args syntax. The following are all legal
declarations for the "special" main():
static public void main(String[] args)
public static void main(String... x)
static public void main(String bang_a_gong[])
Import Statements and the Java API:
There are a gazillion Java classes in the world. The Java API has thousands of classes and the Java community has written the rest. We'll go out on a limb and contend that all Java programmers everywhere use a combination of classes they wrote and classes that other programmers wrote. Suppose we created the following:
public class ArrayList {
public static void main(String[] args) {
System.out.println("fake ArrayList class");
}
}
This is a perfectly legal class, but as it turns out, one of the most commonly used
classes in the Java API is also named ArrayList, or so it seems.... The API
version's actual name is java.util.ArrayList. That's its fully qualified name. The
use of fully qualified names is what helps Java developers make sure that two
versions of a class like ArrayList don't get confused. So now let's say that I want to
use the ArrayList class from the API:
use the ArrayList class from the API:
public class MyClass {
public static void main(String[] args) {
java.util.ArrayList<String> a =
new java.util.ArrayList<String>();
}
}
import statements to the rescue! Instead of the preceding code, our class could
look like this:
look like this:
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
}
}
We can interpret the import statement as saying, "In the Java API there is a
package called 'util', and in that package is a class called 'ArrayList'. Whenever you
see the word 'ArrayList' in this class, it's just shorthand for: 'java.util.ArrayList'."
package called 'util', and in that package is a class called 'ArrayList'. Whenever you
see the word 'ArrayList' in this class, it's just shorthand for: 'java.util.ArrayList'."
As we just implied, a package typically has many classes. The import statement
offers yet another keystroke-saving capability. Let's say you wanted to use a few
different classes from the java.util package: ArrayList and TreeSet. You can
add a wildcard character (*) to your import statement that means, "If you see a
reference to a class you're not sure of, you can look through the entire package for
that class," like so:
offers yet another keystroke-saving capability. Let's say you wanted to use a few
different classes from the java.util package: ArrayList and TreeSet. You can
add a wildcard character (*) to your import statement that means, "If you see a
reference to a class you're not sure of, you can look through the entire package for
that class," like so:
import java.util.*;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
TreeSet<String> t = new TreeSet<String>();
}
}
No comments:
Post a Comment