1.StringBuffer类
No | 方法 | 类型 | 描述 |
1 | public Stringbuffer() | 构造 | 创建一个空的StringBuffer对象 |
2 | public StringBuffer(String str) | 构造 | 将接收到的String内容转换为StringBuffer内容 |
3 | public StringBuffer append(数据类型 变量) | 普通 | 内容连接,等价于String中的"+"操作 |
4 | public StringBuffer insert(int offset,数据类型 变量) | 普通 | 在指定索引位置处插入数据 |
5 | public StringBuffer delete(int start , int end) | 普通 | 删除指定索引范围之内的内容 |
6 | public StringBuffer reserve() | 普通 | 内容反转 |
StringBuilder类与StringBuffer类的功能相似,只是StringBuffer类中的方法添加了synchronized关键字,适合于多线程并发访问下的同步处理,而StringBuilder类中的方法没有使用synchronized关键字定义,属于非线程安全的方法。
2.CharSequence接口
No | 方法 | 类型 | 描述 |
1 | public char charAt(int index) | 普通 | 获取指定索引的字符 |
2 | public int length() | 普通 | 返回字符串的长度 |
3 | public charSequence(int start,int end) | 普通 | 截取部分字符串 |
3.AutoCloseable接口
该接口的主要功能是结合异常处理结构在资源操作完成后实现自动释放功能,该接口的定义如下:
public interface AutoCloseable{
public void close() throws Exception;
}
实例:简单的消息发送与连接关闭的操作
package fifteen;
public class JavaAPIDemo1 {
public static void main(String[] args) throws Exception{
try(IMessage nm = new NetMessage("java")){ //建立连接
nm.send(); //发送消息
}catch(Exception e) { //捕获异常
e.printStackTrace();
}
}
}
interface IMessage extends AutoCloseable{
public void send(); //继承自动关闭接口
}
class NetMessage implements IMessage{
private String msg;
public NetMessage(String msg) {
this.msg = msg;
}
public boolean open() {
System.out.println("[Open]");
return true;
}
@Override
public void send() {
if(this.open()) {
if(this.msg.contains("java")) {
throw new RuntimeException("Hello,java");
}
System.out.println("send message"+this.msg);
}
}
public void close() throws Exception{
System.out.println("close!");
}
}
不管是否抛出异常,都会自动关闭资源,程序输出结果如下:
4.Runtime类
Runtime类描述的是运行时状态,在每一个JVM进程中都会提供唯一的一个Runtime类实例化对象,开发资可以通过Rumtime对象获取与JVM有关的运行时状态。
Runtime类只有一个实例化对象,所以默认其构造方法封装,只能利用getRuntime()来获取实例化对象。
No | 方法 | 类型 | 描述 |
1 | public Runtime getRuntime() | 普通 | 取得Runtime类的实例化对象 |
2 | public int availableProcessors() | 普通 | 获取可用的CPU处理器数量 |
3 | public long maxMemory() | 普通 | 取得最大可用内存量 |
4 | public long totalMemory() | 普通 | 取得总共可用内存量 |
5 | public long freeMemory() | 普通 | 取得空闲内存量 |
6 | public void gc() | 普通 | 运行垃圾收集器,释放垃圾空间。 |
package fifteen;
public class JavaAPIDemo2 {
public static void main(String [] args) throws Exception{
Runtime runtime = Runtime.getRuntime();
System.out.println("[1]TOTAL_Memory" + runtime.totalMemory());//获取可用的内存空间
System.out.println("[1]Free_Memory" + runtime.freeMemory());
String str = "";
for(int x = 0; x < 3000; x++) {
str += x;
}
System.out.println("[2]TOTAL_Memory" + runtime.totalMemory());
System.out.println("[2]Free_Memory" + runtime.freeMemory());
runtime.gc(); //执行gc操作,释放内存
System.out.println("[3]TOTAL_Memory" + runtime.totalMemory());
System.out.println("[3]Free_Memory" + runtime.freeMemory());
}
}
5.System类
System类是一个系统类,主要的功能是进行信息的打印输出,除此之外还有以下的方法
No | 方法 | 类型 | 描述 |
1 | public static void arraycopy(Object src, int srcPos,Object dest,int length) | 普通 | 数组复制操作 |
2 | public static long currentTimeMillis() | 普通 | 取得当前的日期时间,以long型数据返回 |
3 | public static void gc() | 普通 | 执行GC操作 |
开发过程中currentTimeMillis()主要用于获取当前的时间戳。
6.对象克隆
使用Object的clone()方法,利用已有的堆内存空间的保存内容实现数据的完整复制。
克隆方法:protected Object clone() throws CloneNotSupportedException
要克隆的对象类必须实现Cloneable接口,此接口属于标识接口,没有任何方法
package fifteen;
public class JavaAPIDemo3 {
public static void main(String[] args) throws Exception {
Member memberA = new Member("abc",20);
Member memberB = (Member)memberA.clone();
memberB.setName("Raindrop");
System.out.println(memberA);
System.out.println(memberB);
}
}
class Member implements Cloneable{
private String name;
private int age;
public Member(String name, int age) {
this.setName(name);
this.setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "[" + super.toString()+"] name = "+ this.name +"、age ="+this.age;
}
@Override
public Object clone() throws CloneNotSupportedException{
return super.clone(); //覆写Object中的clone()方法
}
}
克隆对象只是复制了另一个对象的内容并没有实现同一个引用。
7.关于日期
Java想要获取当前的日期时间可用通过java.util.Date类来实现
No | 方法 | 类型 | 描述 |
1 | public Date() | 构造 | 实例化Date类对象 |
2 | public Date(long date) | 普通 | 将数字变为Date类对象,long为日期时间数据 |
3 | public long getTime() | 普通 | 将当前的日期转换long型 |
package fifteen;
import java.util.Date;
public class JavaAPIDemo4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date = new Date();
System.out.println(date);
long time = date.getTime(); //将Date类转换为long数字
time += 864000*1000; //加上10天秒数
System.out.println(new Date(time));
}
}
日期的格式化:SimpleDateFormat日期格式化
No | 方法 | 类型 | 描述 |
1 | public SimpleDateFormat(String pattern) | 构造 | 传入日期时间标记实例化对象 |
2 | public final String format(Date date) | 普通 | 将日期格式化为字符串数据 |
3 | public Date parse(String source) throws ParseException | 普通 | 将字符串格式化为日期数据 |
在日期格式化中必须设置有完整的日期转换模板,模板中通过特定的日期标记可以将一个日期格式中的日期数字提取出来,。
y、M、d、H、m、s、S:这几个字母依次对应的是年(四位)、月(两位)、天(两位)、小时(两位)、分钟(两位)、秒(两位)、毫秒(三位)
example1:将日期转化为字符串
package fifteen;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaAPIDemo5 {
public static void main(String[] args) {
Date date =new Date();
SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String time = sDate.format(date);
System.out.println(time);
}
}
example2:将字符串转化为日期
package fifteen;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaAPIDemo5 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String str = "2020-03-14 20:38:07:047";
Date date = sDate.parse(str);
System.out.println(date);
}
}
8.Arrays数组操作类
java.util.Arrays是一个专门实现数组操作的工具类,其常用的数组操作如下
No | 方法 | 类型 | 描述 |
1 | public static void sort(type[] var) | 普通 | 数组排序 |
2 | public static int binarySearch(type[] var,type key) | 普通 | 利用二分查找算法进行数据查找 |
3 | public static int compare(type[] var, type [] var) | 普通 | 比较两个数组的大小,返回3类结果,大于(1),小于(-1),等于(0) |
4 | public static boolean equals(type[] var , type [] var) | 普通 | 数组相等判断 |
5 | public static void fill(type[] var ,type var) | 普通 | 数组填充 |
6 | public static String toString(type[] var) | 普通 | 数组转为字符串 |