当前位置:网站首页>Day_ fourteen

Day_ fourteen

2022-06-25 16:25:00 grp_ grp_ grp

1. String

1.1 summary

java.lang.String : It's a string class , The bottom floor is a final Embellished char Array , therefore String Many features are the properties of arrays , For example, once the length is determined, it cannot be changed

1. Once the string is created , This string object cannot be changed

2. To improve string access and storage efficiency ,Java Virtual machines are a buffering mechanism , Save all strings to string constants

3. In the course of executing the program , A string is used to add a ,String s1 ="a" ; First, search the string constant , Is there a a, If not, create one

4. If there is , String s2 = "a"; No more creation , Take the existing one a return , So lead to String s1 ="a"; String s2 ="a"; here , Use s1==s2, He is also true, Because they point to the same string object Namely a

1.2 Basic use

The first part : Create an object , Because they all point to constant pools

The second part : If used new The way , Then the heap memory object will be created , The string object is stored in heap memory

1.3 Construction method

 

 1.4 Common methods

Study API : ① What is the function ② What are input and output parameters ③ How to use it?

 1 char charAt (int index ) : Returns... In the string , The character in the specified position

2 boolean endsWith(String suffix) : Determines whether the string ends with the specified string

   boolean startsWith(String prefix) : Determine whether the string starts with the specified string

 3 boolean equalsIgnoreCase(String str) : Case insensitive comparison of two strings for equality

4 byte[] getBytes() : Converts a string to a byte array and returns

 

5 char[] toCharArray() : Converts a string to a character array and returns

 

6 int indexOf(String str) : Gets the starting index of the specified string in the string , No return found -1

 

7 int indexOf(String str,int index) : Start at the specified location ( contain ), Gets the starting index of the specified string in the string , No return found -1

  

8  index lastIndexOf(String str) : ditto , The last index to appear No return found -1

 

9  int length() : Returns the length of the string

 

10 String replaceAll(String regex . String replacement);    Replace specified characters , regular expression

    String replace (String str . String replacement); Regular expressions are not supported

 

11 String[] split(String regex) : Split string , Return string array , regular expression , Be careful   spot , Need to escape

 

 12 String substring(int begin); In the string , Substring starting with a subscript ( contain )

 

13  String substring(int begin, int end) :  In the string , Start with a subscript ( contain ) Substring ending at a subscript ( It doesn't contain )

 

 14 String trim() : Remove the spaces on both sides of the string

 

15  String toUpperCase() : Turn capitalization

      String toLowerCase() : Turn lowercase

 

16 static String valueOf(Object obj) : Calling the toString Method , If null, No more calls toString Instead, it returns a string null,  When printing a reference type , Automatically called String Of valueOf therefore Automatically called toString Method

 1.5 Be careful

2. StringBuffer and StringBuilder

2.1 summary

StringBuffer and StringBuilder: Are all string buffers , It can be used for splicing  

StringBuffer、StringBuilder and String The difference between :

                String : The bottom is char Array , Fixed length , Once determined, it cannot be changed , Not suitable for string splicing

                StringBuffer and StringBuilder : The bottom is char Array , Lengthening , Apply for a piece of space in memory in advance , Used to save many characters if the reserved space is insufficient , It will automatically expand . Default capacity is 16, Expand capacity ( Current capacity +1)*2   16->34->70

 StringBuilder and StringBuffer The difference between : 

StringBuffer Thread safety , In multithreaded environment , No problem

StringBuilder Non-thread safety , Problems may occur in a multithreaded environment

 2.2 Use

3. Packaging  

3.1 summary

1. Boxing is the automatic conversion of basic data types to wrapper types  

2. Unpacking is automatically converting the wrapper type to the basic data type

3.2 Use

Basic types   byte b1=12

Encapsulate as wrapper class   Byte b2=new Byte(b1);

3.3 Integer

3.4 transformation

 

 

3.5 summary

 4. System

System Class provides the public static long currentTimeMillis() Used to return the current time Between and 1970 year 1 month 1 Japan 0 when 0 branch 0 The time difference in milliseconds between seconds

This method is suitable for calculating time difference .

System Class represents system , Many system level properties and control methods are placed inside the class . This class is located in java.lang package

Because the constructor of this class is private Of , So we can't create objects of this class , That is, it can't be realized Instantiate this class . Its internal member variables and member methods are static Of , So it can also be very convenient To call .

Common methods :

 5. Date

5.1 summary

  Get time and time operations

5.2 usage

 

 

 
6. Random  

Use

 

 

原网站

版权声明
本文为[grp_ grp_ grp]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190533360367.html