String, stringbuffer, and math

Source: Internet
Author: User
Tags acos asin mathematical functions
This article will introduce some main Java classes, such as string, stringbuffer, and math.

Java is a real object-oriented language. Even a simple program must be designed. Java itself also provides many designed classes for us. To use Java for programming flexibly, it is essential to be familiar with these main classes of Java.

String type
As the name implies, string indicates a string. This class is a String constant class. I believe that anyone who has used C language programming knows what the string is, so I will not go into details here. Note that the strings in Java are different from those in C. In C language, there is no real string. in C language, strings are character arrays, which are very flexible to use. In Java, the string constant is a class ?? String class, which is different from character arrays.

The following describes the constructor of the string class.

Public String ()

This constructor is used to create an empty String constant.

For example, string test = new string ();

Or: string test;

Test = new string ();

Public String (string value)

This constructor uses an existing String constant as a parameter to create a New String constant.

It is also worth noting that Java will create a string class object for each string constant enclosed by double quotation marks. For example, string K = "hi."; Java creates a string class object for "hi." And assigns this object to K. Equivalent:

String temp = new string ("hi .");

String K = temp;

The usage of this constructor is as follows:

String test = new string (k); (Note: K is a string class Object)

String test = new string ("Hello, world .");

Public String (char value [])

This constructor uses a character array as a parameter to create a New String constant.

Usage example:

Char Z [] = {'h', 'E', 'l', 'l', 'O '};

String test = new string (z );

(Note: The content in test is "hello ")

Public String (char value [], int offset, int count)

This constructor is an extension of the previous one. In one sentence, it uses the character array value to take the Count character from the offset character to create a string class object. Usage example:

Char Z [] = {'h', 'E', 'l', 'l', 'O '};

String test = new string (z, 1, 3 );

(Note: The content in test is "ell ")

Note: In the array, subscript 0 indicates the first element, and 1 indicates the second element ......

If the start point offset or the number of truncated count exceeds the threshold, an exception occurs.

Stringindexoutofboundsexception

Public String (stringbuffer buffer)

This constructor uses a stringbuffer class object as a parameter to create a New String constant.

The string class is a String constant, while the stringbuffer class is a string variable, which is different. The stringbuffer class will be introduced later.

The methods of the string class include:

Public char charat (INT index)

This method is used to obtain a character in a String constant. The index parameter specifies the number of characters returned from the string. This method returns a variable of the character type. Usage example:

String S = "hello ";

Char K = S. charat (0 );

(Note: The value of K is 'H ')

Public int compareto (string anotherstring)

This method is used to compare the size of a String constant. The parameter anotherstring is another String constant. If the two string constants are the same, the return value is 0. If the current String constant is large, the return value is greater than 0. If another String constant is large, the return value is smaller than 0. Usage example:

String S1 = "ABC ";

String S2 = "Abd ";

Int result = s2.compareto (S1 );

(Note: The result value is greater than 0, because D is placed behind C in the ASCII code, then S2> S1)

Public String Concat (string Str)

This method sets the parameter ?? String constant STR is followed by the current String constant to generate a new String constant and return it. Usage example: String S1 = "How do ";

String S2 = "you do? ";

String Ss = s1.concat (S2 );

(Note: The SS value is "How do you do? ")

Public Boolean startswith (string prefix)

This method is used to determine whether the current String constant is a parameter ?? Prefix String constant. Yes, return true. No, false is returned. Usage example:

String S1 = "abcdefg ";

String S2 = "BC ";

Boolean result = s1.startswith (S2 );

(Note: The value of result is false)

Public Boolean startswith (string prefix, int toffset)

The toffset parameter added in this overload method specifies the start point for search.

Public Boolean endswith (string suffix)

This method is used to determine whether the current String constant is a parameter ?? End of a suffix String constant. Yes, return true. No, false is returned. Usage example:

String S1 = "abcdefg ";

String S2 = "FG ";

Boolean result = s1.endswith (S2 );

(Note: The value of result is true)

Public void getchars (INT srcbegin, int srcend, char DST [], int dstbegin)

This method is used to extract a string from a String constant and convert it to a character array.

The srcbegin parameter is the starting point of the truncation, srcend is the ending point of the truncation, DST is the target character array, and dstbegin specifies the position where the truncated string is placed in the character array. In fact, srcend is the truncation end point plus 1, and srcend-srcbegin is the number of characters to be truncated. Its usage is as follows:

String S = "abcdefg ";

Char Z [] = new char [10];

S. getchars (2, 4, Z, 0 );

(Note: The value of Z [0] Is 'C', the value of Z [1] is 'D', and two characters 4-2 = 2 are truncated)

Public int indexof (INT ch)

The return value of this method is the position where the character ch first appears in the String constant from left to right. If the String constant does not contain this character,-1 is returned. Usage example:

String S = "abcdefg ";

Int R1 = S. indexof ('C ');

Int r2 = S. indexof ('x ');

(Note: The value of R1 is 2, and the value of R2 is-1)

Public int indexof (int ch, int fromindex)

This method is an overload of the previous method. The new parameter fromindex is the start point of the search. Usage example:

String S = "abcdaefg ";

Int r = S. indexof ('A', 1 );

(Note: The R value is 4)

Public int indexof (string Str)

This overload method returns the position where the String constant STR appears for the first time from left to right in the current String constant. If the current String constant does not contain the String constant STR,-1 is returned. Usage example:

String S = "abcdefg ";

Int R1 = S. indexof ("cd ");

Int r2 = S. indexof ("ca ");

(Note: The value of R1 is 2, and the value of R2 is-1)

Public int indexof (string STR, int fromindex)

The new fromindex parameter in this overload method is the start point of the search.

 

The following four methods are used in a similar way to the preceding four methods, but are searched from right to left in a String constant.

Public int lastindexof (INT ch)

Public int lastindexof (int ch, int fromindex)

Public int lastindexof (string Str)

Public int lastindexof (string STR, int fromindex)

Public int length ()

This method returns the length of a String constant, which is the most common method. Usage example:

String S = "ABC ";

Int result = S. Length ();

(Note: The value of result is 3)

Public char [] tochararray ()

This method converts the current String constant to a character array and returns the result. Usage example:

String S = "who are you? ";

Char Z [] = S. tochararray ();

Public static string valueof (Boolean B)

Public static string valueof (char C)

Public static string valueof (int I)

Public static string valueof (long l)

Public static string valueof (float F)

Public static string valueof (double D)

The preceding six methods can convert values of the Boolean, Char, Int, long, float, and double types into string objects. Usage example:

String R1 = string. valueof (true); (Note: The value of R1 is "true ")

String r2 = string. valueof ('C'); (Note: The value of R2 is "C ")

Float FF = 3.1415926;

String R3 = string. valueof (ff); (Note: The R3 value is "3.1415926 ")

 

Stringbuffer class

The string class is a String constant and cannot be changed. Stringbuffer is a string variable, and its objects can be expanded and modified.

Constructor of the string class

Public stringbuffer ()

Create an empty stringbuffer class object. Public stringbuffer (INT length)

Create a stringbuffer class object with the length parameter.

Note: If the length is smaller than 0, the negativearraysizeexception is triggered.

Public stringbuffer (string Str)

Use an existing String constant to create the stringbuffer class object.

The stringbuffer class has the following methods:

Public String tostring ()

Convert to a string class object and return it. Because the parameters of the display method in most classes are mostly string objects, it is often necessary to convert the stringbuffer class objects to string objects and display their values. Usage example:

Stringbuffer sb = new stringbuffer ("how are you? ");

Label L1 = new label (sb. tostring ());

(Note: How are you declaring the content of a label object L1 and L1 ?)

Public stringbuffer append (Boolean B)

Public stringbuffer append (char C)
Public stringbuffer append (int I)
Public stringbuffer append (long l)

Public stringbuffer append (float F)

Public stringbuffer append (double D)

The preceding six methods can append variables of the Boolean, Char, Int, long, float, and double types to the objects of the stringbuffer class. Usage example:

Double D = 123.4567;

Stringbuffer sb = new stringbuffer ();

SB. append (true );

SB. append ('C'). append (d). append (99 );

(Note: The Sb value is truec123.456799)

Public stringbuffer append (string Str)

Append the String constant STR to the end of the stringbuffer class object.

Public stringbuffer append (char STR [])

Append STR to the end of the stringbuffer class object.

Public stringbuffer append (char STR [], int offset, int Len)

Returns the string array 'str', which starts from the offset value and ends with the object of the stringbuffer class.

Public stringbuffer insert (INT offset, Boolean B)

Public stringbuffer insert (INT offset, char C)

Public stringbuffer insert (INT offset, int I)

Public stringbuffer insert (INT offset, long l)

Public stringbuffer insert (INT offset, float F)

Public stringbuffer insert (INT offset, double D)

Public stringbuffer insert (INT offset, string Str)

Public stringbuffer insert (INT offset, char STR [])

Insert a Boolean, Char, Int, long, float, double type variable, string class object, or character array to the nth offset position of the stringbuffer class object. Usage example:

Stringbuffer sb = new stringbuffer ("abfg ");

SB. insert (2, "CDE ");

(Note: The Sb value is abcdefg)

Public int length ()

This method returns the length of the string variable, which is similar to the length method of the string class.

 

Math class

Mathematics contains many mathematical functions, such as sin, cos, exp, and abs. Math is a tool class that plays an important role in solving problems related to mathematics.

This class has two static attributes: E and PI. E 2.7182818 in e-generation table mathematics, and PI represents PI 3.1415926.

For example, math. E and math. Pi.

Methods of this class include:

Public static int ABS (int)

Public static long ABS (long)

Public static float ABS (float)

Public static double ABS (double)

The ABS method is used to calculate the absolute value.

Public static native double ACOs (double)

Returns the arc cosine of ACOs.

Public static native double asin (double)

Asin calculates the arcsin function.

Public static native double atan (double)

Atan antitangent function.

Public static native double Ceil (double)

Ceil returns the smallest integer greater than.

Public static native double cos (double)

Cos returns the cosine.

Public static native double exp (double)

Exp is used to evaluate the power of E.

Public static native double floor (double)

Floor returns the largest integer less than.

Public static native Double Log (double)

Log returns LNA.

Public static native double POW (double A, double B)

Pow calculates the power of B for.

Public static native double sin (double)

Sin calculates the sine function.

Public static native double SQRT (double)

SQRT calculates the square of.

Public static native double Tan (double)

Tan returns the positive tangent function.

Public static synchronized double random ()

Returns a random number between 0 and 1.

When using these methods, use math. ***** (***** as the method name ). Usage example:

Int A = math. Abs (124 );

Int B = math. Floor (-5.2 );

Double S = math. SQRT (7 );

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.