The Java ingestion package mechanism, which provides a multi-level namespace for classes that resolves issues such as naming conflicts, class file management, and so on.
Java allows a set of function-related classes to be placed under the same package to form a logical class library unit. If you want to put a class under the specified package structure, I should place the code in the following format in the first non-comment line of the Java source program:
Package PackageName;
Once the package statement is used in the Java source file, it means that all of the classes defined in the source file belong to it. The complete class name for each class in the package should be a combination of the package name and class names, and if others need to use the class under the package, you should also use a combination of the package name plus the class name.
Javac-d. What is the difference between Hello.java and Javac Hello.java?
Package Lee;
public class Hello
{
public static void Main (string[] args)
{
System.out.println ("Hello world!");
}
}
The-D option is used to set the save location for the generated class file, specifying that the generated class file will be placed under the current path. After compiling with this command, a folder named Lee is generated under the current path, and a Hello.class file is under the folder.
If you compile directly using the Javac Hello.java command, a Hello.class file will be generated under the current path without the Lee folder being generated.
Must the class in the same package be in the same folder?
Can not be in the same folder. such as Lee. The person and lee.persontest two classes can be one on the C drive one on the D drive, as long as the CLASSPATH environment variable contains both paths.
How does the package name start?
The package name is generally all lowercase letters, concatenating by one or more meaningful words. In order to avoid duplication of package and class names in development, Oracle recommends that the company's Internet domain name be inverted as the package name, such as the Internet domain name is crazyit.org, and all of its classes are placed under the ORG.CRAZYIT package and its child packages.
Domain name inverted. Project name. Module name. Component Name
Can the classes under the same package access each other without a packet prefix?
OK.
Can the parent and child packages be prefixed without a packet when they access each other?
No, you must add a packet prefix.
Import keyword
As seen above, if you need to use other classes in different packages, you always need to use the full name of that class, which is a tedious task. To simplify programming, Java introduces the Import keyword, which can be imported to a Java file only to a class or all of the classes under the specified package hierarchy, and the import statement should appear after the packages statement and before the class definition. A Java source file can contain only one package statement, but it may contain multiple import statements, and multiple import statements are used to import classes under Multiple pack hierarchies.
Importing a single class: Importpackage.subpackage ... ClassName;
Importing all classes: Importpackage.subpackage...*;
The above (*) can only represent classes and cannot represent packages.
Common packages for Java
Java's core classes are placed under the Java package and its sub-packages, and many classes of Java extensions are placed under the Javax package and its sub-packages. These utility classes, which are the aforementioned APIs (application interfaces), are placed under different packages by the capabilities of these classes. The next few packages are common in Java.
Java.lang: This package contains the core classes of the Java language, such as Sting, Math, System, and tread classes, and the classes under this package do not need to be imported using the import statement, and the system automatically imports all classes under this package.
Java.util: This package contains a large number of Java tool classes/interfaces and collection framework classes/interfaces, such as arrays and list, set, and so on.
java.net: This package contains some Java network programming related classes/interfaces.
java.io: This package contains some Java input/output related classes/interfaces.
Java.text: This package contains some Java formatting related classes.
java.sql: This package contains some Java-related classes/interfaces for JDBC database programming.
java.awt: This package contains the related classes/interfaces of the abstract window toolset (toolkits), which are primarily used to build graphical user interface (GUI) programs.
Java.swing: This package contains a related class/interface for swing graphical user interface programming that can be used to build platform-independent Cui programs.