Java Basic Class Library
Java class library is a collection of implemented standard classes provided by the Java language. It is an API for Java programming (Application Program Interface), which can help developers develop Java programs conveniently and quickly. These classes can be divided into different sets according to the implementation functions, and each set forms a package, which is called a class library. Most of the Java class libraries are provided by Sun, and these class libraries are called basic class libraries.
The Java language provides a large number of class libraries for program developers to use. Understanding the structure of the class library can help developers save a lot of programming time and make the program written simpler and more practical. The rich class library resources in Java are also a major feature of the Java language and the basis of Java programming.
A brief introduction to common Java packages is as follows:
java.lang package: mainly contains language-related classes. The java.lang package is automatically loaded by the interpreter and does not require a description to be displayed.
java.io package: mainly contains classes related to input/output. These classes provide support for reading and writing data to different input and output devices, including keyboards, monitors, printers, disk files, etc.
java.util package: includes many classes with specific functions, including date, vector, hash table, stack, etc. The Date class supports time-related operations.
java.swing package and java.awt package: Provides classes that create graphical user interface elements. Through these elements, the programmer can control the appearance interface of the written Applet or Application. The package contains categories such as windows, dialog boxes, menus, etc.
java.net package: contains classes related to network operations, such as TCP Scokets, URLs and other tools.
java.applet package: A class that controls HTML document format, sound in the application, and other resources. The Applet class is an essential class used to create Applets contained in HTML.
java.beans package: defines the application programming interface (API). Java Beans is a neutral platform component structure of the Java application environment.
Introduction to Java Language Pack (java.lang)
The Java language package (java.lang) defines most basic classes in Java, which are automatically called by the Java language and do not need to display declarations. This package contains the Object class, which is the root node of the entire class hierarchy, and also defines classes of basic data types, such as: String, Boolean, Byter, Short, etc. These classes support conversion of numeric types and string operations, etc., which will be briefly introduced below.
Math class
The Math class provides commonly used mathematical operation methods and two mathematical constants Math.PI and Math.E. This class is final and cannot be inherited. All methods and properties in the class are static and objects of the Math class are not allowed to be created outside the class. Therefore, you can only use the methods of the Math class without any changes to it. The following table lists the main methods of the Math class.
[Example] Generate 10 random integers between 10 and 100.
//************ ep8_2.java ****** class ep8_2{ public static void main(String args[]){ int a; System.out.print("Random Number is: "); for(int i=1;i<=10;i++){ a=(int)((100-10+1)*Math.random()+10); System.out.print( " "+a); } System.out.println(); }}Running result: Random number is: 12 26 21 68 56 98 22 69 68 31
Since the random number is generated, the results of each run of Example 8-2 will not be the same. To generate random numbers between [a,b], the general formula is:
(b-a+1) *Math.random()+a
String class
A string is a sequence of characters. In Java, strings are implemented using objects of the class, whether they are constants or variables. java.lang provides two string classes: String class and StringBuffer class.
1. According to the provisions of the Java language, the String class is an immutable Unicode character sequence, and its function is to implement a static string that cannot be changed. For example, the result of concatenating two strings is to generate a new string without changing the original string. In fact, all the results of changing the string are to generate a new string, rather than changing the original string.
The implementation of strings is very similar to arrays. They also use index numbers to indicate the position of characters in the string. The number starts at 0, the number of the second character is 1, and so on. If the number to be accessed is not within the legal scope, the system will generate a StringIndexOutOfBoundsExecption exception. If the value of index is not an integer, a compilation error will occur.
The String class provides several string creation methods as shown in the following table.
2.StringBuffer class
The String class cannot change the contents in the string object, and can only implement the change of the string by creating a new string. If the string needs to be changed dynamically, you need to use the StringBuffer class. The StringBuffer class is mainly used to add, modify and delete string content, that is, the memory space of the object entity of this class can be automatically changed to facilitate the storage of a variable character sequence.
Three constructor methods provided by the StringBuffer class
Several commonly used methods of StringBuffer class