1. What is a package?
Package is a mechanism in Java that is used to organize related classes and interfaces together. Packages can help manage namespaces, prevent naming conflicts, and provide control over access rights. By using packages, Java developers can better organize their code and manage large projects more easily.
2. The origin and evolution of java and javax packages
2.1 java package
java
The package contains core classes in the Java standard library. These classes are the basis of the Java platform and are a basic component of any Java program.java
Classes in packages have existed since Java 1.0 and continue to expand and improve with each new version release.
Example:java
Common categories in packages:
-
: Contains basic classes, such as
String
、Object
、Math
、System
wait. This package is imported automatically without explicit import. -
: Contains collection framework classes (such as
List
、Map
、Set
) and date and time operation categories (such asDate
、Calendar
)。 -
: Classes that provide input and output functions, such as
File
、InputStream
、OutputStream
。 -
: Classes used for network programming, such as
Socket
、URL
。
These packages and classes are the core parts of the Java platform and are designed as the basis for any Java runtime environment.
2.2 javax package
javax
The package was originally the abbreviation of Java Extension Package.javax
The classes and interfaces in were not originally part of the Java core, but were functions provided as extension libraries. Over time, some components in these extension libraries have become very important and widely used, so they are incorporated into the Java standard library, although they are retained.javax
prefix of .
Example:javax
Common classes and subpackages in packages:
-
: Provides classes for building graphical user interfaces (GUIs). Swing is an extension of AWT that provides richer GUI components.
-
: Contains classes and interfaces for handling HTTP requests and responses, and is the core of Java Web development.
-
: Provides classes and interfaces for processing XML documents.
-
: Classes and interfaces that provide email sending and receiving functions.
3. The main differences between java and javax
3.1 Attribution of history and standard library
java
Bag: Contains classes in the Java core standard library. These classes existed since the birth of the Java language and constituted the core functions of the Java platform. These classes are part of Java SE (Standard Edition) and can be found in any Java-compliant runtime environment.javax
Bag: Originally a Java extension package, it contains some classes and interfaces that are not core, but powerful and important. As these functions mature and popularize, manyjavax
Package classes are included in the Java SE standard library, but they are still preservedjavax
Prefix.
3.2 Naming and Evolution
Naming agreement:
java
The classes in the package generally represent core functions, andjavax
The classes in the package represent the initial extended function. Over time, somejavax
The bag has become likejava
The classes in the package are like core, but for backward compatibility and historical reasons, they retainjavax
prefix of .Standardization and compatibility: Although some
javax
Packages are now part of Java SE, but initially they were independent extension libraries. For example,is an extension to AWT to provide more advanced GUI functions.
It was originally designed for Java EE (Enterprise Edition), but is now also used for standardized web development.
3.3 Package Stability
java
Bag:becausejava
Packages are part of the core standard library, and their classes and interfaces are usually very stable. In the new version of Java,java
When modifying classes in packages, they are usually very cautious to ensure backward compatibility.javax
Bag:althoughjavax
Classes in packages are also very stable after standardization, but since they originate from expansion packages, they may experience more changes and expansions in history. However, once it becomes part of the standard,javax
Classes in packages will also be subject to strict version control and compatibility requirements.
4. The future of javax package
With the development of Java,javax
Some classes and interfaces in the package may be migrated to a new namespace. For example, Java EE was renamed to Jakarta EE,javax
Namespaces are gradually beingjakarta
Replaced by namespace. This means that future Java enterprise application development will be more usedjakarta.*
Instead ofjavax.*
。
5. When to use java and javax packages
java
Bag: When you need to use core language functions, basic data structures, input and output, concurrent programming, basic network functions, etc., usejava
Classes and interfaces in packages. For example, processing strings, operating files, performing basic mathematical operations, processing sets, etc.javax
Bag: When you need to use more advanced features or extended features, usejavax
Classes and interfaces in packages. For example, create a graphical user interface, perform web development, process XML or mail, etc.
6. Differences in practical applications
Consider the following two scenarios:
Scenario 1: Create a graphical user interfaceIf you are developing a Java application and need to create a graphical user interface, then you may useClasses in packages, such as
JFrame
、JButton
、JPanel
wait. These classes were initially extended to Java, but are now the standard part of Java SE.
import ; import ; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("My GUI"); JButton button = new JButton("Click Me"); (button); (300, 200); (JFrame.EXIT_ON_CLOSE); (true); } }
Scenario 2: Processing collections and data structuresIn another scenario, if you need to process collections or use basic data structures (such as lists, maps, collections, etc.), you will useClasses in packages, such as
ArrayList
、HashMap
、HashSet
wait. These classes are part of the Java core library and can be found in any Java program.
import ; import ; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); ("Java"); ("Python"); HashMap<String, Integer> map = new HashMap<>(); ("Java", 1); ("Python", 2); (list); (map); } }
7. Summary
java
andjavax
The difference between packages mainly comes from their historical background and functional positioning:
-
java
Bag: Part of the Java core library, which contains the basic classes in the Java standard library and is a core component of any Java running environment. -
javax
Bag: Originally, it was an extension package for Java, including some classes and interfaces that are not core but are very important and commonly used. Later, some were included in the Java standard library.
With the evolution of Java,javax
Many classes in the package have becomejava
Classes in packages are as important and widely used, but they retain themjavax
Prefix to maintain backward compatibility and historical consistency. In the future, as Java EE turns to Jakarta EE,javax
The use of namespaces will gradually decrease, and new expansions and developments will likely be carried out under new namespaces.
This is the end of this article about the difference between import java and javax. For more related content on the difference between import java and javax, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!