Java常用API之System、Math、Random

System

包含几个有用的类字段和方法。,它不能被实例化,没有构造方法。

    常用字段

err
“标准”错误输出流。
in“标准”输入流。
out“标准”输出流。

    常用方法

        currentTimeMillis()

以毫秒为单位返回当前时间。通常同于测试某段代码的运行时间

public class systemdemo {
	public static void main(String[] args) {
		long start = System.currentTimeMillis();// 计算开始时间
		int i = 1;
		while (i < 9999) {
			i++;
			System.out.println(i);
		}
		long end = System.currentTimeMillis();// 计算结束时间
		System.out.println("用时为:" + (end - start) + "毫秒");// 用时为:172毫秒
	}
}

        gc()

运行垃圾回收器,实质是执行了finalize()方法,让虚拟机尽可能的回收当前从来没有被使用的对象来释放空间。

        exit(int status)

参数作为状态代码,非零状态码表示异常终止。如果指定为0则关闭JVM虚拟机。

class Student {
	String name;
	int age;

	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	@Override
	public String toString() {
		return "[姓名=" + name + ", 年龄=" + age + "]";
	}

}

public class SystemDemo {
	public static void main(String[] args) {
		Student s = new Student("井杰", 21);
		System.out.println(s);// [姓名=井杰, 年龄=21]
		s = null;// 让对象不再指向堆内存
		System.gc();// 运行垃圾回收器
		System.out.println(s);// null

		System.out.println("java");// java
		System.exit(0);// 退出JVM
		System.out.println("word");// 没有输出,Java虚拟机已经退出,后面的代码就不执行了

	}
}

        arraycopy(Object src,int srcPos, Object dest,int destPos, int length)

从指定源数组的指定位置开始复制指定长度的数组到目标数组的指定位置替换掉目标数组的内容。

Object src:源数组

int srcPos :源数组中复制的起始位置

Object dest:目标数组

int destPos:目标数组开始被替换的起始位置

int length:复制的长度

import java.util.Arrays;

public class SystemDemo {
	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int[] arr2 = { 11, 12, 13, 14, 15, 16, 17, 18, 19 };
		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.toString(arr2));
		System.arraycopy(arr, 1, arr2, 2, 3);
		System.out.println("------------------------");
		System.out.println(Arrays.toString(arr));
		System.out.println(Arrays.toString(arr2));

	}
}
/***
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[11, 12, 13, 14, 15, 16, 17, 18, 19]
------------------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[11, 12, 2, 3, 4, 16, 17, 18, 19]

 */

Math类

包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数。

Math类中还为我们提供了一些常用值:

E 自然对数的基数。
PI圆周长与其直径的比率。

    常用方法

        abs(int a)

绝对值

        ceil(double a)

向上取整

        floor(double a)

向下取整

        max(int a,int b)

求最大值

        min(int a,int b)

求最小值

        pow(double a,double b)

a的b次幂

        random()

返回带正号的随机double 值,该值大于等于 0.0 且小于 1.0

import java.util.Scanner;

public class Text {

	public static void main(String[] args) {
		int i=(int)(Math.random()*100+1);//生成一个1到100的随机数
		Scanner sc=new Scanner(System.in);
		System.out.println("请猜出当前随机的数是多少,随机数在1到100之间 ");
		while(true) {
			int x=sc.nextInt();
			if(x==i) {
				System.out.println("恭喜你猜对了");
				break;
			}else if(x>i){
				System.out.println("过大");
			}else {
				System.out.println("过小");
			}
		}
	}
}
/**
请猜出当前随机的数是多少,随机数在1到100之间 
10
过小
20
过小
30
过大
25
过小
28
恭喜你猜对了
 */

        round(float a)

四舍五入

        sqrt(double a)

一个数的正平方根

两个数互换的快捷办法

public class MethDeno {
	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		// 利用位运算符^的特点,一个数据被另一个数据位^两次,其值是它本身来快捷进行交换。
		System.out.println("a:" + a + ",b:" + b);// a:10,b:20
		a = a ^ b;
		b = a ^ b;// 此时相当于b = a ^ b ^ b;也就是=a
		a = a ^ b;// 此时相当于a = a ^ b ^ a;也就是=b
		System.out.println("a:" + a + ",b:" + b);// a:20,b:10
	}

}

Random类

获取伪随机数的类

    构造方法

        Random()

创建一个新的随机数对象。  

        Random(long seed)

使用一个种子数创建一个新的随机数对象。这个种子数对最终生成的随机数没有直接的影响。

    常用方法 

        nextInt()

获取一个随机数,它的范围是在int类型范围之内。

        nextInt(int n)

获取随机数,它的范围是在0到n之间,不包含n。

import java.util.Random;

public class RandomDemo {

	public static void main(String[] args) {

		// 创建Random类对象
		Random r = new Random();
		for (int x = 0; x < 5; x++) {
			System.out.print(r.nextInt() + "  ");
		}
		// 830935364 -985538965 -619964702 -646748403 338999265
		System.out.println();
		for (int x = 0; x < 5; x++) {
			System.out.print(r.nextInt(10) + "  ");
		}
		// 2 6 5 6 0
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值