一、系统类System
1、 主要用于获取系统的属性数据。
2、System类常用的方法
常用方法 | 解释 |
---|---|
currentTimeMillis() | 获取当前系统时间(很重要) |
arraycopy(Object src, int srcPos, Object dest, int destPos, int length) | src:源数组、srcPos:源数组中的起始位置、dest:目标数组、destPos:目标数据中的起始位置、length:要复制的数组元素的数量 |
exit(int status) | 退出jvm,如果参数是0表示正常退出jvm,非0表示异常退出jvm |
gc() | 建议jvm赶快启动垃圾回收器回收垃圾 |
finalize() | 如果一个对象被垃圾回收器回收的时候,会先调用对象的finalize()方法 |
getenv(String name) | 根据环境变量的名字获取环境变量 |
getProperty(key) | 根据系统的属性名获取对应的属性值 |
class Person{
String name;
public Person(String name) {
this.name = name;
}
@Override
public void finalize() throws Throwable {
super.finalize();
System.out.println(this.name+"被回收了..");
}
}
public class Demo17.1{
public static void main(String[] args) {
int[] srcArr = {10,12,14,16,19};
//把srcArr的数组元素拷贝到destArr数组中。
int[] destArr = new int[4];
System.arraycopy(srcArr, 1, destArr, 0,4);
System.out.println("目标数组的元素:"+ Arrays.toString(destArr));
System.out.println("当前的系统时间:" + System.currentTimeMillis());
//System.exit(0); //jvm退出
for(int i = 0 ; i<4; i++){
new Person("狗娃"+i);
System.gc(); //建议马上启动垃圾回收期
}
System.out.println("环境变量:"+System.getenv("JAVA_HOME"));
Properties properties = System.getProperties(); //获取系统的所有属性
properties.list(System.out);
String value = System.getProperty("os.name");//根据系统的属性名获取对应的属性值
System.out.println("当前系统:"+value);
}
}
二、RunTime类
1、该类类主要代表了应用程序运行的环境。
2、
常用方法 | 解释 |
---|---|
getRuntime() | 返回当前应用程序的运行环境对象 |
exec(String command) | 根据指定的路径执行对应的可执行文件 |
freeMemory() | 返回Java虚拟机中的空闲内存量,以字节为单位 |
maxMemory() | 返回Java虚拟机试图使用的最大内存量 |
totalMemory() | 返回Java虚拟机中的内存总量 |
public class Demo17.2{
public static void main(String[] args) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe");
Thread.sleep(3000); //让当前程序停止3秒
process.destroy();//杀掉子进程
System.out.println("Java虚拟机中的空闲内存量:"+runtime.freeMemory());
System.out.println("Java虚拟机试图使用的最大内存量:"+ runtime.maxMemory());
System.out.println("返回Java虚拟机中的内存总量:"+ runtime.totalMemory());
}
}
三、日期类 Date
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Demo17.3{
public static void main(String[] args) throws ParseException {
//Date date = new Date(); // 获取当前的系统时间
//System.out.println("年份:"+ date.getYear());// 已过时
Calendar calendar = Calendar.getInstance(); //获取当前的系统时间
System.out.println("年:"+ calendar.get(Calendar.YEAR));
System.out.println("月:"+ (calendar.get(Calendar.MONTH)+1));
System.out.println("日:"+ calendar.get(Calendar.DATE));
System.out.println("时:"+ calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+ calendar.get(Calendar.MINUTE));
System.out.println("秒:"+ calendar.get(Calendar.SECOND));
// 需求:显示当前系统时间,格式为2018年06月20日 xx时xx分xx秒
// 日期格式化类:SimpleDateFormat
// 作用1:可以把日期转换转指定格式的字符串 format()
// 作用2:可以把一个字符转换成对应的日期 parse()
Date date = new Date(); //获取当前的系统时间
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ; //使用了默认的格式创建了一个日期格式化对象
String time = dateFormat.format(date); //可以把日期转换转指定格式的字符串
System.out.println("当前的系统时间:"+ time);
String birthday = "2000年12月26日 11:29:08";
Date date2 = dateFormat.parse(birthday); //注意:指定的字符串格式必须要与SimpleDateFormat的模式要一致才可以转换
System.out.println(date2);
}
}
四、数学类Math
1、主要是提供了很多的数学公式
2、常用方法
常用方法 | 解释 |
---|---|
abs(double a) | 获取绝对值 |
ceil(double a) | 向上取整 |
floor(double a) | 向下取整 |
round(float a) | 四舍五入 |
random() | 产生一个随机数,大于等于0.0且小于1.0的伪随机double值 |
public class Demo17.4{
public static void main(String[] args) {
System.out.println("绝对值:"+Math.abs(-3)); //3
System.out.println("向上取整:"+Math.ceil(3.14)); //4.0
System.out.println("向下取整:"+Math.floor(-3.14)); //-4.0
System.out.println("四舍五入:"+Math.round(3.54)); //4
System.out.println("随机数:"+Math.random());
}
}
五、随机数类Random
import java.util.Random;
public class Demo17.5{
public static void main(String[] args) {
Random random = new Random();
int randomNum = random.nextInt(10)+1; //产生的随机数就是0-10之间
System.out.println("随机数:"+ randomNum);
//需求:编写一个函数随机产生四位的验证码
char[] arr = {'中','国','n','a','Q','f','B'};
StringBuilder sb = new StringBuilder();
Random random = new Random();
//需要四个随机数,通过随机数获取字符数组中的字符
for(int i = 0 ; i< 4 ; i++){
int index = random.nextInt(arr.length);//产生的随机数必须在数组的索引值范围之内
sb.append(arr[index]);
}
System.out.println("验证码:"+ sb);
}
}