Commonly used base class library

Object class is the base class of all classes

protected Object clone() //返回一个对象副本,浅复制

boolean equals(Object obj) //判断对象的地址是否相等

protected void finalize() //调用析构函数

Class<?> getClass() //返回当前类对象

int hashCode(); //返回该对象的哈希值

void notify() //通知在此监视器上的等待线程,进入

void notifyAlll() //通知在此监视器上的所有等待线程

String toString() //没有重写,返回完整的类名,

void wait() //等待线程 释放锁

void wait(long timeout) //等待线程 释放锁

Difference between equals and "=="

  • equals is the method of the object == is the operator
  • equals cannot compare basic types, it is the address of the comparison object == can compare basic types and objects
  • equals The default is to compare whether the address of the object is equal (can be rewritten to compare whether it is an object of the same class) == The basic type of comparison is whether the comparison value is equal, and the comparison object is whether the address of the comparison object is equal

Basic data type and its wrapper class

int --> Integer

char --> Character

Common methods of the Integer class

// 返回对应的封装值
public long longValue();

public double doubleValue();

public int intValue();  
//end 返回对应的封装值

//解析字符串,返回int
public static int parseInt(String s) throws NumberFormatException

//返回Integer对象
public static Integer valueOf(String s) throws NumberFormatException

Boxing: that is, the basic type becomes a reference type

Unboxing: reference types become primitive types

String class

  • String class is an immutable type, final modification
  • Common method
    ````
    char charAt(int index) //return the char character at the index

String concat(String str) //Concatenate characters at the end

boolean isEmpty() //whether the string is empty

int length() //return character length

String toLowerCase() //return lowercase characters

String toUpperCase() //return uppercase characters
````

  • StringBuffer
    • Compared with String, it is more suitable for use when frequent insertion and deletion of strings is required
    • It will modify itself every time, and string processing will not generate a new object
    • is thread safe
    • There is no inheritance relationship with String and cannot be cast

Thread safety means that when data access is synchronized, it does not support multiple threads accessing the same variable at the same time, avoiding data pollution caused by modifications between different threads

StringBuffer sb = new StringBuffer();

StringBuffer sb = new StringBuffer("字符串");

//转换成String
String s = sb.toString();

//常用方法
append(字符串); //在末尾追加字符串

deleteCharAt(int index); //删除指定位置的字符串

delete(int start,int end) //删除指定区间内的所有字符

insert() //在指定位置插入值

reverse() //反转字符串

setCharAt() //设置指定位置的字符值
  • StringBuilder
    • Compatible with StringBuffer, but not guaranteed to be synchronized
    • faster than Stringbuffer
    • not thread safe

      Usage reference StringBuffer

    • main append insert

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681071&siteId=291194637