毕业设计网站建设流程,绵阳网站建设怎么选,wordpress后台图片无法显示,毕设网站建设论文Java的API#xff08;1#xff09;
一、Math的API
是一个帮助我们进行数学计算的工具类私有化构造方法#xff0c;所有的方法都是静态的#xff08;可以直接通过类名.调用#xff09; 平方根#xff1a;Math.sqrt()立方根#xff1a;Math.cbrt()
示例#xff1a;
p…Java的API1
一、Math的API
是一个帮助我们进行数学计算的工具类私有化构造方法所有的方法都是静态的可以直接通过类名.调用 平方根Math.sqrt()立方根Math.cbrt()
示例
public class MathDemo1 {public static void main(String[] args) {// Math类在Java的lang包中所以不需要引入// 且都是静态方法可以直接通过类名.直接调用// 绝对值System.out.println(Math.abs(-123.456)); // 123.456System.out.println(Math.abs(-156)); // 156// 向上取整(只要小数有值就会向整数上进)(往数轴的正方向进一)System.out.println(Math.ceil(123.3)); // 124.0System.out.println(Math.ceil(123.9)); // 124.0System.out.println(Math.ceil(-12.34)); // -12.0// 向下取整(小数的值直接舍弃)(往数轴的负方向减一)System.out.println(Math.floor(0.3)); // 0.0System.out.println(Math.floor(0.9)); // 0.0System.out.println(Math.floor(5.999999)); // 5.0System.out.println(Math.floor(-12.34)); // -13.0// 四舍五入满五向整数位进一(向数轴的两端进一)System.out.println(Math.round(5.363)); // 5System.out.println(Math.round(5.5)); // 6System.out.println(Math.round(5.6666)); // 6System.out.println(Math.round(-12.33)); // -12System.out.println(Math.round(-12.56)); // -13// 获取两个int值中的较大值(当两个不同数据类型的变量进行比较时会转化成精确度大的一方再进行比较)System.out.println(Math.max(3, 100000)); // 100000System.out.println(Math.max(2, 3.0)); // 3.0System.out.println(Math.max(3.14, 9)); // 9.0// 次幂(a的b次幂),返回值和参数值都是double类型System.out.println(Math.pow(3, 2)); // 9.0System.out.println((int)Math.pow(2, 4)); // 16// 返回double类型的随机数范围是 [0.0, 1.0)System.out.println(Math.random()); // 0.123456 (示例输出实际输出会随机)// 开平方根System.out.println(Math.sqrt(4)); // 2.0// 开立方根System.out.println(Math.cbrt(8)); // 2.0}
}二、System的API
System也是一个工具类提供一些与系统相关的方法
1、exit终止虚拟机
public static void exit(int status)——终止当前运行的JAVA虚拟机 // 终止当前的虚拟机// 方法形参的状态码// 0——表示当前虚拟机是正常停止// 非0——表示当前虚拟机是异常停止System.exit(0);System.out.println(看看我执行了吗); // 程序已经结束不会再执行2、currentTimeMillis()和nanoTime()返回当前系统时间
public static long currentTimeMillis()——返回当前系统的时间毫秒和纳秒值
// 返回当前系统的时间毫秒值形式(表示从计算机的时间原点C语言的生日1970.1.1.08:00:00(国内),当程序运行的时间)long l System.currentTimeMillis();System.out.println(l);该方法可以用来获取程序运行的总时间分析对比时间复杂度
示例对比两种方法判断质数的时间 int o 9489997;// 示例判断质数的两种时间复杂度
// long c System.currentTimeMillis();long c System.nanoTime();for (int i 2; i o; i) {if(o % i 0) {System.out.println(该数不是质数);break;}}
// long a System.currentTimeMillis();long a System.nanoTime();// 一个非质数会在该数的平方根前面会出现可以被整除的数以此可以来提高效率for (int i 2; i Math.sqrt(o); i) {if(o % i 0) {System.out.println(该数不是质数);break;}}
// long b System.currentTimeMillis();long b System.nanoTime();System.out.println(第一种方法时间为 (a - c) 纳秒); // 输出 第一种方法时间为23300纳秒System.out.println(第二种方法时间为 (b - a) 纳秒); // 输出 第二种方法时间为19900纳秒3、arraycopy()进行数组的拷贝
public static void arraycopy(数据源数组起始索引目的地数组起始索引拷贝个数)——数组拷贝
// 拷贝数组int[] arr1 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int[] arr2 new int[10];// 把arr1数组中的数据拷贝到arr2数组中// 参数一 数据源要拷贝的数据从哪个数组而来// 参数二 从数据源数组中的第几个索引开始拷贝// 参数三 拷贝的目的地要拷贝到哪个数组// 参数四 目的地数组的起始索引// 参数五 拷贝的个数
// System.arraycopy(arr1, 0, arr2, 0, 10);System.arraycopy(arr1, 0, arr2, 4, 3);for (int i 0; i arr2.length; i) {System.out.print(arr2[i] ); // 输出 0 0 0 0 1 2 3 0 0 0}数组拷贝的注意事项
如果数据源数组和目的地数组都是基本数据类型那么两者的类型必须保持一致否则会报错在拷贝的时候需要考虑数组的长度如果超出范围就会报错如果数组源数组和目的地数组都是引用数据类型那么子类类型可以赋值给父类类型若要再次使用则需要强转
示例
public class SystemDemo2 {public static void main(String[] args) {Student s1 new Student(小明, 20);Student s2 new Student(小资, 21);Student s3 new Student(小兰, 22);Student[] arr1 {s1, s2, s3};Person[] arr2 new Person[3];// 把arr1中的对象的地址赋值给arr2中System.arraycopy(arr1, 0, arr2, 0, 3);// 遍历数组arr2for (int i 0; i arr2.length; i) {Student stu (Student)arr2[i];System.out.println(stu.getName() stu.getAge());}}
}class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}
}class Student extends Person {public Student() {}public Student(String name, int age) {super(name, age);}
}三、Runtime的API
Runtime表示当前虚拟机的运行环境 示例
public class RuntimeDemo1 {public static void main(String[] args) throws IOException {// 1、获取Runtime的对象Runtime r1 Runtime.getRuntime();// 2、虚拟机的终止// 0————正常终止虚拟机// 非0值————异常终止虚拟机
// Runtime.getRuntime().exit(0);
// System.out.println(看看我执行了吗); // 不会执行// 3、获取CPU的线程数System.out.println(Runtime.getRuntime().availableProcessors()); // 4、获取总内存的大小单位byte字节System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024); // 5、已经占用的内存的大小单位byte字节System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);// 6、剩余的内存的大小单位byte字节System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);// 7、运行cmd命令// 打开记事本Runtime.getRuntime().exec(notepad);// shutdown:关机// 加上参数才能执行// -s : 默认在1分钟之后关机// -s -t 指定时间 指定关机时间// -a : 取消关系操作// -r 关闭并重启
// Runtime.getRuntime().exec(shutdown -s -t 36000);Runtime.getRuntime().exec(shutdown -a);}
}