Classpath
The path and writing method of the java compiler when compiling .java files and java virtual machines execute .class files is different.
Without setting any classpath environment variables, javac can compile full-path .java files. For example:
javac d:/myjava/HelloWorld.java
After compilation, generate class files in the same path directory.
The default java virtual machine needs to search for class files from the path of the classpath environment variable to execute. For java virtual machine, this is not a class file, but a class. It has only classpaths, but no file system paths. The classpath environment variable is the environment that provides a search classpath for Java virtual machines. Note that the virtual machine does not recursively search for paths defined by classpath.
That is to say, the above java file can be compiled correctly, but cannot be executed. But if the classpath is set to ".;d:/myjava/", the java virtual machine will first search from the current path, and then search for the class file from d:/myjava.
So after the above HelloWorld.java is compiled, it can be executed directly:
java HelloWorld
Or switch to the d:/myjava directory and execute java HelloWorld.
But the following is the wrong way. In the end, although NewDir.java can be compiled correctly, during execution, it will search for whether NewDir.class is available in the current directory (d:/myjava), and then search for whether NewDir.class is available in the d:/myjava, but it will not recurse to the subdirectory newdir to search for class files.
d:/cd myjavajavac newdir/NewDir.javajava NewDir
For example, under d:/myjava/hello, there are two java source files, and their contents are as follows:
d:/myjava/hello Cat.java Dog.javaCat.java============================================================================================================ public class Dog { public static void main(String [] args) { Cat c = new Cat(); }}The Dog class directly new objects of the Cat class in another file. Whether it is compiled or run, this can be successful, because the javac compiler will automatically search for Cat.class from the path specified by the classpath when compiling Dog.java. This is just the way to search, and this class is a public class, so the compilation is successful.
In short, it should be clear that the javac compiler searches for file paths, which has nothing to do with the environment variable classpath. The Java virtual machine searches for class files, strictly speaking, classes, and the search path is determined by the environment variable classpath and has a sequence.
For more classpath descriptions, see "Package" below.
Package
Packages are collections of classes. Write the package keyword on the first line of the java source file (excluding comment lines or blank lines) and give the package name, you can put the class file into the package.
For example, the d:/myjava/Cat.java file:
package com.longshuai.home;public class Cat { public static void main(String[] args) { System.out.println("com.longshuai.home.Cat"); }}This means putting the Cat class in the com.longshuai.home package. Packages should be named after the reversed domain name to prevent package duplicate names from conflicting, of course, this is not necessary.
For source files that do not use package directives, classes within them will be regarded as "nude classes" by default during compilation.
The Java management package method is managed at the directory level corresponding to the package name. For example, the com.longshuai.home package above should be placed under com/longshuai/home (if it is Windows, backslash), that is, com/longshuai/home/Cat.class.
javac searches for files from paths at compile time. For example, put this Cat.java under com/longshuai/home. When executing, the java virtual machine searches for the class file to be loaded from the classpath, and the way to load the class is to use "." to connect various types of names. Therefore, the methods for compiling this file and executing this file by java virtual machine are:
javac com/longshuai/home/Cat.javajava com.longshuai.home.Cat
Note that there is no relationship between nested packages, such as java.util package and java.util.jar packages do not have any dependencies.
Use the classes in the package and import package (import)
In a Java source file, classes in other files cannot be used directly, unless the class you want to use can be searched by the classpath path. To reference other classes that are not classpath, you can only add them to classpath or load them into packages and then refer to the classes in the package.
References to the class in the package can be referenced by specifying the package name. For example:
com.longshuai.home.Cat c = new com.longshuai.home.Cat();
But obviously this is inconvenient. You can use the import directive to import the classes in the package you need to use in the first few lines of the java source file (but after the package command). For example, import the Cat class, so that you can use the class directly:
import com.longshuai.home.Cat;Cat c = new Cat();
When importing packages, you can use the asterisk "*" at the end to wildly match all imported classes, and only use "*" at the end, because "*" matches the class name, not the package name. Therefore, the "*" symbol cannot be used at non-ends to indicate classes in other packages, such as:
import com.longshuai.home.*; //Import all classes in the com.longshuai.home package import com.longshuai.*; //Import all classes in the com.longshuai package, but the classes in com.longshuai.home will not be imported, //Because although there are nesting between levels, these packages have no relationship import com.*.*; //This is the wrong way to write it
If there is a class with the same name in the imported package, a conflicting error will occur when referring to the class with the same name. For example, there are Date classes in the java.util and java.sql packages.
import java.util.*;import java.sql.*;public class Test { public static void main(String [] args) { Date today = new Date(); }}Compilation:
javac Test.javaTest.java:11: Error: The reference to Date is unclear Date today = new Date(); ^ The class java.sql.Date in java.sql and the class java.util.Date in java.util both match Test.java:11: Error: The reference to Date is unclear Date today = new Date(); ^ The class java.sql.Date in java.util and the class java.util both match 2 errors
At this time, you can explicitly import the Date class, or specify the package name when using the Date class. That is to say, the following two methods are correct:
//Method 1: import java.util.*;import java.sql.*;import java.util.Date;//Method 2: import java.util.*;import java.sql.*;public class Test { public static void main(String [] args) { java.util.Date today = new java.util.Date(); }}In addition to importing classes in packages, you can also static methods and static variables in classes in packages. Just add the static keyword and specify what to import. For example:
import static java.lang.System.*;import static java.lang.System.out;
After static importing methods, the prefix can be omitted, for example:
import static java.lang.System.out;public class ClassName { public static void main() { out.println("HelloWorld");//Equivalent to System.out.println("HelloWorld"); }}Archive package into jar package
The java virtual machine can directly recognize the jar package. You can archive the path corresponding to the package name into a jar package using the jar command. The instructions for using the jar command are as follows:
jar usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...options: -c Create a new file -t List the archive directory -x Extract the specified (or all) files from the archive -u Update existing archives -v Generate detailed output in standard output -f Specify the archive file name -m Contain the manifest information in the specified manifest file -n After creating a new file, perform Pack200 normalization -e Specify the application entry point for a standalone application bundled to an executable jar file -0 Store only; do not use any ZIP compression -P retain the leading '/' (absolute path) and ".." (parent directory) component -M does not create the manifest file for entries -i is the specified jar File Generation Index Information -C Change to the specified directory and contains the following files If any file is a directory, it is processed recursively. The specified order of manifest file name, archive file name, and entry point name is the same as that of the 'm', 'f' and 'e' tags.For example, package a.class and b.class in the current directory into test.jar:
jar cvf test.jar a.class b.class
Check the list of files in the jar package and will be displayed recursively:
jar -tf test.jarMETA-INF/META-INF/MANIFEST.MFjiecheng.class
For example, archive the com directory into d:/dp.jar.
jar cvf d:/dp.jar com/Added manifest is being added: com/(input=0) (output=0) (storage 0%)Added: com/longshuai/(input=0) (output=0) (storage 0%)Added: com/longshuai/home/(input=0) (output=0) (storage 0%)Added: com/longshuai/home/Bird.class (input=420) (output=291) (compressed 30%)Added: com/longshuai/home/Bird.java(input=136) (output=100) (compressed 26%)Added: com/longshuai/home/Cat.class (input=417) (output= 289)(Compressed by 30%)Adding: com/longshuai/home/Cat.java(Input = 134) (Output = 99)(Compressed by 26%)
With the jar file, you can directly set the path of classpath to the jar file name, so that when searching for class files, you will directly search from the jar file. For example, the classpath is set to:
.;d:/myjava;d:/dp.jar
Class search mechanism
When searching for class files in java virtual machines, in addition to the path specified by the classpath environment variable, two default paths will be searched first: jre/lib and jre/lib/ext, classes that seem to be searched in the jar file under jre/lib/ext.
For example, when classpath is set to ".;d:/myjava;d:/myjar.jar", you want to search for the com.longshuai.com.Cat class file:
(a). First search for jar files under jre/lib and jre/lib/ext;
(b).Search for whether there is com/longshuai/com/Cat.class in the current directory;
(c).Search d:/myjava/Cat.class again;
(d).Search for whether there is a com.longshuai.com.Cat class in the d:/myjar.jar file.
If a class is referenced in a java source file, at compile time, the following methods will be used to determine whether the class is reasonable and valid:
(1).Search whether the imported package class contains the class.
(2). Search for implicitly imported java.lang package, which is imported by default.
(3). Whether this class is defined in the current file.
(4). Search for whether the class is in it according to the search rules of the classpath ((a)-(d)).
inherit
The semantic logic of "what is what" can be reflected between classes, so that the inheritance of classes can be realized. For example, a cat is an animal, then a cat can inherit an animal class, while a cat class is called a subclass and an animal class is called a parent class.
After the subclass inherits the parent class, the subclass has all members of the parent class, including member variables and methods. In fact, in memory, when new subclass objects are used, a part of the area is divided into the heap to store attributes inherited from the parent class. For example, the area A obtained by new parent, the area B obtained by new child, and the area A is in the area B.
The reason why the child object contains a parent object is that when new child objects, the child object is first called to construct the child object, and when starting to construct the child object, the parent class constructor is first called to construct the parent object. In other words, before forming a child object, the parent object is always formed first, and then slowly supplement the attributes in the child object. For specific content, see "Rewrite super() of the construction method during inheritance".
A subclass not only has members of the parent class, but also has its own unique members, such as its own methods and its own member variables. It is easy to understand that the member names in subclasses and parent classes are different, but they may also be of the same name. If there are methods of the same name inherited from the parent class in the subclass , such as the parent class has the eat() method and the child class also has the eat() method, then this may be a rewrite of the method (see below). If the member variables in the subclass and the member variables of the parent class have the same name, they are independent of each other . For example, the parent class has a name attribute, and the subclass also defines a name attribute itself, which is allowed because they can be called using this and super respectively.
Use extends keyword when inheriting classes. When inheriting, java only allows inheritance from one parent class.
class Person { String name; int age; void eat() { System.out.println("eating...");} void sleep() {System.out.println("sleep...");}}class Student extends Person { int studentID; Student(int id,String name,int age) { this.name = name; this.age = age; this.studentID = id; } void study() {System.out.println("studing...");}}public class Inherit { public static void main(String[] args) { Student s1 = new Student(1,"Malongshuai",23); System.out.println(s1.studentID+","+s1.name+","+s1.age); s1.eat(); s1.sleep(); s1.study(); }}The above example explanation based on the Java classpath classpath and package is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.