Javase Basics (7)-Common classes

Source: Internet
Author: User
Tags wrapper stringbuffer

I. Packaging Class 1, understanding

Java provides reference types for eight basic data type one by one, which makes it easy to use the properties and methods inside

2. Packing type

Byte-->byte
Short-->short
Int-->integer
Long-->long
Float-->float
Double-->double
Char-->character
Boolean-->boolean

3. Common methods

(1) Integer/byte/short/long/float/double/boolean

Take integer as an example: integer i = 127;
int a = I.intValue (); Convert an integer type to an int type
Integer i2=integer. valueOf (variable); converting other types to corresponding wrapper types
int b = Integer. ParseInt(s);//convert string to int
String S1 = Integer. tohexstring (i);//Converts an integer to the corresponding hexadecimal
String s2 = Integer. tooctolstring (i);//convert integers to the corresponding octal

(2) Character class:

Isletter: Judging whether it is a letter
IsDigit: Judging whether it is a number
Isuppercase: Deciding whether to capitalize
Islowercase: To determine if it is lowercase
Iswhitespace: Determine if it is a space
toUpperCase: Convert to uppercase
toLowerCase: Convert to lowercase

4. Precautions

If the parameter type is a base type, the change of the formal parameter does not affect the actual argument
If the parameter type is a reference type, the argument does not affect the argument if it changes the address
parameter is affected if the member (attribute) is changed
For the eight-pack classes, only the address can be changed each time, it cannot be a member, (reason: The value member maintained inside is the final decoration), so if the parameter type is a wrapper type, the change of the parameters does not affect the actual argument

Ii. Description of String1, String class

The string class is java. A class for saving strings in the lang package

2. Create Object ★

Way One:
String s = "Hello";
Way two:
string s = new string ("Hello");
Difference: ★
Method One: First go to the constant pool to determine if there is a "hello" object, if there is a direct point, if not, you need to create, and then point to
Method Two: First go to the heap to create the string object, and then through the S reference point to the object. The object maintains the property of value, then determines whether there is a "hello" object in the constant pool, and if so, points directly through value, if none is created, by value

3. Immutable properties of the String class ★

The string class holds strings constant , the value inside cannot be changed, and every time you see a change, the address is changed . Cause: The Value property maintained in string is final decorated

4. Common methods of String class ★

SUBSTRING: Intercept substring
IndexOf: Finds the first occurrence of a character or string, and returns 1 if it is not found
LastIndexOf: Finds the last occurrence of a character or string, returns 1 if not found
toUpperCase: Convert to uppercase
Equalsignorecase: Ignore case-insensitive judgments equal
toLowerCase: Convert to lowercase
ToCharArray: Converting to a character array
Trim: Go space before and after
Format: Formatting string
Concat: Stitching Strings
Length: Gets the number of characters
Intern: Determine if there is an object in the constant pool, and if there is a direct point, create and then point to
Split: Split string
CompareTo: Determines the size of two strings, if the former is large, returns a positive number if the former is small, returns a negative number if equal returns 0
CharAt: Gets the character at the specified index
StartsWith: Judging whether to start with XX
EndsWith: Judging who ends with XX
ValueOf: Converting other types to strings
equals: Determines whether the contents of two strings are equal
ToString: Overridden the object class's ToString to return the string contents

Iii. StringBuffer and StringBuilder1, description

StringBuffer belongs to Java. A class in the Lang packet that holds the string, equivalent to the enhanced version of string

2. StringBuffer Create Object

Form one: Constructs a StringBuffer object that maintains an initial capacity of char[] value
StringBuffer s = new StringBuffer ();
Form two: Constructs a StringBuffer object, which maintains the initial capacity for the specified capacity char[] Value
StringBuffer s = new StringBuffer (int capacity);
Form three: Constructs a StringBuffer object, which maintains the initial capacity of str.length () +16 char[] Value
StringBuffer s = new StringBuffer (str);

3. Conversion of StringBuffer and string

(1) String-->stringbuffer
StringBuffer buffer = new StringBuffer(string);
(2) stringbuffer-->string
String s = buffer. toString ();
Or
string s = new string(buffer);

4. Common methods of StringBuffer ★

Append: Appends any type of element to the buffer object, returning the StringBuffer type
Delete: Deletes the element in the specified range, returning the StringBuffer type
Replace: Replaces the element within the specified range content, returning the StringBuffer type
IndexOf: Find the index of the string, if not found, returns-1
Insert: Inserts any type of element at the specified index, returning the StringBuffer type
Reverse: Invert
Length: Long

5, String, StringBuffer, StringBuilder (1) Comparison of string and StringBuffer

Same point:
Both string and StringBuffer are used to hold strings, and there are methods for string manipulation and manipulation, such as getting length, finding, stitching, etc.
Different points:
The string class holds strings constants , and once the values are configured, they cannot be changed, each change is a change of address and need to be re-directed , less efficient
StringBuffer Save is a string variable , the value of the inside even if the configuration, can also change, do not need to re-point , adding and deleting the efficiency of the higher

(2) Comparison of StringBuffer and StringBuilder

Same point:
StringBuffer and StringBuilder are all string variables , creating objects and common methods are the same
Different points:
StringBuffer version 1.0, thread safe , low efficiency
StringBuilder version 1.5, thread insecure , high efficiency

Iv. Math1, description

The math class is java. A class for handling and manipulating numeric types in the lang Packet, which provides a series of static methods

2. Common methods

ABS Absolute
Min Small value
Floor Down rounding
Random number
Ceil rounding up
Max Large value
round rounding: For negative numbers, add 0.5 First, then take the entire
sqrt to seek a prescription
Pow exponentiation

V. Arrays1, description

The arrays class is a class for managing arrays under the java.util package, which provides a series of static methods

2. Common methods

sort (array name) : natural sort. Sorts according to the comparison rules of the array elements themselves.
Requirements: The array element must implement the comparable interface and implement the abstract method inside CompareTo
sort (array name, comparator): custom Sort . Sort according to the comparison rules of the provided comparator
BinarySearch (array name, element to find): Find by binary search method . Returns a negative number if the index of the returned element is found
Requirements: The array is already sorted
BinarySearch (array name, element with lookup, comparator): Find by binary search method. Returns a negative number if the index of the returned element is found
Requirements: The array is already sorted
copyOf (old array, length of new array): Copy array, return a new array of a specified length
Fill (array name, element to be filled): fills the specified element into the array
equals (array 1, array 2): Compares two elements of an array for equality
toString (Array): The elements of the array are stitched together, and the string is eventually returned

Vi. System1, description

The system class is a class in the Java. lang package that is related to systems information and holds a series of static fields and methods

2. Common methods

GC: Running a garbage collection mechanism, the JVM did its best, but not necessarily immediately recycled
ArrayCopy: Copying an array
Exit: Exit
Currenttimemillis: Gets the number of milliseconds in the current time distance of 1970-1-1
getProperties: Gets the list of properties for the current system

Vii. date 1, the first generation

Date d = new Date();
SimpleDateFormat SDF = new SimpleDateFormat("Yyyy-mm-dd hh hour MM min ss sec");
String datestring = Sdf.format (D);

2. Second generation

Calendar C = calendar.getinstance();
int year = C.get (calendar.year);
int month = C.get (calendar.month) +1;
int day = C.get (calendar.day_of_month);
int hour = C.get (Calendar.hour);
int minute = C.get (Calendar.minute);
int second = C.get (Calenar.second);
String datestring = String.Format("%d-%d-%d%d hours%d minutes%d seconds", year,month,day,hour,minute,second);

3. Third generation

LocalDateTime Ldt = localdatetime.now();
DateTimeFormatter DTF = datetimeformatter.ofpattern("Yyyy-mm-dd hh hour MM min ss sec");
String datestring = dtf.format(LDT);

Viii. BigInteger and BigDecimal1, BigInteger Class (1) description

BigInteger belongs to Java. A class that holds large integers under the Math package is larger than the integer range saved by long.

(2) Common methods

Add Plus
Substract minus
Multiply multiply
Divide except
Max Large value
Min Small value

2, BigDecimal (1) Description

BigDecimal belongs to Java. A class under the Math package that holds a higher precision floating-point type, which is more accurate than a double-saved floating-point type, and is better suited for handling currency operations with higher precision requirements

(2) Common methods

Add Plus
Substract minus
Multiply multiply
Divide except
Max Large value
Min Small value

Javase Basics (7)-Common classes

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.