一:常用类
1. Math类
package common;
public class MathDemo {
public static void main(String[] args) {
double powResult=Math.pow(2,3);
System.out.println("2的3次方为:"+powResult);
System.out.println("8的平方根:"+Math.sqrt(8));
System.out.println("-7.35的绝对值是:"+Math.abs(-7.35));
System.out.println("8.1向上取整:"+Math.ceil(8.1));
System.out.println("9.9向下取整:"+Math.floor(9.9));
System.out.println("8.499四舍五入的结果:"+Math.round(8.499));
System.out.println("8.5四舍五入的结果:"+Math.round(8.5));
System.out.println("随机返回一个从0.0(包括)到1.0(不包括)值:"+Math.random());
int ran=(int)(Math.random()*16)+3;
System.out.println("随机返回3到18的数字:"+ran);
System.out.println("圆周率是:"+Math.PI);
}
}
运行结果:
2. Random类:生成“伪随机数”。
public int nextInt(int bound)
返回从0(包含)到bound(不包含)的一个“伪随机”整数值。
public boolean nextBoolean()
返回一个“伪随机”的boolean值。
Random ran=new Random();
int result=ran.nextInt(16)+3;
System.out.println("随机返回3到18的数字:"+result);
System.out.println("随机返回boolean值:"+ran.nextBoolean());
运行结果:
3. System类
包含in、out和err三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。
exit()方法public static void exit(int status)
该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。
gc()方法public static void gc()
该方法的作用是请求系统进行垃圾回收。
垃圾回收时,会先调用finalize()方法,释放非java资源
public static long currentTimeMillis()
以毫秒为单位返回从1970年1月1日午夜到当前事件的毫秒数。
package common;
public class SystemDemo {
public static void main(String[] args) {
System.out.println("正常输出信息...");
System.err.println("输出标准错误信息..."); //红色字体
int[] a = {1,2,3,4};
int[] b = new int[5];
System.arraycopy(a,1,b,3,2); // 数组拷贝
for(int x:b){
System.out.print(x+" ");
}
System.exit(0); // 退出程序
}
}
4. Runtime类
作用:使应用程序与其运行的环境相关联
通过使用getRuntime() 静态方法获得实例。
exec(String command) 执行指定的字符串命令
destroy() 杀掉进程
package common;
import java.io.IOException;
import java.util.Scanner;
public class RuntimeDemo {
public static void main(String[] args) {
Runtime rt= Runtime.getRuntime();
rt.gc(); // 显式请求JVM进行垃圾回收gc
System.out.println("当前JVM的内存总量是:"+rt.totalMemory()+"字节");
System.out.println("JVM试图使用的最大内存量:"+rt.maxMemory());
System.out.println("当前JVM的空闲内存量:"+rt.freeMemory());
try {
rt.exec("notepad");
rt.exec("calc");
Process qq=rt.exec("F:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQ.exe"); // 打开QQ进程
Scanner scan=new Scanner(System.in);
System.out.print("要关闭QQ吗?y/n");
String choice=scan.next();
if("y".equals(choice)){
qq.destroy(); // 销毁进程
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
5. Date类与SimpleDateFormat类
Date类:表示日期和时间
提供操作日期和时间各组成部分的方法
SimpleDateFormat类
用于定制日期时间的格式
Date date = new Date(); //创建日期对象
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //定制日期格式
String now = sdf.format(date);
System.out.println(now);
6. Calendar类
抽象类
用于设置和获取日期/时间数据的特定部分
int get(int field)返回给定日历字段的值
MONTH指示月(0-11)
DAY_OF_MONTH指示一个月中的某天
DAY_OF_WEEK指示一个星期中的某天
package common;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
Calendar cal=Calendar.getInstance();
System.out.println("第几个月:"+(cal.get(Calendar.MONTH)+1));
System.out.println("当前月的第几天:"+cal.get(Calendar.DAY_OF_MONTH));
System.out.println("星期几:"+cal.get(Calendar.DAY_OF_WEEK));
System.out.println("今年的第几天:"+cal.get(Calendar.DAY_OF_YEAR));
System.out.println("*******************************");
cal.set(1937, 6, 7); // 调整日历
System.out.println("第几个月:"+(cal.get(Calendar.MONTH)+1));
System.out.println("当前月的第几天:"+cal.get(Calendar.DAY_OF_MONTH));
System.out.println("星期几:"+cal.get(Calendar.DAY_OF_WEEK));
System.out.println("今年的第几天:"+cal.get(Calendar.DAY_OF_YEAR));
}
}
运行结果: