您的位置:首页 > 编程语言 > Java开发

java中常用的类库学习(1)

2015-11-05 13:42 309 查看
首先是StringBuffer类,该类是String类很类似,也增加了一些新的方法,

比如说reveser方法、字符串连接是用append方法,要转化为普通的String类型可以用toString()方法

看看具体的实现吧:

package excise;

public class StringBufferDemo {
public static void main(String args[]){
StringBuffer buf=new StringBuffer();
buf.append("Hello ");
buf.append("world");
buf.append("数字=").append("1");
System.out.println(buf);
System.out.println(buf.reverse());
System.out.println(buf.insert(1, "wwwwwss"));
System.out.println(buf.indexOf("ss"));
System.out.println(buf.replace(1, 2, "zhoumeixu"));
System.out.println(buf.replace(1, 2, "zhoumeixu"));
System.out.println(buf.length());
String str=buf.toString();
System.out.println("String类");
System.out.println(str);
}

}


接下来是Runtime类,Runtime类表示运行时的操作类,是一个封装了jvm进程类,每一个jvm都有一个Runtime类的实例,

此时由jvm运行为其实例化,Runtime类构造方法是私有化的,也就是单例设计的,

package excise;

public class RuntimeDemo {
public static void main(String args[])throws Exception{
Runtime run=Runtime.getRuntime();
System.out.println(run.freeMemory());//返回java虚拟机中空闲内存量
System.out.println(run.maxMemory());//返回jvm的最大内存量
run.gc();//运行垃圾回收器,释放空间
Process p=run.exec("shutdown -s -t 7200");
}

}


System类,这个类应该不陌生,学java的人用的最多,该类中都是静态方法,可以直接用类.方法调用:

package excise;
import java.util.Properties;
public class Test {
public static void main(String args[])throws Exception{
System.gc();//允许垃圾回收机制
System.out.println(System.currentTimeMillis());//返回以毫秒为单位的当前时间
System.out.println(System.getProperties()); //取得当前全部属性
Properties p=System.getProperties();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: