Java Basic Learning Notes-math functions, characters, and strings

Source: Internet
Author: User
Tags abs acos asin base 10 logarithm first string natural logarithm square root uppercase letter

4.2 Common mathematical functions

The methods in the math class are divided into three categories: trigonometric functions, exponential function Methods , and service methods . Service methods include rounding, minimum, maximum, Absolute and random methods. In addition to these methods, the math class provides two useful double constants, pi (π) and E (base of natural logarithm). You can use both variables in the form of Math.PI and MATH.E in any program.

4.2.13-Angle Function method
Sin (radians) Returns the trigonometric sine function value in radians at the angle of a bit unit
cos (radians) Returns the trigonometric cosine function value of an angle in radians
Tan (Randians) Returns the trigonometric tangent function value of an angle in radians
Toradians (degree) Converts the angle value in degrees units to radians
Todegrees (radians) Converts an angle value in radians to a degree representation
ASIN (a) Returns the inverse trigonometric sine function value of an angle in radians
ACOs (a) Returns the inverse trigonometric function cosine function value of an angle in radians
Atan (a) Returns the value of the inverse trigonometric function tangent function of an angle in radians

    

The return value of ASIN and ATAN is a radian value of-Π/2~Π/2, and the return value of ACOs is between 0 and π.

4.2.2 Exponential function method
EXP (x) Returns the X-square of E
Log (x) Returns the natural base of X
LOG10 (x) Returns the base 10 logarithm of X
Pow (A, B) Returns the B-side of a
sqrt (x) For x>=0 numbers, returns the square root of X

4.2.3 Rounding Method
Ceil (x) The x is rounded up to its nearest whole number. The integer is returned as a double-precision value
Floor (x) The x is rounded down to its nearest integer. The integer is returned as a double-precision value
Rint (x) X takes the integer nearest to it. If x is equal to two integers , an even integer is returned as a double value
Round (x) Returns (int) Math.floor (x+0.5) If X is a single-precision number, or if X is a double-precision number, returns (long) Math.floor (x+0.5)

4.2.4 min, Max and ABS methods

The Min and Max methods are used to return the minimum and maximum values for a two-digit (int, long, float, or double type).

Abs method to return the absolute value of a number (int, long, float, or double type)

  4.2.5 Random method

Random () to generate a type of double ( 0.0<= math.random () <1.0)that is greater than or equal to 0.0 and less than 1.0.

A random number (int) between 0 ~ 9 (math.random () * 10)

A random number between 50 ~ 99 + (int) (Math.random () * 50)

Usually,

A + math.random () * b Returns a random number between a ~ a + b excluding a+b

4.3 character data types and Operations 4.3.4 Character comparisons and tests

Methods in the character class

Method Describe
IsDigit (CH)

Returns true if the specified character is a number

Isletter (CH) Returns true if the specified character is a letter
Isletterordigit (CH)

Returns true if the specified character is a letter or a number

Islowercase (CH) Returns true if the specified character is a lowercase letter
Isuppercase (CH) Returns true if the specified character is an uppercase letter
toLowerCase (CH) Returns the lowercase form of the specified character
toUpperCase (CH) Returns the uppercase form of the specified character

4.4 String Type

String message = "Welcome to Java";

The String, like the system class and the scanner class, is actually a class that is expected to be defined in the Java library. The string type is not a base type, but a reference type. Any Java class can represent a variable as a reference type. A variable declared with a reference type becomes a reference variable that references an object. Here, the message is a reference variable that references a string object that contains welcom to Java.

Simple Methods for String objects

Method Describe
Length () Returns the number of characters in a string
CharAt (Index) Returns the character of the specified position in the string s
Concat (S1) Concatenate this string and string S1 to return a new string
toUpperCase () Returns a new string with all the letters capitalized
toLowerCase () Returns a new string in which all the letters are lowercase
Trim () Returns a new string, removing whitespace characters from both sides

Cases:

String message = "Welcome to Java"; System.println ("The length of  

Show

The length of Welcome to Java is 15

4.4.3 Connection string
    • String s3 = s1.concat (S2);
    • String s3 = s1 + s2;
    • The plus sign (+) can also be used to concatenate numbers and strings. In this case, the number is converted to a string before the connection is made. Note that to implement the Join function with a plus sign, at least one operand must be a string. If one of the operands is not a string (for example, a number), the non-string value is converted to a string and concatenated with another string.
    • The Enhanced + = operator can also be used for string connections.

4.4.5 reading a string from the console

For the console to read the string, call the next () method on the Scanner object.

New Scanner (system.in);
= Input.next ();

The next () method reads a string that ends with a blank character (that is, ', ' \ t ', ' \f ', ' \ r ' or ' \ n '). You can use the Nextline () method to read a whole line of text. The Nextline () method reads a string that presses the ENTER key to the end flag.

4.4.6 reading characters from the console

To read a character from the console, call the Nextline () method to read a string, and then call Charat (0) on the string to return a character.

4.4.7 string comparison

Comparison method for string objects

Method Describe
Equals (S1) Returns True if the string equals the string s1
Equlsignorecase (S1) Returns True if the string equals the string S1; case insensitive
CompareTo (S1) Returns an integer greater than 0, equal to 0, less than 0, if the table name is a string greater than, equal to, or less than S1
Comparetoignorecase (S1) Case-insensitive comparison
StartsWith (prefix) Returns True if the string starts with a specific prefix
EndsWith (suffix) Returns True if the string starts with a specific suffix
Contains (S1) Returns true if S1 is a substring of the string

4.4.8 Get substring

The String class contains methods for getting substrings

Method Describe
SUBSTRING (beginindex) Returns the substring of the string, starting at a character beginindex from a specific position to the end of the string
SUBSTRING (beginindex,endindex) Returns the substring of the string, starting at a character beginindex from a specific position to a character that is labeled endIndex-1 . Characters located in Endindex are not part of the string

4.4.9 gets the character or substring of a string

The String class contains methods to get substrings

Method Describe
INDEXOF (CH) Returns the subscript of the first ch that appears in a string.
IndexOf (Ch,fromindex) Returns the following table of the first ch that appears after Fromindex in a string.
IndexOf (s) Returns the following table of string s that appears in the string.
IndexOf (S,fromindex) Returns the subscript of the first string s that appears after fromindex in a string.
LASTINDEXOF (CH) Returns the subscript of the last ch that appears in the string.
LastIndexOf (Ch,fromindex) Returns the subscript of the last ch that appears before fromindex in a string.
LastIndexOf (s) Returns the subscript of the last string s that appears in the string.
LastIndexOf (S,fromindex) Returns the subscript of the last string s that appears before Fromindex in a string.

Note: If there is no match, it will return-1

4.4.10 the conversion between strings and numbers

int intvalue = integer.parseint (intstring); double doublevalue = double.parsedouble (doublestring);

2018/4/1

Java Basic Learning Notes-math functions, characters, and strings

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.