第7 章 : Java基礎類庫
26 StringBuffer類
String有兩個常量池:
靜态常量池,運作時常量池
String對象執行個體化直接指派形式,可以儲存到常量池中以便重用
// 構造方法
public StringBuffer(String str)
// 追加
public synchronized StringBuffer append(String str)
// 插入
public synchronized StringBuffer insert(int offset, String str)
// 删除指定範圍資料
public synchronized StringBuffer delete(int start, int end)
// 字元串内容反轉
public synchronized StringBuffer reverse()
代碼示例
class Demo{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("hello");
sb.append(" world");
System.out.println(sb.toString());
// hello world
}
}
String 轉換為 StringBuffer 使用構造方法
StringBuffer 轉換為 String 使用toString
操作示例
class Demo{
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("hello");
// 可以連續操作
sb.append(" ").append("world");
System.out.println(sb.toString());
// hello world
// 插入
sb.insert(5, " Java");
System.out.println(sb);
// hello Java world
// 删除
sb.delete(5, 10);
System.out.println(sb);
// hello world
// 反轉
sb.reverse();
System.out.println(sb);
// dlrow olleh
}
}
類似功能類 StringBuilder
JDK >= 1.5
差別
String 字元串,内容不可修改
StringBuffer JDK>=1.0 線程安全, 使用了synchronized
StringBuilder JDK>=1.5 非線程安全
27 CharSequence接口
CharSequence描述字元串結構的接口
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
CharSequence接口方法
public interface CharSequence{
int length();
char charAt(int index);
CharSequence subSequence(int start, int end);
public String toString();
}
28 AutoCloseable接口
AutoCloseable接口 用于資源的自動關閉
JDK >= 1.7
public interface AutoCloseable {
void close() throws Exception;
}
不使用自動關閉代碼示例
interface IMessage{
public void send();
}
class MessageImpl implements IMessage{
private String message;
public MessageImpl(String message) {
this.message = message;
}
@Override
public void send() {
System.out.println("發送消息: " + this.message);
}
public boolean open(){
System.out.println("打開資源");
return true;
}
public void close(){
System.out.println("關閉資源");
}
}
class Demo{
public static void main(String[] args) {
MessageImpl message = new MessageImpl("消息内容");
if(message.open()){
message.send();
message.close();
}
/**
* 打開資源
* 發送消息: 消息内容
* 關閉資源
*/
}
}
結合異常處理語句實作資源自動關閉
interface IMessage extends AutoCloseable{
public void send();
}
class MessageImpl implements IMessage{
private String message;
public MessageImpl(String message) {
this.message = message;
}
@Override
public void send() {
System.out.println("發送消息: " + this.message);
}
public boolean open(){
System.out.println("打開資源");
return true;
}
public void close(){
System.out.println("關閉資源");
}
}
class Demo{
public static void main(String[] args) {
try(MessageImpl message = new MessageImpl("消息内容")){
if(message.open()){
message.send();
}
}catch(Exception e){
}
/**
* 打開資源
* 發送消息: 消息内容
* 關閉資源
*/
}
}
- 44
29 Runtime類
Runtime描述運作時狀态
一個JVM程序隻允許提供一個Runtime,使用了單例設計模式
可以使用靜态方法擷取執行個體化對象
public static Runtime getRuntime()
class Demo {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
// 讀取CPU核心數量
System.out.println(run.availableProcessors());
// 8
// 擷取最大可用記憶體空間 1/4
System.out.println(run.maxMemory());
// 擷取可用記憶體空間 1/64
System.out.println(run.totalMemory());
// 擷取空閑記憶體空間
System.out.println(run.freeMemory());
// 手工進行GC處理
run.gc();
}
}
GC (Garbage Collector) 垃圾收集器
由系統自動調用
或者使用Runtime類中的gc()方法,手工清除
30 System類
常用方法
// 數組拷貝
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
// 擷取時間數值
System.out.println(System.currentTimeMillis());
// 1573918888172
// 垃圾回收
public static void gc() {
Runtime.getRuntime().gc();
}
31 Cleaner類
JDK>=1.9
Cleaner類提供對象清理操作
替代finialize()方法
C++提供了構造函數,析構函數
Java垃圾使用GC回收
class Demo {
public Demo() {
System.out.println("構造函數");
}
@Override
protected void finalize() throws Throwable {
System.out.println("垃圾回收");
super.finalize();
}
public static void main(String[] args) {
Demo demo = new Demo();
demo = null;
System.gc();
/**
* 構造函數
* 垃圾回收
*/
}
}
不建議使用 finalize()方法, 使用AutoCloseable接口
使用Cleaner類,使用單獨的線程去回收資源,能提高整體性能
32 對象克隆
protected native Object clone() throws CloneNotSupportedException;
// 隻有接口名,沒有任何方法,隻是能力辨別接口
public interface Cloneable {
}
接口作用:
标準
能力
class Demo implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) {
Demo demo = new Demo();
Demo demo2 = null;
try {
demo2 = (Demo)demo.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(demo);
System.out.println(demo2);
/**
* Demo@2503dbd3
* Demo@4b67cf4d
*/
}
}