Beginners learn to learn JAVA's key points

xiaoxiao2021-03-05  24

The first is the Path and ClassPath's question second is the third is the third is public, protected, private, static, when used, why is it used, how to use the fourth is the first one, Path and ClassPath What is Path is your JDK's work path, for example, your JDK installed in C: / JDK / Your Path (if there is no PATH) set to path; c: / jdk / bin What is ClassPath ClassPath is an important concept in Java, which describes the classes that the Java virtual machine is loaded in which paths to run during a Class is running and the class to run. Simply put, when a program does not find other class files he need, the system will automatically go to the path indicated by the ClassPath environment variable to find the relationship between the class and user-defined classes provided by third parties, ClassPath and Java packages. Java's package is closely related to the ClassPath relationship. The package is "." Segmentation, Sun recommends using the retrograde of the domain name to distinguish between different packages to avoid conflicts, such as com.company.util. In a package, you need to store the same directory as the package name when stored, such as sample.class in the com.company.util package, to be stored in the COM / COMPANY / UTIL directory. ClassPath has two expressions, one is a classpath, such as C: / Work / Classes, indicating that the C: / Work / Classes directory is a ClassPath entry; another way is ClassPath, such as C: /work/util.jar, indicating that the C: /Work/util.jar file is a ClassPath entry, and any compressed file containing the Java class can be used as an entry of ClassPath. So what is the relationship between ClassPath and the package? Simply put, Java virtual machines look for specific class files in such a way while loading classes: ClassPath packets stored directory specific class files. If there is a C: / Work / Classes entry in ClassPath, the class that needs to be loaded is com.company.util.sample.class, then when loading this class, the virtual opportunity looks up C: / Work / Classes / COM / Company / Util directory, if sample.class is in this directory, the virtual machine can be found, if this class is not in this directory, not in any other classpath, then the virtual opportunity throws a ClassNotFoundException. ClassPath's order and class version conflict Java virtual machine When you load classpath is order, if there are multiple entries in ClassPath, there are classs of the same name, then the class in the front position will be loaded, followed It will be ignored. This type of sub-loading in order may result in a class conflict. For example, classpath = c: /servlet2.2/servlet.jar; c: /servlet2.3/servlet.jar, then in the process of practical applications, you use servlet2.2, not servlet2.3. Many times if you don't pay attention to this, it may result in strange anomalies. The classpath set in the command line state setting command line can be set in two ways.

One is to set the environment variables directly, for example in a Windows environment, we use the set command: set classpath = c: / work / classes; c: /Work/util.jar Another way to perform Javaac, Java or other Java Specify ClassPath: java -classpath c: / work / classes; c: /work/util.jar com.company.util.sample 2 package and import problem Java package (package) in java is the directory, it It is to better manage the Java class (Class) and interfaces. The java language package can be used by another Java development package. If we want to reference a classes in a package, use the import keyword to indicate that Package Bow before the IMPORT row, the package will generate the path to ClassPath, which is specified, for example, ClassPath, C: / TestPackage Com.dir results To generate a COM subdirectory in the C: / Test directory, the directory COM contains all files all files will be placed in the DIR directory in Java's objects in the definition and application of the package, for the already edited class, if In the new class, you should include the already defined class, and the IMPORT statement is implemented in Java. Use the Import statement to join all classes (with above Package package as an example) Import com.dir. *; Join the import statement Classic (with the above package package as an example, if there is hello.class) import com.dir.hello; third, public, protected, private, static, when used, why is it used, how to use each class Created your own namespace, the method and variables can know each other's existence, can be used. Public classes can not only be used by other classes in the same package, can also use classes in other packages; variables and methods for modifying the word public, protected, private: public: any other class, the object can see this class If it can access the data of the variable, or how the method can be used. Class abc {public int pub_i = 5; public void show) () {system.out.println ("pub_i" pub_i);} Class demo {public static void main (string args []) {abc abc = new ABC ( ); System.out.println ("abc.pub_i" abc.pub_i); abc.pub_i = 10; abc.show ();}} protected variable and method: If a variable or method has a modified protected, In the same class, the same package can be used. The class of different packages should be used, and must be a subclass of such a class, and a variable or call can be accessed.

Public class abc {protected int pro_i = 5; protected void show () {system.out.println ("pro_i =" pro_i);}}} package: class def {public static void main (string args []) {Abc abc = new abc (); system.out.println ("abc.pro_i =" abc.pro_i); abc.pub_i = 10; abc.show ();}} Different packets but are subclasses: import mytest .PACK.ABC; Class DEF EXTENDS ABC {Public Static Void Main (String Agrs []) {Def DEF = New Def (); System.out.Println (Def.i); Def.i = 10; Def.SHOW );}} Private does not allow any other class access and call; as the variable name in the subclass is the same as the parent class, the original variable is covered. Overriding and overloading. The method defined in the children and grandchildren is the same name, the same parameter line, the method in the ancestral class is covered; the overloader of the method refers to the polymorphism of an object, that is, multiple methods with the same name But the parameter line is different. Final: final before the method, prevents the method from being covered; factory before the class, the standard is that the class cannot be inherited; Final defines a constant before the variable. Static: Before the variable or method, it indicates that they belong to the class; the static variable is shared between the examples, if it is a PUBLIC static variable, other classes may not access them by instantiation; the static method is called a class method, so it is not Method for calling (oriented) to access an object's data member, although it is not partial variable that is not a method; a class method can only access its local variable. Example: Incorrect reference class staticerror {string mystring = "hello"; public static void main (String args []) {system.out.println (mystring);}} error message: can't make a static reference to nonstatic Variable. Why isn't it correct? Only the object's method can access the variable of the object.

Solution: 1) Remove variables to change the variable Class StaticError {static string mystring = "hello"; public static void main (string args []) {system.out.println (mystring);}} 2) Creating a class first Example Class Nostaticerror {public static void main (string args []) {string mystring = "hello"; system.out.println (mystring);}}} The fourth, making JAR file JAR file is Java Archive File, as the name It is a document format that is closely related to Java as a Java. The JAR file is very similar to zip file - accurately, it is a zip file, so it is called a file package. The only difference between the JAR file and the zip file is in the content of the JAR file, contains a meta-inf / manifest.mf file, which is automatically created in the JAR file when generating the JAR file. Detail JAR is installed with JDK, In the bin directory in the JDK installation directory, Windows is named jar.exe under Windows, and the file named JAR under Linux is. It runs to use the Tools.jar file in the Lib directory in the JDK installation directory. However, in addition to installing JDK, we don't need to do anything, because Sun has helped us do well. We don't even need to put Tools.jar in the ClassPath. Use without any jar commands We can see the usage of JAR commands as follows: jar {ctxu} [vfm0m] [jar- file] [manifest- file] [-c directory] file name ... where {ctxu} is Jar The subcommand of the command, each JAR command can only contain one of the CTXU, which represents: -c Creating a new JAR file package -T listing the content list of the JAR file package -X expands the specified file of the JAR file package or all File -U update the option existing JAR file (add file to the JAR file) [VFM0M] can optionally, you can not choose, the option parameter of the JAR command is the selection of detailed report and print to standard Output -f Specifies the JAR file name, usually this parameter is a must-mode specifying the Manifest manifest file -0 stored, not compressed, which is not compressed, the JAR file package is larger than the volume of the parameter, but the speed is more Fast -m does not generate a list of all items (MANIFEST) files, this parameter ignores the -m parameter [JAR-file] needs to be generated, view, update, or unlocked JAR file package, it is the accessory parameter of the -f parameter [ Manifest- File] The Manifest list file, which is the accessory parameter of the -m parameter [-c Directory] indicates that the operation of the specified directory is executed. It is equivalent to using the CD command to turn this directory without carrying without -C parameter jar command, it can only be available when creating and updating the JAR file package. File name ... Specify a file / directory list, these files / directory are to add files / directories in the JAR file. If the directory is specified, the JAR command will automatically put all the files and subdirectories in the directory into the package.

Let's give some examples to illustrate the usage of JAR commands: 1) JAR CF TEST.JAR TEST This command does not execute the display, the execution result is a Test.jar file in the current directory. If the current directory already has Test.jar, then the file will be overwritten. 2) JAR CVF TEST.JAR TEST This command is the same as the result in the previous example, but due to the function of the V parameter, the package process is displayed, as follows: MANIFEST, the manifest) Add: TEST / (read = 0) (write = 0) (0%) Add: test / test.class (read = 7) (write = 6) (40%) 3) JAR CVFM TEST.JAR TEST This command is similar to 2) However, but there is no meta-inf / manifest file in the generated Test.jar. The information for the package process is also slightly different: increase: TEST / (read = 0) (write = 0) (store 0%) increase : Test / test.class (read = 7) (write = 6) (40%) 4) JAR CVFM TEST.JAR Manifest.mf Test Run Results and 2) Similar, display information is the same, just generate JAR The meta-inf / manifest content in the package is the content that contains manifest.mf. 5) JAR TF TEST.JAR In the case of Test.jar already exists, you can view the contents in Test.jar, such as 2) and 3) The resulting test.jar should be this command, and the results are as follows; for 2) meta-inf / meta-inf / manifest.mftest / test / test.class For 3) Test / Test / Test.class6) JAR TVF Test. In addition to the content displayed in JAR, it also includes details of the package, such as: 0 WED JUN 19 15:39:06 GMT 2002 Meta-INF / 86 WED JUN 19 15:39:06 GMT 2002 Meta- INF / MANIFEST.MF 0 WED JUN 19 15:33:04 GMT 2002 TEST / 7 WED JUN 19 15:33:04 GMT 2002 Test / Test.class7) JAR XF Test.jar Unope Test.jar to the current directory, no Display any information, for 2) generated Test.jar, the unlocked directory structure is as follows: == | - Meta-inf | `- manifest` - test `--test.class8) jar xvf test.jar Operation results are the same as 7), for extracting The process has detailed information display, such as: Meta-INF / Expansion: meta-inf / manifest.mf creation: test / expand: test / test.class9) jar uf test.jar manifest.mf Added in Test.jar Document Manifest.mf, this use JAR TF to see Test.jar can find that Test.jar is more than one manifest than the original.

转载请注明原文地址:https://www.9cbs.com/read-32533.html

New Post(0)