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 .
"Insert diagram is required here"
- exponential function method
"Insert diagram is required here"
"Insert diagram is required here"
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
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)
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"
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