First, Scanner class
Scanner class: Simple text scanner
1, keyboard entry steps:
(1) Need to create keyboard input object: Scanner keyboard input Object =new Scanner (system.in);
(2) Guide package: Import java.util.Scanenr (shortcut key: Ctrl+shift+o);
(3) Receive data: XXX variable name = Keyboard Entry object. NEXTXXX (); (for example: int variable name = Keyboard Entry object. Nextint ();).
2. Construction method
Public Scanner (InputStream source): Enter data in the form of an input stream.
InputStream: Byte input stream.
InputStream in = system.in; The underlying execution returns a byte input stream (the standard input stream).
3. Common methods
public boolean hasnextxxx (): Determines whether the current scanner has the next XXX type data that can be entered.
4. Precautions
(1) keyboard input two int type data, can be output separately.
(2) keyboard input two string type of data, can be output separately.
(3) First input a string type of data, and then input int type of data, can be output separately.
(4) First input an int type of data, and then input a string type of data, respectively output error.
Reason: Input of the second data is a string type, the next data input needs "space" itself is a string, the space has not been entered.
WORKAROUND: Re-create the keyboard entry object before entering the second data.
For example:
5. Common anomalies
Java.util.InputMismatchException: The input data and the received data type do not match the exception.
Second, object class
Object: The root class of the class hierarchy, with each class using Object as the superclass (parent class).
1. Common methods
(1) public int hashcode () returns the hash code value of the object. An address value that can be understood as an address value, but not an actual meaning.
(2) Public final class GetClass () returns the run-time class of this object.
(3) public string GetName () is returned as a string (a method of Class).
For example:
ToString Method: Public String toString (); Returns the string representation of the object. It is recommended that all subclasses override this method. When the ToString method is not overridden, the output is the address value, and if you do not want to output the address value directly, you need to override the ToString () method.
(4) Equals method: Public boolean equals (Object obj) indicates whether an other object is "equal" to this object. Where: a, = =: The comparison is the address value is equal.
B, the method in the Equals:object class, the default comparison is whether the address value is the same (the two objects that are actually executed at the bottom of the Equals method are in = =, the comparison is the address value). However, when this method is overridden in a custom class, the content of the two objects is compared for equality.
C, for example:
(5) Clone () Method: Protected Object Clone () creates and returns a copy of this object. The object class Clone () method performs a specific copy operation.
A, if the class of this object can not implement interface cloneable, it will throw clonenotsupportedexception that the clone does not support the exception, all arrays are considered to implement the interface cloneable.
B, when using the Clone () method, implement the Cloneable interface in a custom class and override the Clone method in that class, otherwise it cannot be called.
C. Exception clonenotsupportedexception when calling the Clone method, workaround: Throw the exception onto the method declaration.
D, for example:
Implementation class:
Test class:
Third, String class
String: Represents a string, which is a constant whose value cannot be changed after it is created. String is a special type of reference, and the default value is null. The string object is immutable, so it can be shared.
1, string common constructs the method
(1) String (): no parameter construction.
(2) string (byte[] bytes): Converts a byte array into a string.
(3) public string (byte[] bytes, int index,int length): Converts part of a byte array to a string.
(4) public string (char[] value): Converts a character array into a string.
(5) ToCharArray (): Converts a string into a character array.
(6) public string (char[] value, int index, int count): Converts a portion of a character array to a string.
(7) public string (string original): Constructs a string constant as a string object.
2. Common judgment function of string class
(1) Boolean equals (Object obj): Compares this string to a specified object.
(2) Boolean equalsignorecase (String str) compares this array of strings to another STR string, regardless of case.
(3) Boolean contains (String str): Determines whether or not the current string contains a str substring (emphasis).
(4) Boolean startsWith (String str): Starts with the current STR string (emphasis).
(5) Boolean endsWith (String str): ends with the current STR string (emphasis).
(6) Boolean isEmpty (): Determines whether the string is empty.
3. Common methods of String class:
(1) public int length () returns the lengths of this string.
Related interview questions: What is the method of finding lengths in arrays, strings, and collections?
Solution: The Length property in the array is the length () method in the string, and the collection is the number of elements that get the collection: size ().
(2) public string concat (String str): Unique function of string, splicing function.
4. Common acquisition function of String class
(1) public int length () Gets the lengths of the strings.
(2) public char charAt (int index) returns the character at the specified index.
(3) public int indexOf (int ch) returns the index of the first occurrence of the specified character in this string.
(4) public int indexOf (int ch,int fromIndex) returns the index of the first occurrence of the specified character in this string and searches starting at the specified index.
(5) public int indexOf (string str) returns the index of the first occurrence of the specified substring in this string.
(6) public int indexOf (string str,int fromIndex) returns the index of the first occurrence of the specified string in this string and searches starting at the specified index.
5. Commonly used interception function of String class
(1) public string substring (int beginindex): Intercepts from the specified position, the default intercept to the end, and returns a new string.
(2) public String substring (int beginindex, int endIndex): ends at the end of the specified position from the beginning of the specified position, and after the package is not wrapped, the package left does not wrap to the right.
6. Common conversion function of string
(1) Public byte[] GetBytes (): Converts a string to a byte array.
(2) Public char[] ToCharArray (): Converts a string into a character array (emphasis).
(3) public static string valueOf (int i): Converts data of type int to string (emphasis), which can convert any type of data into string type.
(4) public string toLowerCase (): All characters in the string are converted to lowercase.
(5) public string toUpperCase (): All characters in the string are capitalized.
7. Other features of type string
(1) public string replace (char Oldchar,char Newchar): Replaces a character in the current string with a new character.
(2) public string Replace (string oldstr,string newstr): Replaces a substring in the current string with a new one.
(3) public string trim (): Removes whitespace at both ends of the string.
(4) public int compareTo (string anotherstring) compares two strings in dictionary order, which is based on the Unicode value of each character in the string.
This method returns the calculation method for the INT value:
A, the first is to represent each string as a character array;
B, get the length of each character array len1, len2;
C, get a small value of two character array length Lim;
D, define an index variable k,k starting from 0:
A, when K is less than the two-character array length of the small value of the Lim value:
(a) Get two arrays of K from 0 to lim the corresponding character array value becomes two new character array c1,c2;
(b) Compare the character values for each index in the two new character array C1, C2, respectively:
If C1[K]!=C2[K], then return c1[k]-c2[k];
If C1[K]==C2[K], then return C1-C2;
b, when K is less than the value of Lim, return len1-len2.
8. Important features of String class
(1) Once the string is assigned, the value cannot be changed.
A, when assigning a constant value to a string, it is preceded by the current string in the string constant pool of the method area, and the address value is returned directly, if not, opening up new space in the string constant pool.
B. When you assign a value to a string by new, a new space is opened directly.
(2) The string class itself overrides the Equals method, eliminating the need to rewrite it to compare the content equality.
Iv. StringBuffer Class (object type)
A thread-safe variable character sequence, a string buffer.
thread safety → synchronization → inefficient execution, such as bank websites.
thread unsafe → out of sync → execution efficiency is high, for example: news website etc.
1, the construction method of StringBuffer
(1) StringBuffer (): Non-parametric construction, initial capacity 16.
(2) StringBuffer (int capacity): Specifies the capacity to construct a string buffer.
(3) StringBuffer (String str): Constructs a string buffer and initializes its contents to the specified string content.
2, StringBuffer the acquisition function
(1) public int length () returns the lengths.
(2) public int capacity () returns the current capacity (if the capacity is exceeded, the system is automatically assigned).
3. Add function of StringBuffer
(1) Public stringbuffer append (String/boolean, etc.): Appends data to the string buffer (appended at the end), and returns the string buffer itself (more in real-world development).
(2) Public stringbuffer Insert (int offset,string str): Adds the current STR string to the specified position, returning the string buffer itself.
4, StringBuffer the deletion function
(1) Public stringbuffer deletecharat (int index): Removes the character from the specified position.
(2) Public stringbuffer Delete (int start,int end): Removes a substring from start to end-1.
5, StringBuffer of the reversal function
Public StringBuffer reverse (): Reverses the character sequence reversal in the buffer and returns itself.
6, the interception function of StringBuffer
(1) public string substring (int start): Intercept from the specified position, the default intercept to the end, the return value is not the buffer itself, is a new string.
(2) public string substring (int start,int end): From the specified position to the specified position end interception, after the packet is not wrapped, the return value is not the buffer itself, is a new string.
7, the replacement function of StringBuffer
Public stringbuffer replace (int start,int end,string str) begins at the specified position to the end of the specified position, replaces with the new STR string, and the return value is the string buffer itself.
8. Common Face Test
(1) The difference between Stringbuffer,string,stringbuilder:
Solution: Both StringBuffer and StringBuilder are a variable sequence of characters that provide a buffer, both of which are considered containers.
StringBuffer: Thread-safe, synchronous, inefficient execution, inefficient execution, but better than the string type, and is a variable sequence of characters, provides a buffer (all of which are stored in characters), is freed, and is better than the string type in memory angle.
StringBuilder: Thread insecure, unsynchronized, execution efficiency high, single-threaded priority in the use of StringBuilder.
String: An immutable sequence of characters that, when defined, opens up space in a constant pool, which is more memory-intensive.
(2) StringBuffer and array differences:
Solution: A, array: A container that can store multiple data, and the type of multiple data must be consistent, the length of the array function is the long attribute;
C, StringBuffer: is a container that is always stored in a buffer only for data of type string, and gets the length of the buffer () method.
The type of guarantee that corresponds to the basic type in Java.
Purpose: To convert a base data type to and from a string type.
1, Byte byte
2. Short Short
3, int Integer
4, Long Long
5, float float
6, double double
7, char character
8, Boolean Boolean
Six, the integer class
JDK5 Features: Automatic Disassembly box.
Int→integer: Boxing, Integer is the wrapper class type in the Int class.
Intteger→int: Unboxing.
1. Get the integer maximum function
(1) public static final int max_value gets the maximum value.
(2) public static final int min_value gets the minimum value.
2, the integer provides the static function
(1) public static String tobinarystring (int i) converts the decimal number to a binary number.
(2) public static String tooctalstring (int i) converts a decimal number to an octal number.
(3) public static String tohexstring (int i) converts a decimal number to a hexadecimal number.
3, the construction method of integer
(1) public Integer (int value)
(2) Public Integer (String s)
4. Method of converting int type and string type to each other
(1) int→string:valueof (int i);
(2) String→int:parseint (String s);
Seven, character class
The Character class wraps the value of a base type char in an object. An object of type Character contains a single field of type Char.
1, construction Method: Public Character (char value).
2. Judging function:
(1) public static Boolean isdigit (char ch) determines whether the specified character is a number.
(2) public static boolean islowercase (int ch): Determines whether the specified character is a lowercase alphabetic character.
(3) public static Boolean isuppercase (int ch): Determines whether the specified character is an uppercase character.
3. Conversion function:
(1) public static int tolowercase (int codepoint)
(2) public static int touppercase (int codepoint)
Viii. Math Class
The math class is final decorated with methods that are static and contain methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and so on.
1, public static int min (int a,int b) returns the smaller of the two int values.
2, public static double random () returns a double with a positive number, which is greater than or equal to o.o, less than 1.0 pseudo-random values, random value range: [0.0,1.0)
Basic types of packaging types in Object,scanner,string,stringbuffer,java, etc.