当前位置: 首页 > news >正文

Java 核心工具类 API 详解(一):从 Math 到 Runtime 的实用指南

文章主页-爱因斯晨

文章专栏-Java专栏

在这里插入图片描述

加速更新中!感谢大家支持和陪伴!
祝兄弟们得偿所愿天天开心!文末有投票哦!

文章目录

    • 文章主页-爱因斯晨
    • 文章专栏-Java专栏
  • 一、Math
    • Math类的常用方法
  • 二、System
  • 三、Runtine

一、Math

不需要背,要学会记一下类名和类的作用,养成查阅API帮助文档的习惯

是一个帮助我们用于进行数学计算的工具类

私有化静态方法,里面的所有的方法全是静态的

Math类的常用方法

在这里插入图片描述

package demo01;public class MathDemo01 {public static void main(String[] args) {//abs 获取参数的绝对值System.out.println(Math.abs(-3.14));System.out.println(Math.abs(3.14));//bug解释//以int型为例,int的取值范围是-2147483648到2147483647//如果参数是-2147483648,那么取绝对值后就会超出int的取值范围,所以会返回-2147483648//如果参数是-2147483647,那么取绝对值后就会返回2147483647//可以索引到源码中的abs方法,发现abs方法的返回值是int类型,所以会返回-2147483648System.out.println(Math.abs(-2147483648));//ceil 向上取整 数学中的进一法,往数轴中的正方向取整,即大于等于该数的最小整数System.out.println(Math.ceil(3.9));System.out.println(Math.ceil(-3.9));//floor 向下取整 数学中的去一法,往数轴中的负方向取整,即小于等于该数的最大整数System.out.println(Math.floor(3.9));System.out.println(Math.floor(-3.9));//round 四舍五入System.out.println(Math.round(3.4));System.out.println(Math.round(-3.6));System.out.println(Math.round(-3.5));//pow 求幂System.out.println(Math.pow(2,3));//max 获取最大值System.out.println(Math.max(3.4,5.6));//random 获取随机数System.out.println(Math.random());}}

在这里插入图片描述

关于abs我们可以回溯源码,可以看到他们做了一个判断

public static long absExact(long a) {if (a == Long.MIN_VALUE)throw new ArithmeticException("Overflow to represent absolute value of Long.MIN_VALUE");elsereturn abs(a);}/*** Returns the absolute value of a {@code float} value.* If the argument is not negative, the argument is returned.* If the argument is negative, the negation of the argument is returned.* Special cases:* <ul><li>If the argument is positive zero or negative zero, the* result is positive zero.* <li>If the argument is infinite, the result is positive infinity.* <li>If the argument is NaN, the result is NaN.</ul>** @apiNote As implied by the above, one valid implementation of* this method is given by the expression below which computes a* {@code float} with the same exponent and significand as the* argument but with a guaranteed zero sign bit indicating a* positive value:<br>* {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))}** @param   a   the argument whose absolute value is to be determined* @return  the absolute value of the argument.*/

在max判断中,其实源码中也做了一个三元计算符的判断

@IntrinsicCandidatepublic static double max(double a, double b) {if (a != a)return a;   // a is NaNif ((a == 0.0d) &&(b == 0.0d) &&(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {// Raw conversion ok since NaN can't map to -0.0.return b;}return (a >= b) ? a : b;}/*** Returns the smaller of two {@code int} values. That is,* the result the argument closer to the value of* {@link Integer#MIN_VALUE}.  If the arguments have the same* value, the result is that same value.** @param   a   an argument.* @param   b   another argument.* @return  the smaller of {@code a} and {@code b}.*/

练习:
判断一个数是否为质数

public static boolean isPrime(int number){for (int i = 2 ; i < number ; i++){if (number%i==0){return false ;}}return true ;
}

我们发现这样效率太慢了,于是我们发现一个数的因子在平方根的左边,于是我们可以从左边遍历,这样可以节省时间

在这里插入图片描述

public static boolean isPrime(int number){for (int i = 2 ; i < Math.sqrt(number) ; i++){if (number%i==0){return false ;}}return true ;
}

练习2:

在这里插入图片描述

要求1:统计一共有多少个水仙花数

package demo01;public class MathDemo02 {public static void main(String[] args) {//要求1:统计一共有多少个水仙花数字//水仙花数:100-999之间的数字int count = 0;for (int i = 100; i < 1000; i++) {int ge = i % 10;int shi = i / 10 % 10;int bai = i / 100 % 10;//判断是否为水仙花数double sum  =Math.pow(ge,3) + Math.pow(shi,3) + Math.pow(bai,3);if (sum == i) {count++;System.out.println(i);}}System.out.println(count);}
}

在这里插入图片描述

要求2:(课后作业)证明没有两位的自幂数

package demo01;public class MathDemo03 {public static void main(String[] args) {//要求1:统计一共有多少个水仙花数字//水仙花数:100-999之间的数字int count = 0;for (int i = 10; i < 99; i++) {int ge = i % 10;int shi = i / 10 % 10;//判断是否为水仙花数double sum  =Math.pow(ge,2) + Math.pow(shi,2);if (sum == i) {count++;System.out.println(i);}}System.out.println(count);}
}

在这里插入图片描述

要求3:统计有多少个四叶玫瑰数

package demo01;public class MathDemo04 {public static void main(String[] args) {//要求1:统计一共有多少个水仙花数字//水仙花数:100-999之间的数字int count = 0;for (int i = 1000; i < 9999; i++) {int ge = i % 10;int shi = i / 10 % 10;int bai = i / 100 % 10;int qian = i / 1000 % 10;//判断是否为水仙花数double sum  =Math.pow(ge,4) + Math.pow(shi,4) + Math.pow(bai,4)+Math.pow(qian,4);if (sum == i) {count++;System.out.println(i);}}System.out.println(count);}
}

在这里插入图片描述

二、System

System也是一个工具类,提供了一个选哪个与系统相关的方法

在这里插入图片描述

计算机中的时间原点:1970年1月1日 00:00:00 也就是C语言的生日

我们是东八区所以是 1970年1月1日 08:00:00

在这里插入图片描述

package demo02;public class SystemDemo01 {public static void main(String[] args) {//终止当前的虚拟机推出程序
//    public static void exit(int status) {
//        System.out.println("退出程序");
//    }//方法的形参://状态码://0:正常退出//非0:异常退出System.exit(0);System.out.println("看看我执行了吗?");}}

在这里插入图片描述

显而易见,程序在输出语句前就退出了执行

package demo02;public class SystemDemo01 {public static void main(String[] args) {//终止当前的虚拟机推出程序
//    public static void exit(int status) {
//        System.out.println("退出程序");
//    }//方法的形参://状态码://0:正常退出//非0:异常退出//System.exit(0);// System.out.println("看看我执行了吗?");//获取当前系统的毫秒值long l = System.currentTimeMillis();System.out.println(l);}
}

在这里插入图片描述

这个类可以获得程序运行的时间

package demo02;public class SystemDemo01 {public static void main(String[] args) {//终止当前的虚拟机推出程序
//    public static void exit(int status) {
//        System.out.println("退出程序");
//    }//方法的形参://状态码://0:正常退出//非0:异常退出//System.exit(0);// System.out.println("看看我执行了吗?");//获取当前系统的毫秒值//long l = System.currentTimeMillis();//System.out.println(l);//拷贝数组int [] arr1 = {1,2,3,4,5,6,7,8,9};int [] arr2 = new int [9];//参数1:源数组//参数2:源数组的起始索引//参数3:目标数组//参数4:目标数组的起始索引//参数5:拷贝的个数System.arraycopy(arr1, 0, arr2, 0, 9);for (int i = 0; i < arr2.length; i++) {System.out.print(arr2[i]);}}
}

在这里插入图片描述

第三个方法细节:

1.如果数据源数组和目的地数组都是基本数据类型,那么两者的类型必须保持一致,否则会报错

2.再拷贝的时候需要考虑数组的长度,如果超出范围也会报错

3.如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型

三、Runtine

在这里插入图片描述

package demo03;public class RoutineDemo01 {public static void main(String[] args) {//1.获取Runtime对象Runtime runtime = Runtime.getRuntime();//2.exit停止虚拟机//runtime.exit(0);//System.out.println("看看我执行了吗?");//3.获取虚拟机的最大内存long maxMemory = runtime.maxMemory();System.out.println("虚拟机的最大内存"+maxMemory);//4.获得CPU的核数int availableProcessors = runtime.availableProcessors();System.out.println("CPU的核数"+availableProcessors);//5.获得JVM的总内存long totalMemory = runtime.totalMemory();System.out.println("JVM的总内存"+totalMemory/1024/1024+"M");//6.获得JVM的空闲内存long freeMemory = runtime.freeMemory();System.out.println("JVM的空闲内存"+freeMemory/1024/1024+"M");//7.运行cmd命令Runtime.getRuntime().exec("notepad.exe");}
}

在第29行代码中会遇到红色波浪线,这就是我们后面要学到的异常

在这里插入图片描述

我们可以将鼠标点击红色波浪线然后Alt+回车

在这里插入图片描述

修改后的代码:

package demo03;import java.io.IOException;public class RoutineDemo01 {public static void main(String[] args) throws IOException {//1.获取Runtime对象Runtime runtime = Runtime.getRuntime();//2.exit停止虚拟机//runtime.exit(0);//System.out.println("看看我执行了吗?");//3.获取虚拟机的最大内存long maxMemory = runtime.maxMemory();System.out.println("虚拟机的最大内存"+maxMemory);//4.获得CPU的核数int availableProcessors = runtime.availableProcessors();System.out.println("CPU的核数"+availableProcessors);//5.获得JVM的总内存long totalMemory = runtime.totalMemory();System.out.println("JVM的总内存"+totalMemory/1024/1024+"M");//6.获得JVM的空闲内存long freeMemory = runtime.freeMemory();System.out.println("JVM的空闲内存"+freeMemory/1024/1024+"M");//7.运行cmd命令Runtime.getRuntime().exec("notepad.exe");}
}
http://www.dtcms.com/a/286186.html

相关文章:

  • 【AI News | 20250717】每日AI进展
  • 【解码文本世界的“隐形分界线”:Windows与Linux回车换行之谜】
  • 基于单片机智能充电器系统设计
  • 如何检查GitHub上可能潜在的信息泄漏
  • 深入理解 Redis 集群化看门狗机制:原理、实践与风险
  • synchronized锁升级过程【AI笔记,仅供自己参考】
  • Pythonday17
  • 中国1km逐月潜在蒸散发数据集 - matlab按shp批量裁剪
  • lesson17:Python函数之递归、匿名函数与变量作用域
  • 电脑装机软件一键安装管理器
  • sky-take-out项目Mybatis的使用
  • MyBatis 动态 SQL:让 SQL 语句随条件灵活变化
  • Java面试宝典:Maven
  • UE5多人MOBA+GAS 番外篇:使用ECC(UGameplayEffectExecutionCalculation)制作伤害计算的流程
  • 【Java新特性】Java 17 新特性全解析
  • 嵌入式Linux:什么是线程?
  • Docker搭建Elasticsearch和Kibana
  • 图机器学习(12)——社区检测
  • 飞牛上使用Docker方式部署LibreTV,再配合内网穿透,实现免费无广告刷剧的服务教程
  • Oracle ADG 一键自动化搭建脚本
  • 【宇树科技:未来1-3年,机器人可流水线打螺丝】
  • Go语言实战案例-模拟登录验证(用户名密码)
  • 什么是高光谱相机,它与数码相机有什么区别?
  • C#引用转换核心原理:类型视角切换
  • 弧焊机器人智能节气装置
  • Android 开机流程中的图片与动画解析
  • leetcode:冗余连接 II[并查集检查环][节点入度]
  • Android 之 audiotrack
  • 协作机器人操作与编程-PE系统示教编程和脚本讲解(直播回放)
  • 多模态大模型重构人机交互,全感官时代已来