Java:常用API:Math类,System类


黑马学习笔记
alt+回车抛出异常

Math

常用方法

这是static方法,直接Math打点调方法
在这里插入图片描述

代码

package com.zhang.math;

/**
 * @Author: ggdpzhk
 * @CreateTime: 2024-08-25
 * Math工具类的基本用法
 */
public class MathTest {
    public static void main(String[] args) {
        //取绝对值
        System.out.println(Math.abs(-90));
        System.out.println(Math.abs(9));
        System.out.println("---------------------");
        //向上取整
        System.out.println(Math.ceil(1.1));//2
        System.out.println(Math.ceil(-3.2));//-3
        //向下取整
        System.out.println(Math.floor(3.9));;//3
        System.out.println(Math.floor(-3.9));//-4
        System.out.println("---------------------");
        //四舍五入
        System.out.println(Math.round(3.5));//4
        System.out.println(Math.round(3.2));//3

        //获取两个int中的最大值 最小值
        System.out.println(Math.max(3, 9));
        System.out.println(Math.min(6, 9));

        //返回a的b次方
        System.out.println(Math.pow(2, 3));//8
        System.out.println(Math.pow(2, -2));//0.25
        System.out.println("---------------------");
        //随机数 返回值为double 范围为[0,1)
        System.out.println(Math.random());


    }
}

  • 运行结果
  • 在这里插入图片描述

System类

Java都是在虚拟机中运行的

常用方法

在这里插入图片描述

代码

package com.zhang.math;

/**
 * @Author: ggdpzhk
 * @CreateTime: 2024-08-25
 * System类系统类相关用法
 */
public class SystemTest {
    public static void main(String[] args) {
        //1.Java都是在虚拟机中运行的
        //终止当前运行的Java虚拟机
        //按照惯例,非零状态表示异常终止
        //System.exit(0);//0就是人为终止(不要轻易使用!!!)
        
        //这行代码下面的代码不会再执行,比如下面有个输出hello world,不会输出
        
        //2.取当前系统的时间
        //返回的是long类型的时间毫秒值,指的是从1970年1月1日0时0分0秒0毫秒开始计算的(c语言诞生的时间)
        //1s = 1000ms
        System.out.println(System.currentTimeMillis());

        //可以用来进行代码性能分析。就是在执行前调用一次,在执行后调用一次,然后计算两者之间的差值,差值就是程序执行时间。
    }
}

RunTime类

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

常用方法

在这里插入图片描述

代码

package com.zhang.math;


/**
 * @Author: ggdpzhk
 * @CreateTime: 2024-08-25
 */
public class RunTimeTest {
    public static void main(String[] args) {
        //创建运行时对象
        Runtime rt = Runtime.getRuntime();
        System.out.println("总内存:" + rt.totalMemory() / 1024 / 1024 + "M");
        System.out.println("空闲内存:" + rt.freeMemory() / 1024 / 1024 + "M");
        System.out.println("最大内存:" + rt.maxMemory() / 1024 / 1024 + "M");
        //启动某个程序并返回代表该程序的对象
        //参数是某个程序的路径
        //Process p = rt.exec();  这有个alt+回车抛出异常
        //让程序在这里暂停5s以后继续往下走
       /* try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*/
        //关闭程序
        //p.destroy();
    }
}
  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值