What class or classes is a method that is declared protected available to?

Each instance maintains its own storage. As the result, each instance variable/method has its own copy in the instances and not shared among different instances. To reference an instance variable/method, you need to identify the instance, and reference it via

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
9 or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
0.

A

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable/method has a single common memory location kept in the class and shared by all the instances. The JVM allocates
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable during the class loading. The
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable exists even if no instance is created. A
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable/method can be referenced via
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
5 or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
6. It can also be referenced from any of its instances [but not recommended], e.g.,
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
7 or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
8.

Non-

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods belong to the instances. To use a non-
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable/method, an instance must first be constructed. On the other hand,
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods belong to the class, they are "global" in nature. You need not construct any instance before using them.

The usage of

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods are:

  1. Provide constants and utility methods, such as
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    3,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    4 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    5 which are
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    6 and can be used directly through the class without constructing instances of the class.
  2. Provide a "global" variable, which is applicable to all the instances of that particular class, for purposes such as counting the number of instances, resource locking among instances, and etc.

[I believe that the keyword "

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5" is used to signal it does not change among the instances. In C, a
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable in a function block does not change its value across multiple invocation. C++ extends
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 to denote class variables/methods.]

UML Notation:

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods are underlined in the class diagram.

Examples

Counting the number of instance created - Instance variable won't work!

Suppose that we want to count the number of instances created. Using an instance variable doesn't work?!

This is because

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
1 is an instance variable. Each instance maintains its own
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
1. When a new instance is constructed,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
1 is initialized to 0, then increment to 1 in the constructor.

Use a static variable to count the number of instances

We need to use a "

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5" variable, or class variable which is shared by all instances, to handle the
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
1.

CircleWithStaticCount.javaUsing static variables/methods as "global" variables and "utility" methods

Another usage of "

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5" modifier is to provide "global" variables and "utility" methods that are accessible by other classes, without the need to create an instance of that providing class. For example, the class
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
7 composes purely
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
6 variables and methods. To use the
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable in
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
0 class [such as
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
1 and
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
2] or
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 methods [such as
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
4 or
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
5], you do not have to create an instance of
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
0 class. You can invoke them directly via the class name, e.g.,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
3,
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
8,
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
9,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
4.

Non-

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 [instance] methods: Although from the OOP view point, each instance has its own copy of instance methods. In practice, the instances do not need their own copy, as methods do not have states and the implementation is exactly the same for all the instances. For efficiency, all instances use the copy stored in the class.

Within a class definition, a

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 method can access only
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods. It cannot access non-
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 instance variables/methods, because you cannot identify the instance. On the other hand, an instance method can access
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 and non-
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables/methods. For example,

If a class has only one single instance [known as singleton design pattern], it could be more efficient to use

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable/method for that particular one-instance class?!

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable or methods cannot be hidden or overridden in the subclass as non-
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5.

The Static Initializer

A static initializer is a block of codes labeled

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5. The codes are executed exactly once, when the class is loaded. For example,

During the class loading, JVM allocates the

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables and then runs the
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 initializer. The
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 initializer could be used to initialize
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables or perform an one-time tasks for the class.

The Class Loader

Every JVM has a built-in class loader [of type

// Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
> javac -d d:\yyypackages\bin Cat.java
// Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
5] that is responsible for loading classes into the memory of a Java program. Whenever a class is referenced in the program, the class loader searches the classpath for the class file, loads the bytecode into memory, and instantiates a
// Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
> javac -d d:\yyypackages\bin Cat.java
// Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
6 object to maintain the loaded class.

The class loader loads a class only once, so there is only one

// Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
> javac -d d:\yyypackages\bin Cat.java
// Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
6 object for each class that used in the program. This
// Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
> javac -d d:\yyypackages\bin Cat.java
// Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
8 object stores the static variables and methods.

During the class loading, the class loader also allocates the

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables, and invokes the explicit initializers and
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 initializers [in the order of appearance].

The Instance Initializer

Similarly, you could use the so-called instance initializer, which runs during the instantiation process, to initialize an instance. Instance initializer is rarely-used. For example,

The "Instantiation" Process

The sequence of events when a new object is instantiated via the

1
2
3
4
5
6
7
8
9
10
01 operator [known as the instantiation process] is as follows:

  1. JVM allocates memory for the instance in the heap.
  2. JVM initializes the instance variables to their assigned values or default values.
  3. JVM invokes the constructor.
  4. The first statement of the constructor is always a call to its immediate superclass' constructor. JVM invokes the selected superclass' constructor.
  5. JVM executes the instance initializers in the order of appearance.
  6. JVM executes the body of the constructor.
  7. The
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    01 operator returns a reference to the new object.

For example,

The "final" Class/Variable/Method

You can declare a class, a variable or a method

1
2
3
4
5
6
7
8
9
10
03.

  • A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 class cannot be sub-classed [or extended].
  • A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 method cannot be overridden in the subclasses.
  • A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 variable cannot be re-assigned a new value.
final Variables of Primitive Type vs. Reference Type
  • A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 variable of primitive type is a constant, whose value cannot be changed. A "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    08" variable of primitive type is a global constant, whose value cannot be changed. For example,
  • A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 variable of a reference type [e.g., an instance of a class or an array] cannot be re-assigned a new reference. That is, you can modify the content of the instance, but cannot re-assign the variable to another instance. For example,
final vs. abstract

1
2
3
4
5
6
7
8
9
10
03 is opposite to
1
2
3
4
5
6
7
8
9
10
11. A
1
2
3
4
5
6
7
8
9
10
03 class cannot be extended; while an
1
2
3
4
5
6
7
8
9
10
11 class must be extended and the extended class can then be instantiated. A
1
2
3
4
5
6
7
8
9
10
03 method cannot be overridden; while an
1
2
3
4
5
6
7
8
9
10
11 method must be overridden to complete its implementation. [
1
2
3
4
5
6
7
8
9
10
11 modifier is applicable to class and method only.]

Package, Import, Classpath & JAR

If I have a class called

1
2
3
4
5
6
7
8
9
10
17 and you also have a class called
1
2
3
4
5
6
7
8
9
10
17. Can the two
1
2
3
4
5
6
7
8
9
10
17 classes co-exist or even be used in the same program? The answer is yes, provided that the two
1
2
3
4
5
6
7
8
9
10
17 classes are placed in two different packages.

A package, like a library, is a collection of classes, and other related entities such as interfaces, errors, exceptions, annotations, and enums.

UML Notation: Packages are represented in UML notation as tabbed folders, as illustrated.

Package name [e.g.,

1
2
3
4
5
6
7
8
9
10
21] and classname [e.g.,
1
2
3
4
5
6
7
8
9
10
22] together form the so-called fully-qualified name in the form of
1
2
3
4
5
6
7
8
9
10
23 [e.g.,
1
2
3
4
5
6
7
8
9
10
24], which unambiguously identifies a class.

Packages are used for:

  1. Organizing classes and related entities.
  2. Managing namespaces - Each package is a namespace.
  3. Resolving naming conflicts. For example,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    25 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    26 are treated as two distinct classes. Although they share the same classname
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    17, they belong to two different packages:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    28 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    29. These two classes can co-exist and can even be used in the same program via the fully-qualified names.
  4. Access control: Besides
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 and
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31, you can grant access of a class/variable/method to classes within the same package only.
  5. Distributing Java classes: All entities in a package can be combined and compressed into a single file, known as JAR [Java Archive] file, for distribution.
Package Naming Convention

A package name is made up of the reverse of the domain Name [to ensure uniqueness] plus your own organization's project name separated by dots. Package names are in lowercase. For example, suppose that your Internet Domain Name is "

1
2
3
4
5
6
7
8
9
10
32", you can name your package as "
1
2
3
4
5
6
7
8
9
10
33".

The prefix "

1
2
3
4
5
6
7
8
9
10
34" and "
1
2
3
4
5
6
7
8
9
10
35" are reserved for the core Java packages and Java extensions, e.g.,
1
2
3
4
5
6
7
8
9
10
36,
1
2
3
4
5
6
7
8
9
10
21, and
1
2
3
4
5
6
7
8
9
10
38,
1
2
3
4
5
6
7
8
9
10
39.

Package Directory Structure

The "dots" in a package name correspond to the directory structure for storing the class files. For example, the

1
2
3
4
5
6
7
8
9
10
40 is stored in directory "
1
2
3
4
5
6
7
8
9
10
41" and
1
2
3
4
5
6
7
8
9
10
42 is stored in directory "
1
2
3
4
5
6
7
8
9
10
43", where "
1
2
3
4
5
6
7
8
9
10
44" denotes the base directory of the package.

JVM can locate your class files only if the package base directory and the fully-qualified name are given. The package base directory is provided in the so-called classpath [to be discussed later].

The "dot" does not mean sub-package [there is no such thing as sub-package]. For example,

1
2
3
4
5
6
7
8
9
10
45 and
1
2
3
4
5
6
7
8
9
10
46 are two distinct packages. Package
1
2
3
4
5
6
7
8
9
10
45 is kept in "
1
2
3
4
5
6
7
8
9
10
48"; whereas package
1
2
3
4
5
6
7
8
9
10
46 is stored in "
1
2
3
4
5
6
7
8
9
10
50".

module

JDK 9 introduces a hierarchical level called "module" on top of packages, which will not be covered in this article.

The "import" Statement

There are two ways to reference a class in your source codes:

  1. Use the fully-qualified name in the form of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    51 [such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    24]. For example, Take note that you need to use the fully-qualified name for ALL the references to the class. This is clumpy!
  2. Add an "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    53" statement at the beginning of the source file. You can then use the classname alone [without the package name] in your source codes. For example,

The compiler, when encounter a unresolved classname, will search the

1
2
3
4
5
6
7
8
9
10
54 statements for the fully-qualified name.

The

1
2
3
4
5
6
7
8
9
10
54 statement provides us a convenient way for referencing classes without using the fully-qualified name. "Import" does not load the class, which is carried out by the so-called class loader at runtime. It merely resolves a classname to its fully-qualified name, or brings the classname into the namespace. "Import" is strictly a compiled-time activity. The Java compiler replaces the classnames with their fully-qualified names, and removes all the
1
2
3
4
5
6
7
8
9
10
54 statements in the compiled bytecode. There is a slight compile-time cost but no runtime cost.

The

1
2
3
4
5
6
7
8
9
10
54 statement[s] must be placed after the
1
2
3
4
5
6
7
8
9
10
58 statement but before the class declaration. It takes the following syntax:

import packagename.classname;
import packagename.*

You can

1
2
3
4
5
6
7
8
9
10
54 a single class in an
1
2
3
4
5
6
7
8
9
10
54 statement by providing its fully-qualified name, e.g.,

You can also

1
2
3
4
5
6
7
8
9
10
54 all the classes in a package using the wildcard
1
2
3
4
5
6
7
8
9
10
62. The compiler will search the entire package to resolve classes referenced in the program. E.g.,

Using wildcard may result in slightly fewer source lines. It has no impact on the resultant bytecode. It is not recommended as it lacks clarity and it may lead to ambiguity if two packages have classes of the same names.

The Java core language package

1
2
3
4
5
6
7
8
9
10
36 is implicitly imported to every Java program. Hence no explicit
1
2
3
4
5
6
7
8
9
10
54 statements are needed for classes inside the
1
2
3
4
5
6
7
8
9
10
36 package, such as
1
2
3
4
5
6
7
8
9
10
66,
1
2
3
4
5
6
7
8
9
10
67,
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
0,
1
2
3
4
5
6
7
8
9
10
69 and
1
2
3
4
5
6
7
8
9
10
70.

There is also no need for

1
2
3
4
5
6
7
8
9
10
54 statements for classes within the same package.

Take note that the

1
2
3
4
5
6
7
8
9
10
54 statement does not apply to classes in the default package.

The "import static" Statement [JDK 1.5]

Prior to JDK 1.5, only classes can be "imported" - you can omit the package name for an imported class. In JDK 1.5, the

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables and methods of a class can also be "imported" via the "
1
2
3
4
5
6
7
8
9
10
74" declaration - you can omit the classname for an imported
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable/method. For example:

1
2
3
4
5
6
7
8
9
10

The

1
2
3
4
5
6
7
8
9
10
74 statement takes the following syntax:

Take note that

1
2
3
4
5
6
7
8
9
10
54 and
1
2
3
4
5
6
7
8
9
10
74 statements does not apply to classes/members in the default package.

Creating Packages

To put a class as part of a package, include a

1
2
3
4
5
6
7
8
9
10
58 statement before the class definition [as the FIRST statement in your program]. For example,

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}

You can create and use package in IDE [such as Eclipse/NetBeans] easily, as the IDE takes care of the details. You can simply create a new package, and then create a new class inside the package.

Compiling Classes in Package

To compile classes in package using JDK, you need to use "

1
2
3
4
5
6
7
8
9
10
80" flag to specify the destination package base directory, for example,

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java

The "

1
2
3
4
5
6
7
8
9
10
80" option instructs the compiler to place the class file in the given package base directory, as well as to create the necessary directory structure for the package. Recall that the dot
1
2
3
4
5
6
7
8
9
10
82 in the package name corresponds to sub-directory structure. The compiled bytecode for
1
2
3
4
5
6
7
8
9
10
83 will be placed at "
1
2
3
4
5
6
7
8
9
10
84"

Running Classes in Package

To run the program, you need to set your current working directory at the package base directory [in this case "

1
2
3
4
5
6
7
8
9
10
85"], and provide the fully-qualify name:

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage

It is important to take note that you shall always work in the package base directory and issue fully-qualified name.

As mentioned, if you use an IDE, you can compile/run the classes as usual. IDE will take care of the details.

The Default Unnamed Package

So far, all our examples do not use a

1
2
3
4
5
6
7
8
9
10
58 statement. These classes belong to a so-called default unnamed package. Use of the default unnamed package is not recommended should be restricted to toy programs only, as they cannot be "imported" into another application. For production, you should place your classes in proper packages.

Java Archive [JAR]

An Java application typically involves many classes. For ease of distribution, you could bundles all the class files and relevant resources into a single file, called JAR [Java Archive] file.

JAR uses the famous "zip" algorithm for compression. It is modeled after Unix's "tar" [Tape ARchive] utility. You can also include your digital signature [or certificate] in your JAR file for authentication by the recipients.

JDK provides an utility called "

1
2
3
4
5
6
7
8
9
10
87" to create and manage JAR files. For example, to create a JAR file, issue the following command:

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
Example

To place the earlier class

1
2
3
4
5
6
7
8
9
10
83 [and possible more related classes and resources] in a JAR file called
1
2
3
4
5
6
7
8
9
10
89:

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]

Read "Java Archive [JAR]" for more details.

Classpath - Locating Java Class Files

Java allows you to store your class files anywhere in your file system. To locate a class, you need to provide the package base directory called classpath [short for user class search path] and the fully-qualified name. For example, given that the package base directory is

1
2
3
4
5
6
7
8
9
10
85, the class
1
2
3
4
5
6
7
8
9
10
83 can be found in
1
2
3
4
5
6
7
8
9
10
84.

When the Java compiler or runtime needs a class [given its fully-qualified name], it searches for it from the classpath. You could specify the classpath via the command-line option

1
2
3
4
5
6
7
8
9
10
93 [or
1
2
3
4
5
6
7
8
9
10
94]; or the environment variable
1
2
3
4
5
6
7
8
9
10
95.

A classpath may contain many entries [separated by

1
2
3
4
5
6
7
8
9
10
96 in Windows or
1
2
3
4
5
6
7
8
9
10
97 in Unixes/Mac]. Each entry shall be a package base directory [which contains many Java classes], or a JAR file [which is a single-file archives of many Java classes].

Example on Package, Classpath and JAR

In this example, we shall kept the source files and class files in separate directories - "

1
2
3
4
5
6
7
8
9
10
98" and "
1
2
3
4
5
6
7
8
9
10
99" - for ease of distribution minus the source.

com.zzz.geometry.Circle

Let's create a class called

1
2
3
4
5
6
7
8
9
10
17 in package
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
01. We shall keep the source file as
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
02 and the class file in package base directory of
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
03.

To compile the

1
2
3
4
5
6
7
8
9
10
17 class, use
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
05 with
1
2
3
4
5
6
7
8
9
10
80 option to specify the destination package base directory.

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
com.zzz.geometry.Cylinder

Next, create a class called

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07 in the same package [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
01] that extends
1
2
3
4
5
6
7
8
9
10
17.

No

1
2
3
4
5
6
7
8
9
10
54 statement for
1
2
3
4
5
6
7
8
9
10
17 is needed in
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07, because they are in the same package.

To compile the

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07 class, we need to provide a classpath to the
1
2
3
4
5
6
7
8
9
10
17 class via option
1
2
3
4
5
6
7
8
9
10
93 [or
1
2
3
4
5
6
7
8
9
10
94], because
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07 class references
1
2
3
4
5
6
7
8
9
10
17 class.

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin -cp d:\zzzpackages\bin Cylinder.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Cylinder.class
com.yyy.animal.Cat

Create another class called

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
19 in another package [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
20]. We shall keep the source file as
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
21 and the class file in package base directory of
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
22.

Again, use

1
2
3
4
5
6
7
8
9
10
80 option to compile the
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
19 class. No classpath needed as the
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
19 class does not reference other classes.

// Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
> javac -d d:\yyypackages\bin Cat.java
// Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
myTest.test

We shall write a

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
26 class [in package
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
27] to use all the classes. We shall keep the source file as
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
28 and the class file in package base directory of
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
29.

To compile the

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
26 class, we need
1
2
3
4
5
6
7
8
9
10
80 option to specify the destination and
1
2
3
4
5
6
7
8
9
10
93 to specify the package base directories of
1
2
3
4
5
6
7
8
9
10
17 and
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07 [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
35] and
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
19 [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
22].

1
2
3
4
5
6
7
8
9
10
0

To run the

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
38 class, set the current working directory to the package base directory of
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
39 [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
29] and provide classpath for
1
2
3
4
5
6
7
8
9
10
17 and
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
07 [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
35],
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
19 [
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
22] and the current directory [for
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
39].

1
2
3
4
5
6
7
8
9
10
1Jarring-up com.zzz.geometry package

Now, suppose that we decided to jar-up the

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
01 package into a single file called
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
48 [and kept in
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
49]:

1
2
3
4
5
6
7
8
9
10
2

To run

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
39 with the JAR file, set the classpath to the JAR file [classpath accepts both directories and JAR files].

Separating Source Files and Classes

For ease of distribution [without source files], the source files and class files are typically kept in separate directories.

  1. Eclipse keeps the source files under "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    98", class files under "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    99", and jar files and native libraries under "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    53".
  2. NetBeans keeps the source files under "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    98", class files under "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    55", jar files and native libraries under "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    56".
Two Classes of the Same Classname?

Suppose that we have two

1
2
3
4
5
6
7
8
9
10
17 classes in two different packages, can we use both of them in one program? Yes, however, you need to use fully-qualified name for both of them. Alternatively, you may also import one of the classes, and use fully-qualified name for the other. But you cannot import both, which triggers a compilation error.

How JVM Find Classes

Reference: JDK documentation on "How classes are found".

To locate a class [given its fully-qualified name], you need to locate the base directory or the JAR file.

The JVM searches for classes in this order:

  1. Java Bootstrap classes: such as "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    58" [runtime class], "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    59" [internationalization class],
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    60,
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    61, and others.
  2. Java Standard Extension classes: JAR files located in "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    62" directory [for Windows and Ubuntu]; "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    63" and "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    64" [for Mac]. The location of Java's Extension Directories is kept in Java's System Property "
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    65".
  3. User classes.

The user classes are searched in this order:

  1. The default
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    66, i.e., the current working directory.
  2. The
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    95 environment variable, which overrides the default.
  3. The command-line option
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    93 [or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    94], which overrides the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    95 environment variable and default.
  4. The runtime command-line option
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    71, which override all the above.

The JVM puts the classpath is the system property

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
72. Try running the following line with a
1
2
3
4
5
6
7
8
9
10
93 option and without
1
2
3
4
5
6
7
8
9
10
93 [which uses
1
2
3
4
5
6
7
8
9
10
95 environment variable] to display the program classpath:

1
2
3
4
5
6
7
8
9
10
3javac|java's command-line option -classpath or -cp

I have demonstrated the command-line option

1
2
3
4
5
6
7
8
9
10
94 [or
1
2
3
4
5
6
7
8
9
10
93] in the earlier example.

The CLASSPATH Environment Variable

Alternatively, you could also provide your classpath entries in the

1
2
3
4
5
6
7
8
9
10
95 environment variable. Take note that if
1
2
3
4
5
6
7
8
9
10
95 is not set, the default classpath is the current working directory. However, if you set the
1
2
3
4
5
6
7
8
9
10
95 environment variable, you must include the current directory in the
1
2
3
4
5
6
7
8
9
10
95, or else it will not be searched.

Read "" for more details about

1
2
3
4
5
6
7
8
9
10
95 environment variable.

It is recommended that you use the

1
2
3
4
5
6
7
8
9
10
93 [
1
2
3
4
5
6
7
8
9
10
94] command-line option [customized for each of your applications], instead of setting a permanent
1
2
3
4
5
6
7
8
9
10
95 environment for all the Java applications. IDE [such as Eclipse/NetBeans] manages
1
2
3
4
5
6
7
8
9
10
93 [
1
2
3
4
5
6
7
8
9
10
94] for each of the applications and does not rely on the
1
2
3
4
5
6
7
8
9
10
95 environment.

More Access Control Modifiers – protected and default package-private

Java has four access control modifiers for class/variable/method. Besides the

1
2
3
4
5
6
7
8
9
10
30 [available to all outside classes] and
1
2
3
4
5
6
7
8
9
10
31 [available to this class only], they are two modifiers with visibility in between
1
2
3
4
5
6
7
8
9
10
30 and
1
2
3
4
5
6
7
8
9
10
31:

  • package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93: available to all classes in the same package and the subclasses derived from it.
  • default [package-private]: If the access control modifier is omitted, by default, it is available to classes in the same package only. This is also called package-private accessibility.

Java Source File

A Java source file must have the file type of "

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
94". It can contain at most one top-level
1
2
3
4
5
6
7
8
9
10
30 class, but may contain many non-
1
2
3
4
5
6
7
8
9
10
30 classes [not recommended]. The file name shall be the same as the top-level
1
2
3
4
5
6
7
8
9
10
30 classname.

The source file shall contain statements in this order:

  1. Begins with one optional
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    58 statement. If the
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    58 statement is omitted, the default package [
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    00] is used. Use of default package is not recommended for production.
  2. Follows by optional
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    54 or
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    54
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 statement[s].
  3. Follows by
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    04,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    05 or
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    06 definitions.

Each

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
04,
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
05 or
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
06 is compiled into its own "
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
10" file.

The top-level class must be either

1
2
3
4
5
6
7
8
9
10
30 or default. It cannot be
1
2
3
4
5
6
7
8
9
10
31 [no access to other classes including JVM?!] nor
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
93 [meant for member variables/methods accessible by subclasses], which triggers compilation error "modifier
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
14 not allowed here".

Dissecting the Hello-world

Let us re-visit the "Hello-world" program, which is reproduced below:

1
2
3
4
5
6
7
8
9
10
4
1
2
3
4
5
6
7
8
9
10
5
  • The class
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    15 is declared
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 so that it is accessible by any other classes. In this case, the JRE needs to access the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    15 class to run the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18.
    Try declaring the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    15 class
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31/
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93/package and run the program.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31 and
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93 are not allowed for outer class. package is fine and JRE can also run the program?! What is the use of a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31 class, which is not accessible to others? I will explain the usage of
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31 class later in the so-called inner class.
  • Similarly, the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method is declared
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30, so that JRE can access and invoke the method.
    Try declaring the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31/
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93/package. You can compile the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 with
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31/
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93/package, but cannot run the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method.
  • The
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method is declared
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5. Remember that a
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 variable/method belongs to the class instead of a particular instance. There is no need to create an instance to use a
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 variable/method. A
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 method can be invoked via the classname, in the form of
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    40. JRE can invoke the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    41 method, by calling
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    42 from the class directly. Note that we did not create any instance of the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    15 class.
    Try omitting the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 keyword and observe/explain the error message.
  • The
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method takes an argument of a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    67 array, corresponding to the command-line arguments supplied by the user, performs the program operations, and return
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    47 [or nothing] to the JRE.
    Try omitting the argument
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    48 from the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 method. You can compile, but JRE cannot find the matching
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    50.
  • In C language, the signature of
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    18 function is:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    6Two parameters are needed for the command-line argument -
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    52 to specify the number of arguments and the string-array
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    53 to keep the arguments. In Java, only one parameter - a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    67 array is needed. This is because Java array contains the length internally, and the number of arguments can be retrieved via
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    55.
    Furthermore, in C, the name of the program is passed as the first command-line argument. In Java, the program name is not passed, as the class name is kept within the object. You can retrieve the class name via
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    56.
System.out.println[]

If you check the JDK API specification, you will find that:

  • "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    66" is a class in the package
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    36.
  • "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    59" is a
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    60 variable of the class
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    61.
  • "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    59" is an instance of class "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    63".
  • The class
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    63 provides a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 method called "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    66".

The figure illustrate the classes involved in

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
67.

Take note that each of the dot [

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
00] opens a 3-compartment box!!!

Example

As an example, the reference "

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
69" can be interpreted as follows:

  • "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    70" is a class.
  • "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    71" is a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 variable of class "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    70" [because it is referenced via the classname].
  • The variable "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    71" belongs to a class say "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    76".
  • The class "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    76" provides a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 method "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    79".
  • The "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    79" method returns an instance "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    81" of class say "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    82".
  • The "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    82" class has avariable [
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 or instance] called "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    85".
  • The variable "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    85" belongs to a class say "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    87".
  • The class "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    87" provides a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 method called "
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    90".

Nested and Inner Classes

Read "".

More on Variables and References

Types of Variables

The type of a variable determines what kinds of value the variable can hold and what operations can be performed on the variable. Java is a "strong-type" language, which means that the type of the variables must be known at compile-time.

Java has three kinds of types:

  1. Primitive type: There are eight primitive types in Java:
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    91,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    92,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    93,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    94,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    95,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    96,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    97, and
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    98. A primitive-type variable holds a simple value.
  2. Reference type: Reference types include
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    04,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    05,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    06 and array. A reference-type variable holds a reference to an object or array.
  3. A special
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    02 type, holding a special
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    02 reference. It could be assigned to a reference variable that does not reference any object.

A primitive variable holds a primitive value [in this storage]. A reference variable holds a reference to an object or array in the heap, or

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
02. A references variable can hold a reference of the type or its sub-type [polymorphism]. The value
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
02 is assigned to a reference variable after it is declared. A reference is assigned after the instance is constructed. An object [instance] resides in the heap. It must be accessed via a reference.

Java implicitly defines a reference type for each possible array type - one for each of the eight primitive types and an object array.

Scope & Lifetime of Variables

The scope of a variable refers to the portion of the codes that the variable can be accessed. The lifetime refers to the span the variable is created in the memory until it is destroyed [garbage collected]. A variable may exist in memory but not accessible by certain codes.

Java supports three types of variables of different lifetimes:

Automatic Variable [or Local Variable]: Automatic Variables include method's local variables and method's parameters. Automatic variables are created on entry to the method and are destroyed when the method exits. The scope of automatic variables of a method is inside the block where they are defined. Local variable cannot have access modifier [such as

1
2
3
4
5
6
7
8
9
10
31 or
1
2
3
4
5
6
7
8
9
10
30]. The only modifier applicable is
1
2
3
4
5
6
7
8
9
10
03.

For example,

Member variable [or Instance variable] of a Class: A member variable of a class is created when an instance is created, and it is destroyed when the object is destroyed [garbage collected].

Static variable [or Class variable] of a Class: A

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable of a class is created when the class is loaded [by the JVM's class loader] and is destroyed when the class is unloaded. There is only one copy for a
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variable, and it exists regardless of the number of instances created, even if the class is not instantiated. Take note that
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables are created [during class loading] before instance variables [during instantiation].

Variable Initialization

All class member and

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables that are not explicitly assigned a value upon declaration are assigned a default initial value:

  • "zero" for numeric primitive types:
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    13 for
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    93,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    91,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    92 and
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    94,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    18 for
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    95,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    20 for
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    96;
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    22 [null character] for
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    97;
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    24 for
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    98;
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    02 for reference type [such as array and object].

You can use them without assigning an initial value.

Automatic variables are not initialized, and must be explicitly assigned an initial value before it can be referenced. Failure to do so triggers a compilation error "variable xxx might not have been initialized".

Array Initializer

Array's elements are also initialized once the array is allocated [via the

1
2
3
4
5
6
7
8
9
10
01 operator]. Like member variables, elements of primitive type are initialized to zero or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
24; while reference type are initialized to
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
02. [Take note that C/C++ does not initialize array's elements.]

For example,

You can also use the so-called array initializer to initialize the array during declaration. For example,

1
2
3
4
5
6
7
8
9
10
7

Stack/Heap and Garbage Collector

Where Primitives and Objects Live?

Primitive types, such as

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
93 and
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
96, are created in the program's method stack during compiled time for efficiency [less storage and fast access]. Java's designer retained primitives in a object-oriented language for its efficiency.

Reference types, such as objects and arrays, are created in the "heap" at runtime [via the

1
2
3
4
5
6
7
8
9
10
01 operator], and accessed via a reference. Heap is less efficient as stack, as complex memory management is required to allocate, manage and release storage.

For automatic variable of reference type: the reference is local [allocated in the method stack], but the object is allocated in the heap.

Stack and heap are typically located at the opposite ends of the data memory, to facilitate expansion.

Object References

When a Java object is constructed via the

1
2
3
4
5
6
7
8
9
10
01 operator and constructor, the constructor returns a value, which is a bit pattern that uniquely identifies the object. This value is known as the object reference.

In some JVM implementations, this object reference is simply the address of the object in the heap. However, the JVM specification does not specify how the object reference shall be implemented as long as it can uniquely identify the object. Many JVM implementations use so-called double indirection, where the object reference is the address of an address. This approach facilitates the garbage collector [to be explained next] to relocate objects in the heap to reduce memory fragmentation.

Objects are created via the

1
2
3
4
5
6
7
8
9
10
01 operator and the constructor. The
1
2
3
4
5
6
7
8
9
10
01 operator:

  1. creates a new instance of the given class, and allocate memory dynamically from the heap;
  2. calls one of the overloaded constructors to initialize the object created; and
  3. returns the reference.

For primitives stored in the stack, compiler can determine how long the item lasts and destroy it once it is out of scope. For object in heap, the compiler has no knowledge of the creation and lifetime of the object.

In C++, you must destroy the heap's objects yourself in your program once the objects are no longer in use [via

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
36 operator]. Otherwise, it leads to a common bug known as "memory leak" - the dead objects pile-up and consume all the available storage. On the other hand, destroying an object too early, while it is still in use, causes runtime error. Managing memory explicitly is tedious and error prone, although the programs can be more efficient.

In Java, you don't have to destroy and de-allocate the objects yourself. JVM has a built-in process called garbage collector that automatically releases the memory for an object when there is no more reference to that object. The garbage collector runs in a low priority thread.

An object is eligible for garbage collection when there is no more references to that object. Reference that is held in a variable is dropped when the variable has gone out of its scope. You may also explicitly drop an object reference by setting the object reference to

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
02 to signal to the garbage collector it is available for collection. However, it may or may not get garbage collected because there is no guarantee on when the garbage collector will be run or it will be run at all. The garbage collector calls the object's destructor [a method called
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
38], if it is defined, before releasing the memory back to the heap for re-use.

If a new reference is assigned to a reference variable [e.g., via

1
2
3
4
5
6
7
8
9
10
01 and constructor], the previous object will be available for garbage collection if there is no other references.

System.gc[] & Runtime.gc[]

You can explicitly ask for garbage collection by calling static methods

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
40 or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
41. However, the behavior of these methods is JVM dependent. Some higher priority thread may prevent garbage collector from being run. You cannot rely on the
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
42 methods to perform garbage collection as the JVM specification merely states that "calling this method suggests that the Java Virtual Machine expends effort toward recycling unused objects". So the critical question "When the storage is recovered?" cannot be answered in Java.

Pitfalls of Java

Java's garbage collector frees you from worrying about memory management of objects [no more

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
43 or
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
36 like C/C++] so that you can focus on more productive works. It also insure against so called "memory leak" [i.e., used objects were not de-allocated from memory and slowly fill up the precious memory space]; or releasing object too early which results in runtime error. These are common problems in C/C++ programs.

However, garbage collector does has its drawbacks:

  1. Garbage collector consumes computational resources and resulted in runtime overhead.
  2. The rate of execution is not guarantee and can be inconsistent. This is because JVM specification does not spell out when and how long the garbage collector should be run. This may have an impact on real-time programs, when a response is expected within a certain time, which cannot be interrupted by the garbage collector.

Many programmers prefer to use C++ for game programming and animation, as these programs could create millions of objects in a short span. Managing memory efficiently is critical, instead of relying on garbage collector.

There are some [imperfect] solutions to memory management in Java, e.g.,

  1. Pre-allocate and re-use the objects, instead of creating new objects. This requires effort from programmers.
  2. The author of "jBullet", which is a Java port of the famous Collision Physics library "Bullet Physics", created a library called
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    45, which allocates objects on the method's stack instead of program heap. This improves real-time performance by reducing the frequency of garbage collection.

This solution shall remain imperfect until the Java designers decided to allow programmers to manage the storage, which is not likely.

More on Methods

Passing Arguments into Methods - By Value vs. By Reference

Recall that a method receives arguments from the caller, performs operations defined in the method body, and returns a piece of result or

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
47 to the caller.

To differentiate the parameters inside and outside the method, we have:

  • Actual parameters [or arguments]: The actual values passed into the method and used inside the method.
  • Formal parameters [or method parameters]: The placeholders used in the method definition, which are replaced by the actual parameters when the method is invoked.

For example:

In the above method definition,

// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
47 is a parameter placeholder or formal parameter. If we invoke the method with a variable
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
48 with value of
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
49, i.e.,
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
50,
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
51 is the actual parameter.

Passing Primitive-type Argument into Method - Pass-by-Value

If the argument is a primitive type [e.g.,

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
93 or
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
96], a copy of identical value is created and passed into the method. The method operates on the cloned copy. It does not have access to the original copy. If the value of the argument is changed inside the method, the original copy is not affected. This is called pass-by-value [passing a cloned value into the method].

For example,

1
2
3
4
5
6
7
8
9
10
8
1
2
3
4
5
6
7
8
9
10
9

Although the variables are called number in the caller as well as in the method's formal parameter, they are two different copies with their own scope.

Passing Reference-Type Argument into Method - Also Pass-by-Value

If the argument is a reference type [e.g., an array or an instance of a class], a copy of the reference is created and passed into the method. Since the caller's object and the method's parameter have the same reference, if the method changes the member variables of the object, the changes are permanent and take effect outside the method.

For example,

1
2
3
4
5
6
7
8
9
10
8
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
1

If a method affect values outside the method itself other than the value returned, we say that the method has side-effect. Side effects may not be obvious by reading the method's codes, and must be handled with extreme care, and should be avoided if feasible. Proper comments should be provided in the method's header.

Re-assigning the Reference inside the Method

Since a copy of the reference is passed into the method, if the method re-assigns the reference to the argument, the caller's object and the argument will not have the same reference. Change in the argument will not be reflected in the caller's object.

For example,

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
2
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
3Reference-Type Argument - Pass-by-Reference or Pass-by-value?

As the object parameter can be modified inside the method, some people called it pass-by-reference. However, in Java, a copy of reference is passed into the method, hence, Java designers called it pass-by-value.

Passing a Primitive as a One-Element Array?

Primitive-type parameters are passed-by-value. Hence, the method is not able to modify the caller's copy. If you wish to let the method to modify the caller's copy, you might pass the primitive-type parameter as a one-element array, which is not recommended.

Method Overloading vs. Overriding

An overriding method must have the same argument list; while an overloading method must have different argument list. You override a method in the subclass. You typically overload a method in the same class, but you can also overload a method in the subclass.

A overriding method:

  1. must have the same parameter list as it original.
  2. must have the same return-type or sub-type of its original return-type [since JDK 1.5 - called covariant return-type].
  3. cannot have more restrictive access modifier than its original, but can be less restrictive, e.g., you can override a
    package com.zzz.test;
     
    public class HelloPackage {
       public static void main[String[] args] {
          System.out.println["Hello from a package..."];
       }
    }
    93 method as a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    30 method.
  4. cannot throw more exceptions than that declared in its original, but can throw less exceptions. It can throw exceptions that is declared in its original or their sub-types.
  5. overriding a
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    31 method does not make sense, as private methods are not really inherited by its subclasses.
  6. You cannot override a non-
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 method as
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5, and vice versa.
  7. Technically, a subclass does not override a
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 method, but merely hides it. Both the superclass' and subclass' versions can still be accessed via the classnames.
  8. A
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    03 method cannot be overridden. An
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11 method must be overridden in an implementation subclass [otherwise, the subclass remains
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11].

A overloading method:

  1. must be differentiated by its parameter list. It shall not be differentiated by return-type, exception list or access modifier [which generates compilation error]. It could have any return-type, exception list or access modifier, as long as it has a different parameter list than the others.
  2. can exist in the original class or its sub-classes.

Frequently-Used Packages in JDK API

JDK API is huge and consists of many packages [refer to JDK API specification]. These are the frequently-used packages:

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    36 [the core JDK package]: contains classes that are core to the language, e.g.,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    66,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    67,
    // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    0,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    70,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    69, and etc.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    21: contains utilities such as
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    22,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    71,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    72,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    73,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    74,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    75.
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    76: contains input and output classes for reading files and I/O streams, such as
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    77.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    38: contains networking support, such as
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    79 and
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    80.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    45 [Abstract Windowing Toolkit]: contains classes for implementing a graphical user interface, including classes like
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    82,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    83,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    84.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    46: contains event handling classes, such as key-press, mouse-click etc.
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    86: Advanced GUI classes, e.g.,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    87,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    88,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    89, etc.
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    90: contains classes for implementing Java applets.
  • // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    91: contains classes for database programming, such as
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    92,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    93,
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    94.
  • Many others.

Package java.lang Frequently-used Classes

"

1
2
3
4
5
6
7
8
9
10
36" is the Java core language package, which contains classes central to the Java language. It is implicitly "
1
2
3
4
5
6
7
8
9
10
54ed" into every Java program. That is, no explicit "
1
2
3
4
5
6
7
8
9
10
54" statement is required for using classes in
1
2
3
4
5
6
7
8
9
10
36.

Frequently-used classes in "

1
2
3
4
5
6
7
8
9
10
36" are:

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    67,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    01 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    02:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    67 is immutable whereas
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    01/
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    02 is mutable.
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    01 is thread-safe; while
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    02 is not thread-safe and is meant for single-thread operations.
  • // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    0: contains
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    08 fields
    // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    1 and
    // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    2; and many
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    6 methods such as
    // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    4,
    // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    5,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    15,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    16,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    17,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    18,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    19,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    20,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    21,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    22,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    23, and etc.
  • Wrapper class for primitive types:
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    24,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    69,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    26,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    27,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    28,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    29,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    30, and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    31. The wrapper class is used to wrap a primitive type into a Java class. They are used when a class is needed for purpose such as using multithreading, synchronization and collection. They also contains
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 utility methods [such as
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    5] and
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 constants [such as
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    35].
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    66: contains the
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 variables
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    38,
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    59, and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    40, corresponds to the standard input, output, and error streams.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    70: the common root class for all the Java classes. This common root class defines the baseline behaviors needed to support features like multithreading [lock and monitor], synchronization [
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    42,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    43,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    44], garbage collection,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    45,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    46 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    47.

java.lang.String, StringBuilder & StringBuffer

Read "Java String is Special".

Wrapper Classes for Primitive Types

The designers of Java language retain the primitive types in an object-oriented language, instead of making everything object, so as to improve the runtime efficiency and performance. However, in some situations, an object is required instead of a primitive value. For examples,

  • The data structures in the
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    48 framework, such as the "dynamic array"
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    73 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    50, stores only objects [reference types] and not primitive types.
  • Object is needed to support synchronization in multithreading.
  • Objects are needed, if you wish to modify the arguments passed into a method [because primitive types are passed by value].

JDK provides the so-called wrapper classes that wrap primitive values into objects, for each of the eight primitive types -

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
24 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
91,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
26 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
92,
1
2
3
4
5
6
7
8
9
10
69 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
93,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
27 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
94,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
28 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
95,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
29 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
96,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
30 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
97, and
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
31 for
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
98, as shown in the class diagram.

Wrapper Classes are Immutable

Each of the wrapper classes contains a private member variable that holds the primitive value it wraps. The wrapped value cannot be changed. In other words, all the wrapper classes are immutable.

Wrap via Constructors

Each of the wrapper classes has a constructor that takes in the data type it wraps. For examples:

All wrapper classes, except

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
30, also have a constructor that takes a
1
2
3
4
5
6
7
8
9
10
67, and parse the
1
2
3
4
5
6
7
8
9
10
67 into the primitive value to be wrapped.

Static factory method valueOf[] [JDK 5]

The constructors had been deprecated in JDK 9. You should use

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 factory method
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
71 to construct an instance.

For examples, the following

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
71 are defined in the
1
2
3
4
5
6
7
8
9
10
69 class:

For example,

Unwrap via xxxValue[] methods

The

1
2
3
4
5
6
7
8
9
10
11 superclass
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
75 defines the following
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
76 methods to unwrap, which are implemented in concrete subclasses
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
24,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
26,
1
2
3
4
5
6
7
8
9
10
69,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
27,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
28,
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
29. In other words, you can get an
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
93 or
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
96 value from an
1
2
3
4
5
6
7
8
9
10
69 object.

Similarly, the

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
30 and
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
31 classes have a
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
88 and
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
89, respectively.

ExampleConstants - MIN_VALUE, MAX_VALUE and SIZE

All wrapper classes [except

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
31] contain the following constants, which give the minimum, maximum, and bit-length.

For examples:

Static Methods for Parsing Strings

Each of the wrapper classes [except

// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
30] also contain a
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 method to parse a given
1
2
3
4
5
6
7
8
9
10
67 into its respective primitive value:

For examples:

Auto-Boxing & Auto-Unboxing [JDK 1.5]

Prior to JDK 1.5, the programmers have to explicitly wrap a primitive value into an object, and explicitly unwrap an object to get a primitive value. For example,

The pre-JDK 1.5 approach involves quite a bit of code to do the wrapping and unwrapping. Why not ask the compiler to do the wrapping and unwrapping automatically? JDK 1.5 introduces a new feature called auto-boxing and unboxing, where the compiler could do the wrapping and unwrapping automatically for you based on their contexts. For example:

With the auto-boxing and unboxing, your can practically ignore the distinction between a primitive and its wrapper object.

java.lang.Math - Mathematical Functions & Constants

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
7 class provides mathematical constants [
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
1 and
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
2] and functions [such as
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
4,
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
5]. A few functions are listed below for references. Check the JDK API specification for details.

For examples:

Take note that

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
0 class is
1
2
3
4
5
6
7
8
9
10
03 - you cannot create subclasses. The constructor of
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
0 class is
1
2
3
4
5
6
7
8
9
10
31 - you cannot create instances.

java.lang.Object - The Common Java Root Class

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
03 is the superclass of all Java classes. In other words, all classes are subclass of
1
2
3
4
5
6
7
8
9
10
70 - directly or indirectly. A reference of class
1
2
3
4
5
6
7
8
9
10
70 can hold any Java object, because all Java classes are subclasses of
1
2
3
4
5
6
7
8
9
10
70. In other word, every Java class "is a"
1
2
3
4
5
6
7
8
9
10
70.

Java adopts a single common root class approach in its design, to ensure that all Java classes have a set of common baseline properties. The Object class defines and implements all these common attributes and behaviors that are necessary of all the Java objects running under the JVM. For example,

  • Ability to compare itself to another object, via
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    45 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    46.
  • Provides a text string description, via
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    47.
  • Inter-thread communication, via
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    42,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    43 and
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    44.
  • Automatic garbage collection.

The

1
2
3
4
5
6
7
8
9
10
70 class has the following
1
2
3
4
5
6
7
8
9
10
30 methods:

  • The method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    16 returns a runtime representation of the class in a
    // Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
    > javac -d d:\yyypackages\bin Cat.java
    // Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
    8 object. A
    // Set current working directory to source file [d:\yyypackages\src\com\yyy\animal]
    > javac -d d:\yyypackages\bin Cat.java
    // Output class file is d:\yyypackages\bin\com\yyy\animal\Cat.class
    8 object exists for all the objects in Java. It can be used, for example, to discover the fully-qualified name of a class, its members, its immediate superclass, and the interfaces that it implemented. For example,
  • The method
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    47 returns a text string description of the object's current state, which is extremely useful for debugging. The
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    47 is implicitly called by
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    66 and the string concatenation operator
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    22. The default implementation in
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    70 returns the classname followed by it hash code [in hexadecimal] [e.g.,
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    24]. This method is meant to be overridden in the subclasses.
  • The method
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    45 defines a notion of object equality, based on the object's contents rather than their references. However, the default implementation in
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    70 class use "
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    27" which compares the object's references. This method is meant to be overridden in the subclasses to compare the content via "deep" comparison, rather than references. The
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    45 shall be reflective and transitive, i.e.,
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    29 is
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    30,
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    31 shall be
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    30; if
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    29 and
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    34 are
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    30, then
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    36 shall be
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    30.
  • The method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    38 maps an object into a hash value. The same object must always produce the same hash value. Two objects which are
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    45 shall also produce the same hash value.
  • The method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    40 is used to make a duplicate of an object. It creates an object of the same type from an existing object, and initializes the new object’s member variables to have the same value as the original object. The object to be cloned must implement
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    41 interface. Otherwise, a
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    42 will be thrown. For reference type variable, only the reference is cloned, not the actual object.
  • The methods
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    42,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    43,
    // To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
    > jar cvf myjarfile.jar c1.class ... cn.class
    44 are used in concurrent [multithread] programming.
  • The method
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    38 is run before an object is destroyed [i.e., destructor]. It can be used for cleanup operation before the object is garbage-collected.

java.lang.System

The

1
2
3
4
5
6
7
8
9
10
66 class contains three
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 variables
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
49,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
50 and
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
51, corresponding to the standard input, output and error streams, respectively.

The
1
2
3
4
5
6
7
8
9
10
66 class also contains many useful
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 methods, such as:
  • // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    54: terminate the program with the return code.
  • // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    55 &
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    56: get the current time in milliseconds and nanoseconds. These methods can be used for accurate timing control.
  • // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    57: retrieving all the system properties.

java.lang.Runtime

Every Java program is associated with an instance of

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
58, which can be obtained via the static method
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
59. You can interface with the operating environment via this
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
58, e.g.,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
61 launches the
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
62 in a separate process.

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
4

Package java.util Frequently-Used Classes

java.util.Random

Although

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
9 method can be used to generate a random double between
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
64, the
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
65 class provides more extensive operations on random number, e.g., you can set a random number generator with a initial seed value, to generate the same sequence of random values repeatedly.

Example

Example: Simulating throw of 3 dice.

java.util.Scanner & java.util.Formatter [JDK 1.5]

Read "".

java.util.Arrays

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
66 class contains various
// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 methods for manipulating arrays, such as comparison, sorting and searching.

For examples,

  • The
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    5 method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    69, compare the contents of two
    // Set the current working directory to the directory containing HelloPackage.java
    > javac -d e:\myproject HelloPackage.java
    
    93 arrays and return boolean
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    30 or
    // Set the current working directory to the package base directory
    e:\myproject> java com.zzz.test.HelloPackage
    
    24.
  • The static method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    73 sorts the given array in ascending numerical order.
  • The static method
    // Set the current working directory to the package base directory [i.e., e:\myproject]
    e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
    added manifest
    adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
    74 searches the given array for the specified value using the binary search algorithm.
  • others

Example: See "Collection Framework".

java.util.Collection

See "Collection Framework".

Package java.text Frequently-Used Classes

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
75 package contains classes and interfaces for handling text, dates, numbers and currencies with locale [internationalization] support.

[TODO] compare with [JDK 1.5]

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
76 and format specifiers and
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
77/
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
78 - check for locale support.

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
79/
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80 and
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
81/
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
82 supports both output formatting [number/date -> string] and input parsing [string -> number/date] in a locale-sensitive manner for internationalization [i18n].

java.text.NumberFormat

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
79 class can be used to format numbers and currencies for any locale. To format a number for the current Locale, use one of the static factory methods:

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
5

The available factory methods are:

The default currency format rounds the number to two decimal places; the default percent format rounds to the nearest integral percent; the default integer format rounds to the nearest integer.

Example 1
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
6
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
7
package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
8Example 2

In this example, we use

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 method
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
85 to retrieve all supported locales, and try out
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
86,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
87,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
88,
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
89.

package com.zzz.test;
 
public class HelloPackage {
   public static void main[String[] args] {
      System.out.println["Hello from a package..."];
   }
}
9

You can also use the

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
79 to parse an input string [represent in the locale] to a
// To create a JAR file from c1 ... cn classes [c:create, v:verbose, f:filename]:
> jar cvf myjarfile.jar c1.class ... cn.class
75:

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
0

java.text.DecimalFormat

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80 class is a subclass of
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
79, which adds support for formatting floating-point numbers, such as specifying precision, leading and trailing zeros, and prefixes and suffixes. A
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80 object has a pattern to represent the format of the decimal number, e.g., "
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
95", where
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
13 denotes zero padding, and
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
97 without the zero-padding.

To use a

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80 with the default locale, invoke its constructor with the pattern, e.g.,

To use a

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80 with locale, get a
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
79 by calling the
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
86 and downcast it to
// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
80. For example,

java.text.DateFormat

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
81 class can be used to format a date instance with locale.

Read "Date and Time".

To format a date/time for the current locale, use one of the

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
5 factory methods:

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
1

The available factory methods for getting a

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
81 instance are:

The exact display for each style depends on the locales, but in general,

  • // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    06 is completely numeric, such as 12.13.52 or 3:30pm
  • // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    07 is longer, such as Jan 12, 1952
  • // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    08 is longer, such as January 12, 1952 or 3:30:32pm
  • // Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
    > javac -d d:\zzzpackages\bin Circle.java
    // Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
    09 is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

You can also use the

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
81 to parse an input string containing a date in the locale to a
// Set the current working directory to the package base directory
e:\myproject> java com.zzz.test.HelloPackage
72 object.

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
2

java.text.SimpleDateFormat

The

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
82 is a concrete subclass of
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
13 for formatting and parsing dates in a locale-sensitive manner. It supports output formatting [date to string], input parsing [string to date], and normalization.

You can construct a

// Set the current working directory to the package base directory [i.e., e:\myproject]
e:\myproject> jar cvf hellopackage.jar com\zzz\test\HelloPackage.class
added manifest
adding: com/zzz/test/HelloPackage.class[in = 454] [out= 310][deflated 31%]
82 via one of its constructors:

// Set the current working directory to the directory containing HelloPackage.java
> javac -d e:\myproject HelloPackage.java
3

For example, [TODO]

Writing Javadoc

A great feature in Java is the documentation can be integrated with the source codes, via the so-called JavaDoc [Java Documentation] comments. In other languages, documentation typically is written in another file, which easily gets out-of-sync with the source codes.

JavaDoc comments begin with

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
15 and end with
// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
16. They are meant for providing API documentation to the users of the class. JavaDoc comments should be provided to describe the class itself; and the
1
2
3
4
5
6
7
8
9
10
30 variables, constructors, and methods.

You can use JDK utility

// Set current working directory to source file [d:\zzzpackages\src\com\zzz\geometry]
> javac -d d:\zzzpackages\bin Circle.java
// Output class file is d:\zzzpackages\bin\com\zzz\geometry\Circle.class
18 to extract these comments automatically and produce API documentation in a standard format.

With JavaDoc comments, you can keep the program documentation inside the same source file instead of using another documentation file. This provides ease in synchronization.

JavaDoc comments and API documentation are important for others to re-use your program. Write JavaDoc comments while you are writing the program. Do not leave them as after-thought.

What class or classes is a variable available to if it is declared without an access modifier?

A class that is declared without any access modifiers is said to have package or friendly access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

Which modifier is used to specify that a method Cannot be used outside a class?

Any variable, method, or class declared to use the default access modifier cannot be accessed by any other class outside of the package from which it was declared. Above are some ways of using the default access modifier for a variable or method.

What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it?

What modifier should you use on a class so that a class in the same package can access it but a class [including a subclass] in a different package cannot access it? > Use the default modifier.

Chủ Đề