Java.lang Bag Learning (transfer from the micro-science garden)

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

The Java language Pack (Java.lang) defines most of the basic classes in Java, which are called automatically by the Java language and do 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


"Example 8-2" produces a random integer between 10 10~100.
Ep8_2.java **********
Class ep8_2{
public static void Main (String args[]) {
int A;
System.out.print ("Random number:");
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: 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

The

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
in 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, with the index number indicating 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.

Java.lang Bag Learning (transfer from the micro-science garden)

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.