Key points for beginners to learn java well

Source: Internet
Author: User
Tags zip extension

The first is the path and classpath problems.
The second is about package and import.

The third is public, protected, Private, static, when to use, why to use, and how to use

The fourth is to create a jar file.

 

First, the path and classpath Problems

What is path

Is the working path of your JDK

For example, if your JDK is installed in C:/JDK/, set your path (if there is no path) to path; C:/JDK/bin.

What is classpath?

Classpath is an important concept in Java. It describes the paths in which the Java Virtual Machine loads the classes to run and the classes to be used when running a class. To put it simply, when a program cannot find the other class files it needs, the system will automatically go to the path specified by the classpath environment variable to find the third-party classes and user-defined classes,

Relationship between classpath and Java package

Java package is closely related to classpath. Packages are separated by ".". Sun recommends that different packages be distinguished by Reverse arrangement of domain names to avoid conflicts, such as com. Company. util. Classes in a package must be stored in the same directory as the package name when they are stored, as shown in the preceding COM. company. sample In the util package. class, which must be stored in the COM/Company/util directory.
Classpath has two expressions: classpath pointing to a directory, for example, C:/work/classes, indicating that the C:/work/classes directory is a classpath entry; another method is to point to the classpath of the compressed file, such as C:/work/util. jar, indicating C:/work/util. A jar file is a classpath entry. Any ZIP file that contains a Java class can be used as a classpath entry.
So what is the relationship between classpath and package? To put it simply, when loading classes, the Java Virtual Machine looks for specific class files in this way: classpath + package storage directory + specific class files. For example, if the classpath contains a C:/work/classes entry, the class to be loaded is com. company. util. sample. class, then when loading this class, the virtual opportunity to find the C:/work/classes/COM/Company/util directory, if the sample. the class can be found by the virtual machine in this directory. If this class is not in this directory and is not in any other classpath, A classnotfoundexception is thrown.

The order of classpath conflicts with the class version.

When loading classes, the Java Virtual Machine looks for the classpath in sequence. If multiple entries in the classpath have classes with the same name, the classes in the earlier position will be loaded, will be ignored later. Loading classes in order may cause version conflicts. For example, classpath = C:/servlet2.2/servlet. jar; C:/servlet2.3/servlet. Jar. in actual application, you use servlet2.2 instead of servlet2.3. In many cases, if you do not pay attention to this, it may cause strange exceptions.

Classpath settings in the command line status

Classpath in the command line status can be set in two ways.
One is to directly set environment variables. For example, in windows, we use the set command:
Set classpath = C:/work/classes; C:/work/util. Jar
Another method is to directly specify classpath when executing javac, Java, or other Java commands:
Java-classpath C:/work/classes; C:/work/util. Jar com. Company. util. Sample

IIPackage and import problems

A package in Java actually refers to a directory, which is used to better manage Java classes and interfaces ). Java packages can be used by another Java Development Kit. If we want to reference a class in a package, we can use the import keyword to mark it. The package line should be generated in the path specified by classpath after package is packaged before the import line.
For example, classpath is C:/test.
Package com. dir
The result is that the Com Sub-directory is generated under the C:/test directory, and the com directory contains all files in the Dir directory.

Objects in Java are embodied in the definition and application of packages. For compiled classes, if you want to use them in the new class, you should include the classes that have been defined recently, use the Import Statement in Java.
Use the Import Statement to add all classes (for example, package above)
Import com. dir .*;

Add a special class using the Import Statement (for example, if the package contains hello. Class in the directory)

Import com. dir. Hello;

Third, public, protected, Private, static, when to use, why to use, how to use

Each class creates its own namespace, which means that methods and variables can know each other and can be used.
Public classes can be used not only by other classes in the same package, but also by classes in other packages;
Public, protected, and private modifiers for variables and methods:
Public: any other class or object that can see this class can access the data or usage of the variable.
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 variables and methods:
If a variable or method in a class has the modifier protected, the same class and the same package can be used. Classes of different packages must be sub-classes of these classes and can be accessed or called.
Public Class ABC {
Protected int pro_ I = 5;
Protected void show (){
System. Out. println ("pro_ I =" + pro_ I );}
}
Classes of the same 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 packages but sub-classes:
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 cannot be accessed or called by any other class;
When the variable name in the subclass is the same as that in the parent class, the original variable is overwritten.
Overriding and overloading ). If a method defined in a descendant class has the same name and same parameter row as a method in the ancestor class, the method in the ancestor class is overwritten. Method overloading refers to the polymorphism of an object, that is, multiple methods use the same name, but the parameter rows are different.
Final:
Final prevents the method from being overwritten before the method;
Before the final class, the mark is that the class cannot be inherited;
Final defines a constant before the variable.
Static:
Before variables or methods, it indicates they belong to the class;
Static variables are shared among instances. If they are public static variables, other classes can access them without instantiation;
Static methods are called class methods, so they can be called without instantiation (process-oriented)
Methods of an object can access data members of an object, although they are not local variables of the method. Methods of a class can only access their own local variables.
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 not? Only object methods can access object variables.
Solution:
1) rename a variable
Class staticerror {
Static string mystring = "hello ";
Public static void main (string ARGs []) {
System. Out. println (mystring );}
}
2) create a class instance first
Class nostaticerror {
Public static void main (string ARGs []) {
String mystring = "hello ";
System. Out. println (mystring );}
}
}

Fourth, create a JAR File

Jar files are Java archive files, which are closely related to Java and are a document format of Java. Jar files are very similar to zip files-specifically, they are zip files, so they are called file packages. The only difference between a jar file and a zip file is that the content of the JAR file contains a META-INF/manifest. MF file, which is automatically created when the JAR file is generated.

Jar command details

Jar is installed with JDK. in the bin directory under the JDK installation directory, the file name in Windows is jar.exe and in Linux is jar. To run the SDK, you must use the tools. jar file in the lib directory under the JDK installation directory. However, we don't need to do anything except install JDK, because Sun has already helped us. We don't even need to put tools. jar in classpath.

We can see the usage of the jar command without any jar command is as follows:

Jar {ctxu} [vfm0m] [Jar-file] [manifest-file] [-C Directory] File Name...

{Ctxu} is a sub-command of the jar command. Each jar command can contain only one of the ctxu commands, which indicate:

-C: Create a New jar package.

-T list the contents of the jar package

-X expand the specified file or all files in the jar package.

-U: update an existing jar package (add the file to the jar package)

Options in [vfm0m] can be selected or not. They are the option parameters of the jar command.

-V: generate a detailed report and print it to the standard output.

-F specifies the JAR file name. This parameter is usually required.

-M: Specifies the manifest file to be included.

-0: It is only stored and not compressed. the JAR file package generated in this way is larger than the JAR file package generated without this parameter, but faster.

-M does not generate the manifest file for all items. This parameter ignores the-M parameter.

A [Jar-file] is a jar package that needs to be generated, viewed, updated, or unwrapped. It is a subsidiary parameter of the-F parameter.

[Manifest-file] is the manifest configuration file, which is a subsidiary parameter of the-M parameter.

The [-C Directory] indicates the operation to run the jar command in the specified directory. It is equivalent to converting to this directory using the CD command and then executing the jar command without the-C parameter. It can only be used when creating and updating the JAR file package.

File Name... specifies a file/directory list. These files/directories are the files/directories to be added to the jar package. If a directory is specified, all files and subdirectories in the directory are automatically packaged into the package.

Here are some examples to illustrate how to use the jar command:

1) jar CF test. jar test

The command does not display the execution process. The execution result is that the test. jar file is generated in the current directory. If the current directory already has test. jar, the file will be overwritten.

2) jar CVF test. jar test

The command is the same as the result in the preceding example. However, the packaging process is shown as follows due to the function of the V parameter:

Manifest)

Added: Test/(read = 0) (write = 0) (0% is stored)

Added: Test/test. Class (read = 7) (write = 6) (compressed by 14%)

3) jar cvfm test. jar test

The command is similar to 2), but the generated test. jar does not contain the META-INF/manifest file, and the packaging process information is slightly different:

Added: Test/(read = 0) (write = 0) (0% is stored)

Added: Test/test. Class (read = 7) (write = 6) (compressed by 14%)

4) jar cvfm test. Jar manifest. MF Test

The running result is similar to 2), and the display information is the same, but the META-INF/manifest content in the jar package is different, it contains the contents of manifest. MF

5) jar TF test. Jar

If test. Jar already exists, you can view the content in test. Jar. For example, for test. Jar generated in 2) and 3), the command is as follows;

For 2)

META-INF/

META-INF/manifest. MF

Test/

Test/test. Class

For 3)

Test/

Test/test. Class

6) jar tvf test. Jar

In addition to the content shown in (5), it also includes the details of the files in the package, such:

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. Class

7) jar XF test. Jar

Unpack test. jar to the current directory without any information. For 2) the generated test. Jar directory structure is as follows:

=

| -- META-INF

| '-- Manifest

'-- Test

'-- Test. Class

8) jar xvf test. Jar

The running result is the same as 7). Detailed information about the decompression process is displayed, for example:

Create: META-INF/

Expand: META-INF/manifest. MF

Create: Test/

Expand: Test/test. Class

9) jar UF test. Jar manifest. MF

The file manifest. mf is added to test. Jar. Use jar TF to view test. Jar. You can find that test. Jar has a manifest more than the original one. By the way, if you use the-M parameter and specify manifest. mf file, then manifest. mf is used as the manifest of the inventory file, and its content will be added to the manifest; however, if it is added to the jar package as a general file, it is no different from the general file.

10) jar UVF test. Jar manifest. MF

(9) The results are the same and detailed information is displayed, for example:

Added: manifest. MF (read = 17) (write = 19) (Compressed-11%)

Some tips on jar packages

1) Use unzip to decompress the JAR File

As mentioned before, the JAR file is actually a zip file, so you can use common tools for extracting ZIP files to decompress the JAR file, for example, Windows WinZip, WinRAR, and Linux unzip. Use WinZip and WinRAR to decompress the files because they are intuitive and convenient. The unzip is used because the-D parameter can be used to specify the target directory during decompression.

When extracting a jar file, you cannot use the-C parameter of jar to specify the extraction target, because the-C parameter is only available when the package is created or updated. To decompress the file to a specified directory, You need to copy the JAR file to the target directory before decompression, Which is troublesome. If you use unzip, you don't need to worry about it. You just need to specify a-d parameter. For example:

Unzip test. jar-d dest/

2) use tools such as WinZip or WinRAR to create jar files

The jar file mentioned above is the ZIP file that contains the META-INF/manifest, so you only need to use WinZip, WinRAR and other tools to create the zip compressed package, add a META-INF directory containing the manifest file to the zip package. If you use the-M parameter of the jar command to specify the configuration file, you only need to modify the manifest as needed.

3) use the jar command to create a zip file

In some Linux systems, the unzip command is provided, but there is no zip command. You can decompress the ZIP file to create a zip file. To create a zip file, use the jar command with the-M parameter, because the-M parameter indicates that the manifest list is not added when creating the jar package, in this case, you only need. change the jar extension. the zip extension is used to create a zip file without compromise. For example, change the example 3rd in the previous section:

Jar cvfm test.zip Test

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.