Java的常用类库 Runtime System Math Random Arrays Colnealble

Java的常用类库

一、StringBuffer可以改变字符串

 

二、Runtime类:每个Java应用程序都有一个Runtime类实例,是应用程序能够与其运行的环境相连接。可以通过getRuntime方法获取当前运行时。

·通过public staticRuntime getRuntime();获取Runtime的实例。

1、  Runtime的应用:

·通过public Process exec(String command)throws IOException
        运行电脑系统指定的应用程序
·获取系统的信息:public long maxMemory()、public long freeMemory()、public long totalMemory();
·通过public void gc()运行垃圾回收器

public static void main(String[] args) {

       Runtime run = Runtime.getRuntime();

       System.out.println("系统的最大内存"+run.maxMemory());

       System.out.println("系统的空先内存"+run.freeMemory());

       System.out.println("系统的全部内存"+run.totalMemory());

       String str="";

       for(int i=0;i<10000;i++){

           str+=i;

       }

       System.out.println("释放前的空闲内存"+run.freeMemory());

       run.gc();

       System.out.println("释放后的空闲内存"+run.freeMemory());

        }

系统的最大内存66650112

系统的空先内存4934792

系统的全部内存5177344

释放前的空闲内存4567264

释放后的空闲内存4956160

 

三、System类

·public static long currentTimeMillis()//返回以毫秒为单位的当前时间
·public static void gc();//调用 System.gc() 实际上等效于调用:Runtime.getRuntime().gc();
   ·在调用gc()进行垃圾回收前,会先调用Object中的protected void finalize()throws Throwable//进行回收前的工作

package com.jtlyuan;

class Person{

    String name;

    int age;

    public Person(String name, int age) {

       super();

       this.name = name;

       this.age = age;

    }

    protected void finalize() throws Throwable {

       super.finalize();

       System.out.println("我本回收了");

    }

}

public class SystemTest {

    public static void main(String[] args) {

       Person p = new Person("梁任元",24);

       System.gc();

    }

}

 
   
 
四、                  Math类:所有方法都是静态方法,提过了各种数学运算如:指数、对数、平方根和三角函数,round(数字)进行四舍五入
 
五、                  Random类:生成随机数

 

方法
描述
public Random()
实例化对象
public int nextInt(int n)
随机返回[0,n]
 
六、                  Arrays类:一个专门操作数组的类
NO
方法
描述
1
public static int binarySearch(int[] a,
byte key)
进行二分查找
2
public static void sort(int[] a)
进行升序排列
3
 
 
 

七、克隆:就是复制,把一个对象全部内容完整复制下来。

只有实现了Cloneable接口的类的对象才能克隆。

package com.jtlyuan;

class Person implements Cloneable{

    String name;

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    @Override

    protected Object clone() throws CloneNotSupportedException {

       return super.clone();

    }

    public Person(String name) {

       super();

       this.name = name;

    }

}

public class CloneTest {

    public static void main(String[] args) throws CloneNotSupportedException {

       Person p1 = new Person("jtlyuan");

       Person p2 = (Person)p1.clone();

       System.out.println(p2.getName());

       p2.setName("梁任元");

       System.out.println(p2.getName());

    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值