当前位置:网站首页>Common APIs
Common APIs
2022-07-03 07:06:00 【L gold p】
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 Can't change
Once the string is created , This string object cannot be changed
To improve string access and storage efficiency Java The virtual machine uses a caching mechanism , Save the strings to the string constant pool
In the process of execution , If you want to use a string a,String s1="a" First, search the character constant pool Is there a a, If not, create one If there is Sting s2="a"; No more creation , Take the existing one a return
So lead to String s1="a" String s2="a" here s1==s2 He is also true Because they point to the same string object Namely a
1、2 Basic use
1、3 Do not splice frequently
1、4 Construction method
1、5 Common methods
Study API
1 What is the function
2 What are input and output parameters
3 How to use it?
// 1 char charAt (int index ) : Returns... In the string , The character in the specified position
String s1 = "qwehihasd";
char c1 = s1.charAt(2);
// e
System.out.println(c1);
// 2 boolean endsWith(String suffix) : Determines whether the string ends with the specified string
// boolean startsWith(String prefix) : ditto , Judgment begins
System.out.println("Hello.java".endsWith(".java"));
// Note blank space , If there is a space, it won't match
System.out.println("Hello.java".endsWith(".java "));
// 3 boolean equalsIgnoreCase(String str) : Case insensitive comparison of two strings for equality
System.out.println("abc".equalsIgnoreCase("aBc"));
// 4 byte[] getBytes() : Converts a string to a byte array and returns
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
// 5 char[] toCharArray() : Converts a string to a character array and returns
char[] chars = "abc".toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
// 6 int indexOf(String str) : Gets the starting index of the specified string in the string , No return found -1
System.out.println("askdhqwbe".indexOf("kd")); // 2
System.out.println("askdhqwbe".indexOf("kda")); // -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
System.out.println("askdhaqwbe".indexOf("a", 5)); // -1
// 8 index lastIndexOf(String str) : ditto , The last index to appear No return found -1
System.out.println("askdhaqwbe".lastIndexOf("a")); // 5
// 9 int length() : Returns the length of the string
System.out.println("abc".length());
// 10 String replaceAll(String regex . String replacement);
// Replace specified characters , regular expression
// String replace (String str . String replacement); Regular expressions are not supported
// use 1 hold a Replaced and returned a new string
// regular expression , But I didn't write regular expressions There is no difference
System.out.println("hucgasdqweasd".replaceAll("a", "1"));
System.out.println("hucgasdqweasd".replace("a", "1"));
// because . In regular expressions , Arbitrary character
System.out.println("qwe.rty.yui.uio".replaceAll(".", ","));
// have access to \ escape
System.out.println("qwe.rty.yui.uio".replaceAll("\\.", ","));
System.out.println("qwe.rty.yui.uio".replace(".", ","));
// 11 String[] split(String regex) : Split string , Return string array , regular expression , Be careful .....
// spot , Need to escape
String s2 = "2022.1.14";
String[] arr = s2.split("\\.");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// 12 String substring(int begin); In the string , Substring starting with a subscript ( contain )
System.out.println("abcdef".substring(2));// cdef
// 13 String substring(int begin, int end) :
// In the string , Start with a subscript ( contain ) Substring ending at a subscript ( It doesn't contain )
System.out.println("abcdef".substring(2, 4));// cd
// 14 String trim() : Remove the spaces on both sides of the string
System.out
.println(" a d sadasd ");
System.out
.println(" a d sadasd "
.trim());
// 15 String toUpperCase() : Turn capitalization
// String toLowerCase() : Turn lowercase
System.out.println("asd".toUpperCase());
// 16 static String valueOf(Object obj) :
// Calling the toString Method , If null, No more calls toString Instead, it returns a string null
String_05 s = null;
// When printing a reference type , Automatically called String Of valueOf therefore Automatically called toString Method
System.out.println(s);
1、6 Be careful
2、StringBuffer and StringBuilder
2、1 summary
2、2 Basic use
2、3 Common methods
3、 Packaging
3、1 summary
【 ask 】 Want to do more with basic type data , What do I do ?
【 answer 】 The most convenient way is to encapsulate it as an object . Because more properties and behaviors can be defined in the object description to operate on the basic data type . We don't need to encapsulate basic types by ourselves ,JDK It has been encapsulated for us .
【 Concept 】
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
3.3 Integer
3.3.1 Basic use
3.3.2 transformation
3.3.3 Common methods
3.3.4 Automatic packing and unpacking
3.3.5、 Deep shaping constant pool
Judge the current value Whether in -128~127 Between
If it's not in range Just directly new object
3.3.6 summary
4.System
4.1 summary
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 .
Member variables
System Class contains in、out and err Three member variables , They represent the standard input stream
( Keyboard entry ), Standard output stream ( Monitor ) And standard error output stream ( Monitor ).
Member method
native long currentTimeMillis():
The function of this method is to return the current computer time , Time is expressed in the current computer
Between and GMT Time ( Greenwich mean time )1970 year 1 month 1 Number 0 when 0 branch 0 The number of milliseconds in seconds .
void exit(int status):
The function of this method is to exit the program . among status The value of is 0 It means normal exit , Non zero means
Abnormal exit . Using this method, the exit function of program can be realized in GUI programming .
4.2 Common methods
5.Date
5.1 summary
Get time and time operations
5.2 Construction method
5.3 Common methods
5.4 format
5.5 Calendar
6. Random
6.1 summary Random number from 0 Start
6.2 Use
边栏推荐
- Sorting out the core ideas of the pyramid principle
- 691. 立方体IV
- centos php7.3安装redis扩展
- Architecture notes
- Basic components and intermediate components
- JUC forkjoinpool branch merge framework - work theft
- Asynchronous programming: async/await in asp Net
- PAT甲级真题1166
- Win 10 find the port and close the port
- EasyExcel
猜你喜欢
VMware virtual machine C disk expansion
JMeter test result output
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
每日刷题记录 (十一)
机器学习 | 简单但是能提升模型效果的特征标准化方法(RobustScaler、MinMaxScaler、StandardScaler 比较和解析)
Practical plug-ins in idea
Personally design a highly concurrent seckill system
Jenkins
【已解决】Unknown error 1146
New knowledge! The virtual machine network card causes your DNS resolution to slow down
随机推荐
Advanced API (local simulation download file)
Software testing learning - day one
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
MySQL mistakenly deleted the root account and failed to log in
On the practice of performance optimization and stability guarantee
Use of framework
Software testing assignment - the next day
Realize PDF to picture conversion with C #
【已解决】Unknown error 1146
Getting started with pytest
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
MySQL transaction rollback, error points record
服务器如何设置多界面和装IIS呢?甜甜给你解答!
Abstract learning
How to specify the execution order for multiple global exception handling classes
"Baidu Cup" CTF game 2017 February, Web: blast-1
Advanced API (batch image Download & socket dialog)
Thoughts in Starbucks
Upgrade CentOS php7.2.24 to php7.3
卡特兰数(Catalan)的应用场景