Java Basics Four Math class character strings console input and output StringBuilder and StringBuffer

Source: Internet
Author: User
Tags abs mathematical functions stringbuffer

Fourth: Math functions, characters, and strings math class

Math is the final class: in Java.lang.Math, all mathematical functions are static methods

In a Java program,all classes in the Java.lang package are implicitly imported .

    • Trigonometric methods

"Insert diagram is required here"

    • exponential function method

"Insert diagram is required here"

    • Rounding Method

"Insert diagram is required here"

    • Min, Max, ABS method

Max (double x,double y): Returns the larger number of X and Y
Min (doublex,double y): Returns the x, y smaller number

The following is correct: Math.max (2.5,3) . Returns 3.0.

Abs method returns absolute value

    • Random method

Math.random method generates a random number of type double between [0.0,1.0]

A + (int) (Math.random () *b) returns [A, a+b]
A + (int) (Math.random () * (b+1)) returns [A, a+b]

Character

Unicode codes are used in Java characters. A character of two bytes, denoted by a four-digit hexadecimal number that begins with \u. From ' \u0000 ' to ' \u007f ' corresponds to 128 ASCII codes.

The methods of the character class are as follows:

"Insert diagram is required here"

Type conversions and shaping direct volume type conversions are similar to the following:

        byte b = ' a '; // correct        int i = ' a '; // correct        b = ' \ufff4 '; // Error        b = (byte) ' \ufff4 '; // correct
String

String class: is a final class. Represents a fixed-length sequence of characters that cannot be changed after instantiation

    • Simple Methods for string objects

"Insert diagram is required here"

    • Comparison method for string objects

"Insert diagram is required here"

Note : the operator = = can only check if a srting variable points to the same object, and the equal method determines whether the contents of the two string variables are the same.

    • The String class contains methods to get substrings

"Insert diagram is required here"

    • Conversion between strings and numbers

The ValueOf method converts the base data type to a string. For example
String S1 = string.valueof (1.0); "1.0"
String s2 = string.valueof (true); "True"
string conversion to base type
Double.parsedouble (str)
Integer.parseint (str)
Boolean.parseboolean (str)

    • Format Console Output

Formatted string:% [-+ 0,][width][.precision] Format descriptor
String.Format (format, item1, item2, ...); + and space mark to display sign
Format descriptor: Not truncated when width is not sufficient
%b Boolean value
%c character
%d decimal integers
%f floating-point numbers, including double types
%e,%e Scientific counting method
%s string
String.Format ("Format $:%1d,%2s", "the" "ABC"); Result "Format $:99,abc"

    • Console input

Scanner Class (Java.util.Scanner)
Scanner Scanner = new Scanner (system.in);
The parameter type of the constructor scanner can also be java.io.File
Double d = scanner.nextdouble ();
Method:
Nextbyte (), Nextshort (), Nextint ()
Nextlong (), Nextfloat (), nextdouble ()
Next (), nextline ()

Next () method reads a string ending with a white-space character (', ' \ t ', ' \f ', ' \ R ', ' \ n ')

The Nextline () method reads a line of string with the ENTER key as the end flag.

To read a single character, you need to read the string first and then get the first character of the string.

    • StringBuilder and StringBuffer

StringBuilder and StringBuffer (final Class) can also modify the string after initialization.
The StringBuffer method for modifying buffers is synchronous and more suitable for multitasking environments.
StringBuilder is similar to the StringBuffer working mechanism in single-tasking mode.

Here's how:

"Insert diagram is required here"

Java Basics Four Math class character strings console input and output StringBuilder and StringBuffer

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.