StringBuffer
1、String类的特点:
(1)String对象有两种实例化方式:
直接赋值:只开辟一块堆内存空间。可以自动入池;
构造方法:开辟两块堆内存空间,不会自动入池,可用intern()手工入池;
(2)字符串是String类的匿名对象;
(3)字符串一旦声明则不可更改,改变的是String对象的引用;
2、为解决String类的内容不可改变的问题,Java提供了StringBuffer
类(StringBuffer
对象的内容可以修改)。
String类使用+
进行字符串的连接,而StringBuffer
必须使用append()
进行连接操作:
append()
定义:public StringBuffer append(数据类型 变量);
范例:StringBuffer
使用
public class Demo {
public static void main(String[] args) {
// String类可以直接赋值,但是StringBuffer只能通过构造方法
StringBuffer buf = new StringBuffer();
buf.append("hello").append(" World ").append("!!");
change(buf); // 引用传递
System.out.println(buf);
// 结果:
// hello World !!
// Hello
}
public static void change(StringBuffer temp) {
temp.append("\n").append("Hello");
}
}
由上述代码可以发现StringBuffer
类的内容可以修改,而String的内容不可以修改。
3.、String
类与StringBuffer
类的定义
String类 | StringBuffer类 |
---|---|
public final class String extends Object implements Serializable, Comparable<String>, CharSequence | public final class StringBuffer extends Object implements Serializable, CharSequence |
由上表得知,String
和StringBuffer
都是CharSequence
接口的子类。因此在开发中,如果某些方法的操作使用CharSequence
接口,那么只要传递字符串即可。
public class Demo {
public static void main(String[] args) {
CharSequence seq = "hello"; // 向上转型
System.out.println(seq); // 调用String类覆写的toString()
}
}
4、虽然String和StringBuffer
类有着共同的接口,但是这两个类的对象之间不能直接转换。
(1)将String变为StringBuffer
对象有两种方式:
方式一:利用StringBuffer
构造方法:public StringBuffer(String str);
public class Demo {
public static void main(String[] args) {
// 将Hello由String型变为StringBuffer型
StringBuffer buf = new StringBuffer("Hello");
System.out.println(buf);
}
}
方式二:利用append()
:public StringBuffer append(String str)
;
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
// 将Hello由String型变为StringBuffer型
buf.append("Hello");
System.out.println(buf);
}
}
(2)将StringBuffer
变为String类对象
方式一: 利用toString()
可以将StringBuffer
转换为String;
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello");
String str = buf.toString();
System.out.println(str);
}
}
方式二:利用String类的构造方法:public String(StringBuffer buffer);
5、String类中提供有与StringBuffer
对象比较的方法:public boolean contentEquals(StringBuffer sb)
范例:比较String
与StringBuffer
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello");
System.out.println("hello".contentEquals(buf)); // false
System.out.println("Hello".contentEquals(buf)); // true
}
}
6、String类中定义了许多方法,便于用户开发;而StringBuffer
中也定义了许多方法,而且部分方法与String类正好互补。
(1)字符串反转:public StringBuffer reverse();
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello");
System.out.println(buf.reverse()); // olleH
}
}
(2)在指定位置插入数据:public StringBuffer insert(intoffset,数据类型 变量);
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello");
buf.insert(0, "MLDN").insert(0, "你好");
System.out.println(buf); // 你好MLDNHello
// 可以插入任意数据类型的数据
buf.insert(0,11);
System.out.println(buf); // 11你好MLDNHello
}
}
(3)删除部分数据:public StringBuffer delete(int start,int end);
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello");
buf.delete(2,4);
System.out.println(buf); // Heo
}
}
7、JDK1.5新增了一个字符串操作类:StringBuilder
,定义结构如下:
public final class StringBuilderextends Object implements Serializable,CharSequence
StringBuffer
和StringBuffer
类在定义上非常相似,并且方法也几乎一样。
小结:String、
StringBuffer
和StringBuilder
的区别?String的内容一旦声明则不可改变,而
StringBuffer
和StringBuilder
声明的内容可以改变;
StringBuffer
类中提供的方法都是同步方法,属于安全的线程操作;而StringBuilder
提供的方法都是非同步方法,属于不安全的线程操作。开发中使用String类,只有要频繁修改内容时,才考虑使用
StringBuffer
或StringBuilder
。
Runtime类(了解即可)
每个JVM进程中都存在一个Runtime对象,这个类的主要功能是取得一些与运行时有关的环境属性或者进行线程创建。
1、Runtime类定义:public class Runtime extends Object
,其构造方法私有化,这是单例设计模式的应用,为了保证整个进程中只有唯一的Runtime对象。因此Runtime提供了一个static方法,用于取得Runtime的实例化对象:public static Runtime getRuntime();
2、Runtime是与本地运行有关的所有属性的集合,Runtime中定义有如下方法:
(1) 返回所有可用内存空间:public long totalMemory();
(2) 返回最大可用内存空间:public long maxMemory();
(3) 返回空余内存空间:public long freeMemory();
public class Demo {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime(); // 取得Runtime类实例化对象
System.out.println(run.totalMemory()); // 128974848
System.out.println(run.maxMemory()); // 1884815360
System.out.println(run.freeMemory()); // 125561416
String str = "";
for (int x = 0; x < 1000; x++) {
str += x; // 产生大量垃圾
}
System.out.println(run.totalMemory()); // 526909440
System.out.println(run.maxMemory()); // 1884815360
System.out.println(run.freeMemory()); // 377484040
}
}
由上述结果发现:一旦产生过多的垃圾,就会改变可用内存空间的大小。Runtime中有一个方法可以释放垃圾空间:public void gc();
public class Demo {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime(); // 取得Runtime类实例化对象
System.out.println(run.maxMemory()); // 1884815360
System.out.println(run.totalMemory()); // 128974848
System.out.println(run.freeMemory()); // 125561416
String str = "";
for (int x = 0; x < 2000; x++) {
str += x; // 产生大量垃圾
}
System.out.println(run.maxMemory()); // 1884815360
System.out.println(run.totalMemory()); // 128974848
System.out.println(run.freeMemory()); // 107935784
run.gc(); // 释放垃圾空间
System.out.println(run.maxMemory()); // 1884815360
System.out.println(run.totalMemory()); // 128974848
System.out.println(run.freeMemory()); // 126933104
}
}
GC(Garbage Collector)垃圾收集器,用于释放无用的内存空间;
GC会由系统不定期进行自动的回收,或者调用Runtime类中的gc()手工回收。
3、Runtime类中有一个方法可以调用本机的可执行程序,并且创建进程。
public Process exec(String command) throws IOException;
范例:执行程序
public class Demo {
public static void main(String[] args) throws Exception{
Runtime run = Runtime.getRuntime(); // 取得Runtime类实例化对象
Process pro = run.exec("notepad.exe"); // 调用本机程序
Thread.sleep(2000);
pro.destroy(); // 销毁进程
}
}
System类(了解即可)
1、可用System.arraycopy()
实现数组拷贝,该方法的定义如下:
public static void arraycopy(Object src, int srcPos, Object dest,int destPos, int length)
2、 在System类中定义有一个方法,用于取得当前系统时间,其定义如下:public static long currentTimeMillis();
范例:统计某项操作的执行时间
public class Demo {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis(); // 取得开始时间
String str = "";
for (int x = 0; x < 3000; x++) {
str += x;
}
long end = System.currentTimeMillis(); // 取得结束时间
System.out.println("循环所使用的时间:" + (end - start) + " ms");
}
}
要想统计出所花费的毫秒时间,用long型数据直接进行数学计算即可。
3、在System类中定义了一个方法:public static void gc()
,该方法是间接调用Runtime类中的gc()
。对象产生会调用类中的构造方法,可以进行一些处理操作。但是对象被回收时,却什么都做不了。
class Member {
public Member() {
System.out.println("对象产生了!");
}
}
public class Demo {
public static void main(String[] args) {
Member member = new Member(); // 对象产生时可以进行一些操作
member = null; // 对象被回收时,无法进行其他操作
}
}
如果要在销毁对象时,给对象增添一些操作,可以覆写Object类中的finalize()
其定义:protected void finalize() throws Throwable
。对象回收时,就算抛出了异常,也不会影响程序的正常执行。
范例:对象收尾
class Member{
public Member() {
System.out.println("对象诞生了");
}
@Override
protected void finalize() throws Throwable {
System.out.println("对象被销毁了");
throw new Exception("对象还会再产生的");
}
}
public class Demo {
public static void main(String[] args) throws Exception {
Member member = new Member();
member = null;
System.gc(); // 手工处理垃圾收集
}
}
构造方法是对象初始化时调用的,而finalize()是回收对象前调用的。
final、finally和finalize的区别
final:是一个关键字,用于定义不能被继承的类,不能被覆写的方法和常量;
finally:是一个关键字,异常的统一出口;
finalize:是一个Object类的方法(
protected void finalize() throws Throwable
),是对象回收前调用的方法,即使出现异常也不会导致程序中断。
对象克隆
在Object类中提供有对象克隆的方法:protected Object clone() throws CloneNotSupportedException
.该方法抛出一个CloneNotSupportedException
不支持克隆异常,当要克隆的类没有实现Cloneable
接口,就会出现该异常。Cloneable
接口是标识接口,表示一种操作能力。
范例:实现对象克隆
class Book implements Cloneable { // 此类对象可以被克隆
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "书名:" + this.title + ",价格:" + this.price;
}
// 由于此类需要对象克隆操作,所以需要进行方法覆写
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // 覆写后调用父类的克隆方法
}
}
public class Demo {
public static void main(String[] args) throws Exception {
Book bookA = new Book("Java开发", 79.8);
Book bookB = (Book) bookA.clone();
System.out.println(bookA);
System.out.println(bookB);
// 该步操作,是为了证明克隆和引用的不同
bookB.setTitle("JSP开发");
System.out.println(bookB);
}
}