Math、System、Runtime

Math

  • 代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法。

Math类提供的常见方法

方法名说明
public static int abs(int a)获取参数绝对值
public static double ceil(double a)向上取整
public static double floor(double a)向下取整
public static int round(float a)四舍五入
public static max(int a,int b)获取两个int值中较大的值
public static double ppow(double a,double b)返回a的b次幂的值
public static double random()返回值为double的随机值,范围[ 0.0,1.0 ]
public class Test {
    public static void main(String[] args) {
        // 目标:了解Math类提供的常见方法
        System.out.println(Math.abs(12));   //12
        System.out.println(Math.abs(-12));  //12

        System.out.println(Math.ceil(4.0001));  //5.0
        System.out.println(Math.ceil(4.0));     //4.0

        System.out.println(Math.floor(4.999));  //4.0
        System.out.println(Math.floor(4.0));    //4.0

        System.out.println(Math.round(3.499));      //3
        System.out.println(Math.round(3.5001));     //3

        System.out.println(Math.max(10,20));    //20
        System.out.println(Math.min(10,20));    //10

        System.out.println(Math.pow(2,3));  //8.0
        System.out.println(Math.pow(3,2));  //9.0

        System.out.println(Math.random());
    }
}

System

  • System代表程序所在的系统,也是一个工具类。

System类提供的常见方法 

方法名说明
public static void exit(int status)终止当前运行的Java虚拟机
public static long currentTimeMillis()返回当前系统的时间毫秒值形式

Runtime 

  • 代表程序所在的运行环境
  • Runtime是一个单例类

Runtime类提供的常见方法

方法名说明
publci static Runtime getRuntime()返回与当前Java应用程序=关联的运行时对象

public void exit(int status)

终止当前运行的虚拟机
public int availableProcessors()返回Java虚拟机可用的处理器数
public long totalMemory()返回Java虚拟机中的内存总数
public long freeMemory()返回Java虚拟机中的可用内存
public Process exec(String command)启动某个程序,并返回代表该程序的对象
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException, InterruptedException {
        // 目标:了解Runtime类提供的常见方法
        //返回与当前Java应用程序关联的运行时对象
        Runtime r = Runtime.getRuntime();

        //终止当前运行的虚拟机
        //r.exit(0);

        //返回Java虚拟机可用的处理器数
        System.out.println(r.availableProcessors());

        //返回Java虚拟机中的内存总量
        System.out.println(r.totalMemory() / 1024.0 / 1024.0 + "MB");

        //返回Java虚拟机中的可用内存
        System.out.println(r.freeMemory() / 1024.0 / 1024.0 + "MB");

        //启动某个程序,并返回代表该程序的对象
        Process p = r.exec("D:\\数据库\\Navicat Premium 16\\navicat.exe");
        Thread.sleep(5000); //让程序在这里暂停5s后继续往下走
        p.destroy();    //销毁!关闭程序
    }
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. spyder 5.4.1 requires pyqt5<5.16, which is not installed. spyder 5.4.1 requires pyqtwebengine<5.16, which is not installed. Successfully installed aiofiles-23.1.0 altair-4.2.2 blinker-1.6.2 cachetools-5.3.1 chardet-5.1.0 cmake-3.26.3 cpm_kernels-1.0.11 fastapi-0.95.2 ffmpy-0.3.0 gitdb-4.0.10 gitpython-3.1.31 gradio-3.32.0 gradio-client-0.2.5 h11-0.14.0 httpcore-0.17.2 httpx-0.24.1 latex2mathml-3.76.0 linkify-it-py-2.0.2 lit-16.0.5 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 mdtex2html-1.2.0 mdurl-0.1.2 nvidia-cublas-cu11-11.10.3.66 nvidia-cuda-cupti-cu11-11.7.101 nvidia-cuda-nvrtc-cu11-11.7.99 nvidia-cuda-runtime-cu11-11.7.99 nvidia-cudnn-cu11-8.5.0.96 nvidia-cufft-cu11-10.9.0.58 nvidia-curand-cu11-10.2.10.91 nvidia-cusolver-cu11-11.4.0.1 nvidia-cusparse-cu11-11.7.4.91 nvidia-nccl-cu11-2.14.3 nvidia-nvtx-cu11-11.7.91 orjson-3.8.14 protobuf-3.20.3 pydantic-1.10.8 pydeck-0.8.1b0 pydub-0.25.1 pygments-2.15.1 pympler-1.0.1 python-multipart-0.0.6 rich-13.4.1 semantic-version-2.10.0 sentencepiece-0.1.99 smmap-5.0.0 starlette-0.27.0 streamlit-1.22.0 streamlit-chat-0.0.2.2 torch-2.0.1 transformers-4.27.1 triton-2.0.0 tzlocal-5.0.1 uc-micro-py-1.0.2 uvicorn-0.22.0 validators-0.20.0 websockets-11.0.3 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv 解释下
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值