What is a package
The package in Java (Package) actually refers to the directory, which is to better manage Java classes (class) and Interfaces (Interface). The Java language package can be used by another Java development package. If we want to refer to a class in a package, use the Import keyword to mark it. Like what:
import java.util.
date=new Date
Hint: Import Java.util. Represents that all public classes and interfaces in Java.util are introduced into the current package. The matching characters here can be called into multiple class names.
Common Java Standard Packages
The JDK provides us with a number of standard Java classes and interfaces that are necessary to write Java programs, know the classes and interfaces that each package contains, and are familiar with these classes and interfaces as basic skills that every Java programmer should master.
Common packages in Java are: Java.applet, java.awt, JAVA.I, Java.lang, java.net, and Java.util, and so on. The java.applet contains classes and interfaces for designing small applications (applets); java.awt is a window Toolbox package (awtabstract window Toolkit) with some GUI-interface-related classes java.io package support input and output, such as file input stream class: FileInputStream; Java.lang contains threads, exceptions, systems, integers, and other related classes, is the default load in Java program a package; java.net this class supports TCP/IP network protocols , and contains the socket class and URL-related classes, which are to be used in network programming; Java.util contains common classes of programs, such as Datedictionary, and so on.
In addition to some of the standard packages mentioned above, there are many other packages, such as database programming, we may need to use java.sql package, write network program will also use Java.rmi package (Rmiremote method invocation) and so on. In addition, the Javax. package is a number of standard package extensions, commonly used packages are: Javax.swing, Javax.sound.
Create a package yourself
Packages in Java can be created by themselves. We can put many functions similar to the class and interface in the same package, for easy management and use.
Basic ways to create packages
1. Define public classes;
2. The first sentence plus "Package package name";
3. Place the Java file generated class in a directory with the package name Directory name
4. All public classes in this package can be accessed in other programs using the "Import package name".
Creation and use of packages
//filename B.java
package com.chen.test //定义一个包
public class B //定义一个类
public void addint iint j //两个数求和,并输出
System.out.printlni+j
The code above is to create a simple package: com.chen.test, we name this file B.java. Now run javac-d c\ B.java compile the package (the command generates the Com\chen\test directory under C Disk and saves the compilation results in B.class). Now that we have entered the C\com\chen\test directory, we can see that the compiled class file is also included.
OK, now that the package has been created, what you need to do is set the c\com\chen\test in your environment variable CLASSPATH.