Java know how (76) Language Pack (Java.lang) Introduction

Source: Internet
Author: User
Tags mathematical constants natural logarithm sin square root

The Java language Pack (Java.lang) defines most of the basic classes in Java, called automatically by the Java language and does not need to display claims . The package contains the object class, which is the root node of the entire class hierarchy and also defines the classes of the base data type, such as String, Boolean, Byter, short, and so on. These classes support the conversion of numeric types and the manipulation of strings, and so on, which are briefly described below.

Math class

The Math class provides common mathematical operations and two mathematical constants for Math.PI and MATH.E. The class is final and cannot be inherited, the methods and properties in the class are all 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 and you cannot make any changes to it. Table 8-1 lists the main methods of the math class.

table 8-1 The main methods of the math class
Method function
int abs (int i) To find the absolute value of an integer (another method for long, float, double)
Double Ceil (double D) Smallest integer not less than D (return value is type Double)
Double floor (double D) Maximum integer less than D (return value is double)
int max (int i1,int i2) Maximum number of two integers (other methods for long, float, double)
int min (int i1,int i2) Find the minimum number of two integers (other methods for long, float, double)
Double Random () Generate a random number between 0~1
int round (float f) Find the integer nearest to F
Long round (double D) Find the long integer closest to D
Double sqrt (double A) Find square root
Double sin (double D) The Sin value of D (other methods of finding trigonometric functions such as Cos,tan,atan)
Double log (double x) Seeking natural logarithm
Double exp (Double x) x power of E (ex)
Double Pow (double A, double b) Ask for the power of B of a

1"Example 8-2" produces 10 10~a random integer between 100. 2 //********** Ep8_2.java **********3 classep8_2{4 Public Static voidMain (String args[]) {5 intA;6System.out.print ("Random number:");7 for(inti=1;i<=10;i++){8A= (int) ((100-10+1) *math.random () +10);9System.out.print ("" +a);Ten } One System.out.println (); A } -}

Running Result: Random number: 12 26 21 68 56 98 22 69 68 31


As the result is a random number, example 8-2 will not be the same for each run. To produce a random number between [A, b], its general formula is:
(b-a+1) *math.random () +a

String class

A string is a sequence of characters. In Java, a string, whether it is a constant or a variable, is implemented with the object of the class. Java.lang provides two kinds of string classes: The String class and the StringBuffer class.

1.String class
According to the Java language, the string class is a sequence of immutable Unicode characters that implements a static string that cannot be changed. For example, the result of connecting two strings is to generate a new string without changing the original string. In fact, all the results of changing a string are generating a new string instead of changing the original string.

The string is similar to the implementation of an array, and is also indicated by the index number to indicate the position of the character in the string, starting with a number of 0, the 2nd character being numbered 1, and so on. If the number to be accessed is not in the legal range, the system generates a Stringindexoutofboundsexecption exception. If the value of index is not an integer, a compilation error is generated.

The string class provides several methods of creating strings, as shown in table 8-2.

Table 8-2 String Creation method
Method function
String s= "Hello!" Automatically creates a string instance with a literal constant.
String S=new String (string s) Passed to the construction method through a String object or string constant.
Public String (Char value[]) Assigns the entire character array to the String construction method.
Public String (char value[], int offset, int count) Assigns a portion of a character array to the String construction method, offset is the starting subscript, and count is the length of the sub-array.


2.StringBuffer class
The string class cannot change the contents of a string object, but it can only be achieved by creating a new strand. If the string needs to change dynamically, it needs to use the StringBuffer class. The StringBuffer class is mainly used to implement the addition, modification, and deletion of the string content, which means that the memory space of the object entity can automatically change size, so as to store a variable sequence of characters.

Three construction methods provided by the StringBuffer class
Construction Method Description
StringBuffer () The StringBuffer object, created using the parameterless construction method, has an initial capacity of 16 characters, and the object's capacity increases automatically when the object holds a sequence of characters greater than 16 characters. The object can get the length of the sequence of characters stored in the entity through the long () method, and the actual capacity of the current object is obtained through the capacity () method.
StringBuffer (int length) The StringBuffer object, created using this construction method, has an initial capacity of the number of characters specified by the parameter length, and the object's capacity automatically increases when the length of the character sequence that the object holds is longer than length, in order to hold the added characters.
StringBuffer (Strin str) The StringBuffer object created with this construction method has an initial capacity of the parameter string str length plus 16 characters.

Some common methods of StringBuffer class
Method Description
Append () Use the Append () method to convert other Java type data to a string and then append it to the StringBuffer object.
Insert (int index, String str) The Insert () method inserts a string into a position in the character sequence of the object.
Setcharat (int n, char ch) Replaces the character in the character sequence N of the current StringBuffer object with the character specified by the parameter ch, and the value of n must be non-negative and less than the length of the string sequence in the current object.
Reverse () Use the reverse () method to flip a sequence of characters in an object.
Delete (int n, int m) Deletes a sequence of substrings from a sequence of characters in the current StringBuffer object. Here n Specifies the subscript for the first character to be deleted, and M specifies the subscript for the next character of the last character to be deleted, so the deleted substring is from n~m-1.
replace (int n, int m, String str) The sequence of characters in the object is replaced with STR, and the substituted sub-character sequence is specified by subscript N and M.
Series Articles:Java know how much (interface) Java know how much (40) interface and the difference between the abstract class Java know how much (41) Generic detailed Java know how much (42) generic wildcard and type parameter range Java know how much (43) exception handling basic Java know how much (44) exception Type Java know how much (45) uncaught exception Java knows how many (47) try and catch Java know how many (+) multiple catch statements using Java know how much (in a) Try statement nested Java know how much (a) Throw: Exception thrown Java know how much (in) Jav A Throws clause Java know how much (Finallyjava) know how much (52) built-in exception Java know how much (53) Use Java to create their own exception subclass Java know how much (54) Assertion detailed Java know how much (55) thread Java know how much (56) threading Model Java know how much (57) Main thread Java know how much (58) Threads Runnable interface and thread class detailed Java know how much (59) Create multithreaded Java know how much () isAlive () and join () Use Java know how much (61) Thread Priority Java know how much (62) thread synchronization Java know how much (63) thread to communication Java know how much (64) Thread deadlock Java know how much (65) thread suspend, resume and terminate Java know how much (66) Input output (IO) Overview of the stream and flow Java know how much (67) character-oriented input stream Java knows how much (68) character-oriented output stream Java knows how much (69) byte-oriented input and output stream Java knows how much (70) The application Java know how much (71) file and directory management Java know how much (72) the random Read and write ja VA know how much (73) file compression processing Java know how much (74) Basic class library Java know how much (the) object class

Java know how (76) Language Pack (Java.lang) Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.