Java语言-15:System类、Math类和Random类的常用方法

1、System类
package System;


import java.util.Arrays;


/*
 *常用的方法:
public static void gc()运行垃圾回收器。 
public static void exit(int status)终止当前正在运行的 Java 虚拟机。参数用作状态码;  一般情况,需要终止
Jvm,那么参数0
public static long currentTimeMillis()返回以毫秒为单位的当前时间

public static void arraycopy(Object src,int srcPos, Object dest,int destPos, int length)


从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
src:原数组
dest:目标数组
srcPos :从原数组的哪个位置开始
destPos:到目标数组的哪个位置结束
length:长度
 * */
public class System_some_method {


public static void main(String[] args) {
/*方法1:public static void gc()运行垃圾回收器。
* 该方法实际执行的是finalized的方法。
* 例如:若 a 为一对象,执行System.gc();
* 其作用是不给a对象指定堆内存,即在堆内存中释放出a对象所占的内存空间
*  */

//方法2:public static void exit(int status)终止当前正在运行的 Java 虚拟机。参数用作状态码;一般情况,需要终止,参数为0;
//程序表示:
// System.out.print("hello");
// System.exit(0); //Java虚拟机停止
// System.out.print("world"); 此事该三行程序输出为hello。world没有输出的原因是在执行world之前Java虚拟机JVM与已经停止了

//方法3:public static long currentTimeMillis()返回以毫秒为单位的当前时间
//程序表示:
//得到当前时间
long time = System.currentTimeMillis();
System.out.println(time); //此时输出结果为:1525253376676

//public static void arraycopy(Object src,int srcPos, Object dest,int destPos, int length)
//从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束
//程序表示:
//定义一个数组
int[]arr = {1,2,3,4,5,6,7,8,9,10}; //源数组
int[]arr1 = {11,12,13}; //目标数组
System.arraycopy(arr, 1, arr1, 1, 1);
//输出目标数组
System.out.println(Arrays.toString(arr1));//出现运行异常java.lang.ArrayIndexOutOfBoundsException

}


}
2、Math类
package MATH;
/*
 * Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。  
 *常用的方法:
 * public static int abs(int a):绝对值
 * public static double ceil(double a):向上取整
 * public static double floor(double a):向下取整
 * public static int max(int a,int b):求最大值
 * public static int min(int a,int b):求最小值
 * public static double pow(double a,double b):a的b次幂
 * public static double random()返回带正号的 double 值,该值大于等于 0.0 且小于 1.0
 * public static int round(float a):四射五入
 * public static double sqrt(double a):一个数的正平方根
 *
 *JDK5的特性:静态导入(导入方法的级别)
 * */
public class math_method {


public static void main(String[] args) {
//方法1:public static int abs(int a):绝对值
int i = Math.abs(-1);
System.out.println(i);
//上面两行程序可简化为
System.out.println(Math.abs(-1));//输出为1

//方法2:public static double ceil(double a):向上取整
System.out.println(Math.ceil(2.3));//输出为3.0

//方法3:public static double floor(double a):向下取整
System.out.println(Math.floor(2.9));//输出为2.0

//方法4:public static int max(int a,int b):求最大值
System.out.println(Math.max(2, 5));//输出为5
//注意:该方法可嵌套,例如求三个数的最大值,
System.out.println(Math.max(Math.max(3, 5),4));//输出为5,如果有更多的数该方法同样适用,但更为简便的求多个数的最大值这种方法太复杂

//方法5:public static int min(int a,int b):求最小值
System.out.println(Math.min(2, 4));  //输出为:2
//求最小值的方法同求最大值的方法类似,均可嵌套比较

//方法6:public static double pow(double a,double b):a的b次幂
System.out.println(Math.pow(2, 3)); //输出结果为:8.0

//方法6:public static double random()返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。是一个随机数
System.out.println(Math.random()); //输出为0.0到1.0之间的一个随机数,例如本次输出为:0.30995073210444

//方法7:public static int round(float a):四舍五入
System.out.println(Math.round(3.4)); //输出为:3

//方法8:public static double sqrt(double a):一个数的正平方根
System.out.println(Math.sqrt(2)); //输出结果为:1.4142135623730951

}


}
3、Random类
package Random;


import java.util.Random;


/*
 * Random:是一个可以获取随机数的类
 *
 *public Random():无参构造方法
 *public Random(long seed) :指定long类型的数据进行构造随机数类对象 
 *
 *public int nextInt():获取随机数,它的范围是在int类型范围之内
 *
 *public int nextInt(int n):获取随机数,它的范围是在[0,n)之间
 *
 *Random 类按如下方式实现 nextInt 方法: 


 public int nextInt() {
   return next(32);
 }
 *
 * */
public class Random_method {


public static void main(String[] args) {
//创建Randomd对象
Random r = new Random();

//方法:public int nextInt(int n):获取随机数,它的范围是在[0,n)之间
for(int x = 0 ; x < 10 ; x ++) {
int n = r.nextInt(3) ;
System.out.print(n+"  "); //输出为:2  2  1  2  2  2  1  0  0  0  
}






}


}


猜你喜欢

转载自blog.csdn.net/qq_41833394/article/details/80186457