java base class library

The java class library is a collection of standard classes that have been implemented in the java language.It is java programming api (application program interface), which can help developers to develop java programs conveniently and quickly. These classes differ depending on the function they implement,Can be divided into different sets,Each collection forms a package,Called a class library.Most of the java class libraries are provided by the sun company,These class libraries are called base class libraries.

A lot of class libraries are provided in the java language for use by program developers.Understanding the structure of the class library can help developers save a lot of programming time,And can make the program written simpler and more practical.The rich class library resources in java are also a major feature of the java language.Is the basis of java programming.

A brief introduction of java common packages is as follows:

java.lang package:mainly contains language-related classes.The java.lang package is automatically loaded by the interpreter,No description is required.

java.io package:mainly contains input/output related classes,These classes provide support for reading and writing data to different input and output devices.These input and output devices include keyboards, displays, printers, disk files, and more.

java.util package:includes many classes with specific functions,There are dates, vectors, hash tables, stacks, etc. The date class supports time-related operations.

java.swing package and java.awt package:Provides classes for creating graphical user interface elements.Through these elements,Programmers can control the appearance of the applets or applications they write.The package contains classes such as windows, dialog boxes, and menus.

java.net package:contains classes related to network operations,Such as tcp scokets, url and other tools.

java.applet package:A class containing resources that control the format of html documents, sounds in applications, etc.The applet class is an essential class for creating applets to be included in html.

java.beans package:defines the application programming interface (api), java beans is a neutral platform component structure for the java application environment.

Introduction to java language pack (java.lang)The java language package (java.lang) defines most of the basic classes in java,Called automatically by the java language,No display statement is required.This package contains the object class, which is the root node of the entire class hierarchy.Classes for basic data types are also defined,Such as:string, boolean, byter, short, etc. These classes support conversion of numeric types, manipulation of strings, etc.Here is a brief introduction.

math class

The math class provides commonly used mathematical operations and two mathematical constants, math.pi and math.e.This class is final and cannot be inherited.The methods and properties in the class are all static,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 changing them.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 ("The 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:The random number is:12 26 21 68 56 98 22 69 68 31

Since random numbers are generated,The results of Example 8-2 will not be the same each time.To generate a random number between [a, b], the general formula is:

 (B-a + 1) * math.random () + a

String class

A string is a sequence of characters.In Java, whether a string is a constant or a variable is implemented using an object of a class.java.lang provides two string classes:the string class and the stringbuffer class.

1.string class

According to the java language,The string class is a sequence of unicode characters of immutable,Its role 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 altering the original string.In fact, all the results of changing strings are generating new strings,Instead of changing the original string.

Strings are very similar to arrays,The position of the character in the string is also indicated by the index number,Numbering starts at 0, the second character is numbered 1, and so on.If the number i want to access is outside the legal range,The system generates a stringindexoutofboundsexecption exception. If the value of index is not an integer,A compilation error will occur.

The string class provides several methods for creating strings as shown in the following table.

2.stringbuffer class

The string class cannot change the contents of a string object,String changes can only be achieved by creating a new string.If the string needs to change dynamically,You need to use the stringbuffer class. The stringbuffer class is mainly used to add, modify, and delete string content, which means that the memory space of this type of object entity can be automatically changed in size.In order to store a variable character sequence.

Three constructors provided by the stringbuffer class

Several common methods of the stringbuffer class

  • Previous PHP remove backslash \ from json string and remove backslash before double quotes
  • Next The difference between substr and substring in Javascript