When you overload a java method, you write multiple methods with a shared name.
In summary, we can define a simply-named method (with the parameters needed to do the calculation) and a body that hides the "messy" calculation. Then we can place it in a class, debug it, and forget about it. We are always only one method name away from hiding whatever complexity a program requires. By layering methods, we can quickly amplify their powers. Show
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.java9 or // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage0. A // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5 variable during the class loading. The // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5 variable/method can be referenced via // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage5 or // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage6. 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.HelloPackage7 or // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage8. Non- // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5 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.java5 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.java5 variables/methods are:
(I believe that the keyword " // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5" 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.java5 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.java5 to denote class variables/methods.) UML Notation: // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 variables/methods are underlined in the class diagram. ExamplesCounting 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 instancesWe need to use a " // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5" 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" methodsAnother usage of " // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5" 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.class6 variables and methods. To use the // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.class0 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.class1 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.class2) or // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.class4 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.class5), 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.class0 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.class3, // 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.class8, // 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.class9, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class4. Non- // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 (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.java5 method can access only // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 variables/methods. It cannot access non- // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5 and non- // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5 variable/method for that particular one-instance class?! // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.java5. The Static InitializerA static initializer is a block of codes labeled // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5. 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.java5 variables and then runs the // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 initializer. The // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 initializer could be used to initialize // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 variables or perform an one-time tasks for the class. The Class LoaderEvery 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.class5) 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.class6 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.class6 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.class8 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.java5 variables, and invokes the explicit initializers and // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 initializers (in the order of appearance). The Instance InitializerSimilarly, 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" ProcessThe sequence of events when a new object is instantiated via the 1 2 3 4 5 6 7 8 9 1001 operator (known as the instantiation process) is as follows:
For example, The "final" Class/Variable/MethodYou can declare a class, a variable or a method 1 2 3 4 5 6 7 8 9 1003.
final Variables of Primitive Type vs. Reference Type
final vs. abstract1 2 3 4 5 6 7 8 9 1003 is opposite to 1 2 3 4 5 6 7 8 9 1011. A 1 2 3 4 5 6 7 8 9 1003 class cannot be extended; while an 1 2 3 4 5 6 7 8 9 1011 class must be extended and the extended class can then be instantiated. A 1 2 3 4 5 6 7 8 9 1003 method cannot be overridden; while an 1 2 3 4 5 6 7 8 9 1011 method must be overridden to complete its implementation. [ 1 2 3 4 5 6 7 8 9 1011 modifier is applicable to class and method only.] Package, Import, Classpath & JARIf I have a class called 1 2 3 4 5 6 7 8 9 1017 and you also have a class called 1 2 3 4 5 6 7 8 9 1017. Can the two 1 2 3 4 5 6 7 8 9 1017 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 1017 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 1021) and classname (e.g., 1 2 3 4 5 6 7 8 9 1022) together form the so-called fully-qualified name in the form of 1 2 3 4 5 6 7 8 9 1023 (e.g., 1 2 3 4 5 6 7 8 9 1024), which unambiguously identifies a class. Packages are used for:
Package Naming ConventionA 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 1032", you can name your package as " 1 2 3 4 5 6 7 8 9 1033". The prefix " 1 2 3 4 5 6 7 8 9 1034" and " 1 2 3 4 5 6 7 8 9 1035" are reserved for the core Java packages and Java extensions, e.g., 1 2 3 4 5 6 7 8 9 1036, 1 2 3 4 5 6 7 8 9 1021, and 1 2 3 4 5 6 7 8 9 1038, 1 2 3 4 5 6 7 8 9 1039. Package Directory StructureThe "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 1040 is stored in directory " 1 2 3 4 5 6 7 8 9 1041" and 1 2 3 4 5 6 7 8 9 1042 is stored in directory " 1 2 3 4 5 6 7 8 9 1043", where " 1 2 3 4 5 6 7 8 9 1044" 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 1045 and 1 2 3 4 5 6 7 8 9 1046 are two distinct packages. Package 1 2 3 4 5 6 7 8 9 1045 is kept in " 1 2 3 4 5 6 7 8 9 1048"; whereas package 1 2 3 4 5 6 7 8 9 1046 is stored in " 1 2 3 4 5 6 7 8 9 1050". moduleJDK 9 introduces a hierarchical level called "module" on top of packages, which will not be covered in this article. The "import" StatementThere are two ways to reference a class in your source codes:
The compiler, when encounter a unresolved classname, will search the 1 2 3 4 5 6 7 8 9 1054 statements for the fully-qualified name. The 1 2 3 4 5 6 7 8 9 1054 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 1054 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 1054 statement(s) must be placed after the 1 2 3 4 5 6 7 8 9 1058 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 1054 a single class in an 1 2 3 4 5 6 7 8 9 1054 statement by providing its fully-qualified name, e.g., You can also 1 2 3 4 5 6 7 8 9 1054 all the classes in a package using the wildcard 1 2 3 4 5 6 7 8 9 1062. 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 1036 is implicitly imported to every Java program. Hence no explicit 1 2 3 4 5 6 7 8 9 1054 statements are needed for classes inside the 1 2 3 4 5 6 7 8 9 1036 package, such as 1 2 3 4 5 6 7 8 9 1066, 1 2 3 4 5 6 7 8 9 1067, // 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.class0, 1 2 3 4 5 6 7 8 9 1069 and 1 2 3 4 5 6 7 8 9 1070. There is also no need for 1 2 3 4 5 6 7 8 9 1054 statements for classes within the same package. Take note that the 1 2 3 4 5 6 7 8 9 1054 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.java5 variables and methods of a class can also be "imported" via the " 1 2 3 4 5 6 7 8 9 1074" 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.java5 variable/method. For example: 1 2 3 4 5 6 7 8 9 10 The 1 2 3 4 5 6 7 8 9 1074 statement takes the following syntax: Take note that 1 2 3 4 5 6 7 8 9 1054 and 1 2 3 4 5 6 7 8 9 1074 statements does not apply to classes/members in the default package. Creating PackagesTo put a class as part of a package, include a 1 2 3 4 5 6 7 8 9 1058 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 PackageTo compile classes in package using JDK, you need to use " 1 2 3 4 5 6 7 8 9 1080" 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 1080" 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 1082 in the package name corresponds to sub-directory structure. The compiled bytecode for 1 2 3 4 5 6 7 8 9 1083 will be placed at " 1 2 3 4 5 6 7 8 9 1084" Running Classes in PackageTo 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 1085"), 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 PackageSo far, all our examples do not use a 1 2 3 4 5 6 7 8 9 1058 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 1087" 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 ExampleTo place the earlier class 1 2 3 4 5 6 7 8 9 1083 (and possible more related classes and resources) in a JAR file called 1 2 3 4 5 6 7 8 9 1089: // 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 FilesJava 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 1085, the class 1 2 3 4 5 6 7 8 9 1083 can be found in 1 2 3 4 5 6 7 8 9 1084. 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 1093 (or 1 2 3 4 5 6 7 8 9 1094); or the environment variable 1 2 3 4 5 6 7 8 9 1095. A classpath may contain many entries (separated by 1 2 3 4 5 6 7 8 9 1096 in Windows or 1 2 3 4 5 6 7 8 9 1097 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 JARIn this example, we shall kept the source files and class files in separate directories - " 1 2 3 4 5 6 7 8 9 1098" and " 1 2 3 4 5 6 7 8 9 1099" - for ease of distribution minus the source. com.zzz.geometry.CircleLet's create a class called 1 2 3 4 5 6 7 8 9 1017 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 1017 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 1080 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.CylinderNext, 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 1017. No 1 2 3 4 5 6 7 8 9 1054 statement for 1 2 3 4 5 6 7 8 9 1017 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 1017 class via option 1 2 3 4 5 6 7 8 9 1093 (or 1 2 3 4 5 6 7 8 9 1094), 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 1017 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.CatCreate 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 1080 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.testWe 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 1080 option to specify the destination and 1 2 3 4 5 6 7 8 9 1093 to specify the package base directories of 1 2 3 4 5 6 7 8 9 1017 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 100 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 1017 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 101 Jarring-up com.zzz.geometry packageNow, 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 102 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 ClassesFor ease of distribution (without source files), the source files and class files are typically kept in separate directories.
Two Classes of the Same Classname?Suppose that we have two 1 2 3 4 5 6 7 8 9 1017 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 ClassesReference: 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:
The user classes are searched in this order:
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 1093 option and without 1 2 3 4 5 6 7 8 9 1093 (which uses 1 2 3 4 5 6 7 8 9 1095 environment variable) to display the program classpath: 1 2 3 4 5 6 7 8 9 103 javac|java's command-line option -classpath or -cpI have demonstrated the command-line option 1 2 3 4 5 6 7 8 9 1094 (or 1 2 3 4 5 6 7 8 9 1093) in the earlier example. The CLASSPATH Environment VariableAlternatively, you could also provide your classpath entries in the 1 2 3 4 5 6 7 8 9 1095 environment variable. Take note that if 1 2 3 4 5 6 7 8 9 1095 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 1095 environment variable, you must include the current directory in the 1 2 3 4 5 6 7 8 9 1095, or else it will not be searched. Read "Environment Variables (PATH, CLASSPATH, JAVA_HOME)" for more details about 1 2 3 4 5 6 7 8 9 1095 environment variable. It is recommended that you use the 1 2 3 4 5 6 7 8 9 1093 ( 1 2 3 4 5 6 7 8 9 1094) command-line option (customized for each of your applications), instead of setting a permanent 1 2 3 4 5 6 7 8 9 1095 environment for all the Java applications. IDE (such as Eclipse/NetBeans) manages 1 2 3 4 5 6 7 8 9 1093 ( 1 2 3 4 5 6 7 8 9 1094) for each of the applications and does not rely on the 1 2 3 4 5 6 7 8 9 1095 environment. More Access Control Modifiers – protected and default package-privateJava has four access control modifiers for class/variable/method. Besides the 1 2 3 4 5 6 7 8 9 1030 (available to all outside classes) and 1 2 3 4 5 6 7 8 9 1031 (available to this class only), they are two modifiers with visibility in between 1 2 3 4 5 6 7 8 9 1030 and 1 2 3 4 5 6 7 8 9 1031:
Java Source FileA 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 1030 class, but may contain many non- 1 2 3 4 5 6 7 8 9 1030 classes (not recommended). The file name shall be the same as the top-level 1 2 3 4 5 6 7 8 9 1030 classname. The source file shall contain statements in this order:
Each // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java04, // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java05 or // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java06 is compiled into its own " // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java10" file. The top-level class must be either 1 2 3 4 5 6 7 8 9 1030 or default. It cannot be 1 2 3 4 5 6 7 8 9 1031 (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.java14 not allowed here". Dissecting the Hello-worldLet us re-visit the "Hello-world" program, which is reproduced below: 1 2 3 4 5 6 7 8 9 104 1 2 3 4 5 6 7 8 9 105
System.out.println()If you check the JDK API specification, you will find that:
The figure illustrate the classes involved in // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java67. Take note that each of the dot ( // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java00) opens a 3-compartment box!!! ExampleAs an example, the reference " // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java69" can be interpreted as follows:
Nested and Inner ClassesRead "Nested and Inner Classes". More on Variables and ReferencesTypes of VariablesThe 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:
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.HelloPackage02. 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.HelloPackage02 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 VariablesThe 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 1031 or 1 2 3 4 5 6 7 8 9 1030). The only modifier applicable is 1 2 3 4 5 6 7 8 9 1003. 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.java5 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.java5 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.java5 variables are created (during class loading) before instance variables (during instantiation). Variable InitializationAll class member and // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 variables that are not explicitly assigned a value upon declaration are assigned a default initial value:
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 InitializerArray's elements are also initialized once the array is allocated (via the 1 2 3 4 5 6 7 8 9 1001 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.HelloPackage24; while reference type are initialized to // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage02. (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 107 Stack/Heap and Garbage CollectorWhere Primitives and Objects Live?Primitive types, such as // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java93 and // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java96, 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 1001 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 ReferencesWhen a Java object is constructed via the 1 2 3 4 5 6 7 8 9 1001 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 1001 operator and the constructor. The 1 2 3 4 5 6 7 8 9 1001 operator:
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.HelloPackage36 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.HelloPackage02 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.HelloPackage38), 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 1001 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.HelloPackage40 or // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage41. 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.HelloPackage42 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 JavaJava'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.HelloPackage43 or // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage36 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:
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.,
This solution shall remain imperfect until the Java designers decided to allow programmers to manage the storage, which is not likely. More on MethodsPassing Arguments into Methods - By Value vs. By ReferenceRecall 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.java47 to the caller. To differentiate the parameters inside and outside the method, we have:
For example: In the above method definition, // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage47 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.HelloPackage48 with value of // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage49, i.e., // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage50, // Set the current working directory to the package base directory e:\myproject> java com.zzz.test.HelloPackage51 is the actual parameter. Passing Primitive-type Argument into Method - Pass-by-ValueIf the argument is a primitive type (e.g., // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java93 or // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java96), 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 108 1 2 3 4 5 6 7 8 9 109 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-ValueIf 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 108 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 MethodSince 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..."); } }3 Reference-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. OverridingAn 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:
A overloading method:
Frequently-Used Packages in JDK APIJDK API is huge and consists of many packages (refer to JDK API specification). These are the frequently-used packages:
Package java.lang Frequently-used Classes" 1 2 3 4 5 6 7 8 9 1036" 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 1054ed" into every Java program. That is, no explicit " 1 2 3 4 5 6 7 8 9 1054" statement is required for using classes in 1 2 3 4 5 6 7 8 9 1036. Frequently-used classes in " 1 2 3 4 5 6 7 8 9 1036" are:
java.lang.String, StringBuilder & StringBufferRead "Java String is Special". Wrapper Classes for Primitive TypesThe 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,
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.class24 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java91, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class26 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java92, 1 2 3 4 5 6 7 8 9 1069 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java93, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class27 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java94, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class28 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java95, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class29 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java96, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class30 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java97, and // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class31 for // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java98, as shown in the class diagram. Wrapper Classes are ImmutableEach 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 ConstructorsEach 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.class30, also have a constructor that takes a 1 2 3 4 5 6 7 8 9 1067, and parse the 1 2 3 4 5 6 7 8 9 1067 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.java5 factory method // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class71 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.class71 are defined in the 1 2 3 4 5 6 7 8 9 1069 class: For example, Unwrap via xxxValue() methodsThe 1 2 3 4 5 6 7 8 9 1011 superclass // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class75 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.class76 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.class24, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class26, 1 2 3 4 5 6 7 8 9 1069, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class27, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class28, // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class29. In other words, you can get an // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java93 or // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java96 value from an 1 2 3 4 5 6 7 8 9 1069 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.class30 and // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class31 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.class88 and // To create a JAR file from c1 ... cn classes (c:create, v:verbose, f:filename): > jar cvf myjarfile.jar c1.class ... cn.class89, respectively. ExampleConstants - MIN_VALUE, MAX_VALUE and SIZEAll 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.class31) contain the following constants, which give the minimum, maximum, and bit-length. For examples: Static Methods for Parsing StringsEach 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.class30) also contain a // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 method to parse a given 1 2 3 4 5 6 7 8 9 1067 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 & ConstantsThe // 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.class1 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.class2) 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.class4, // 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.class5). 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.class0 class is 1 2 3 4 5 6 7 8 9 1003 - 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.class0 class is 1 2 3 4 5 6 7 8 9 1031 - 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 1070 - directly or indirectly. A reference of class 1 2 3 4 5 6 7 8 9 1070 can hold any Java object, because all Java classes are subclasses of 1 2 3 4 5 6 7 8 9 1070. In other word, every Java class "is a" 1 2 3 4 5 6 7 8 9 1070. 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,
The 1 2 3 4 5 6 7 8 9 1070 class has the following 1 2 3 4 5 6 7 8 9 1030 methods:
java.lang.SystemThe 1 2 3 4 5 6 7 8 9 1066 class contains three // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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 1066 class also contains many useful // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 methods, such as:
java.lang.RuntimeEvery 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 Classesjava.util.RandomAlthough // 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.class9 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. ExampleExample: Simulating throw of 3 dice. java.util.Scanner & java.util.Formatter (JDK 1.5)Read "Formatted-text I/O". java.util.ArraysThe // 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.java5 methods for manipulating arrays, such as comparison, sorting and searching. For examples,
Example: See "Collection Framework". java.util.CollectionSee "Collection Framework". Package java.text Frequently-Used ClassesThe // 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.NumberFormatThe // 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 1package 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..."); } }8 Example 2In this example, we use // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java5 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.class75: // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java0 java.text.DecimalFormatThe // 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.HelloPackage13 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.DateFormatThe // 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.java5 factory methods: // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java1 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,
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.HelloPackage72 object. // Set the current working directory to the directory containing HelloPackage.java > javac -d e:\myproject HelloPackage.java2 java.text.SimpleDateFormatThe // 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.class13 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.java3 For example, [TODO] Writing JavadocA 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.class15 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.class16. 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 1030 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.class18 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. When you ____ a Java method you write multiple methods with a shared name?Terms in this set (45) A block can exist entirely within another block or entirely outside and separate from another block, and sometimes blocks can overlap. When you overload a Java method, you write multiple methods with a shared name.
When you overload a method you write multiple methods with the same _?Overloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods.
Is a term to indicate when multiple methods are written with the same name but with different parameter lists?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.
When you ____ methods you risk creating an ambiguous situation one in which the compiler Cannot determine which method to use?When you overload methods, you risk creating an ambiguous situation -- one in which the compiler cannot determine which method to use. When you write your own constructors, they can receive parameters. Such parameters are often used to initialize data fields for an object.
|