JavaAPI(6.2)

6.2 System与Runtime类

项目结构

在这里插入图片描述

Example10 (手敲代码)

package SystemRuntime;
/*
* 通过时间差计算出从开始到循环结束所用的时间,是一个代码性能计时实列*/

public class Example10 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        int sum = 0;
        for (int i = 0; i < 100000000; i++) {
            sum += i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间为: " + (endTime - startTime) + " ms");
    }
}

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

Example13 (手敲代码)

package SystemRuntime;
/*
* 演示了java'运行环境runtime的一些基本信息*/

public class Example13 {
    public static void main(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        System.out.println("处理器个数: " + runtime.availableProcessors());
        System.out.println("空闲内存数: " + runtime.freeMemory() / 1024 / 1024 + " MB");
        System.out.println("最大可用内存数: " + runtime.maxMemory() / 1024 / 1024 + " MB");
        System.out.println("内存总量: " + runtime.totalMemory() / 1024 / 1024 + " MB");
    }
}

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

Practice03(思考作答)

package SystemRuntime;

/**
 * 对比== 和 equals()方法的区别
 */
public class practice03 {
    public static void main(String[] args) {
        // 1、对于基本数据类型(只有==)
        int a = 10;
        int b = 10;
        System.out.println(a == b); // true
        System.out.println("********************************");
        // 2、对于引用数据类型(有==和equals())
        // 2.1、字符串是new出来的情况
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2); // false
        System.out.println(s1.equals(s2)); // true
        System.out.println("********************************");
        // 2.2、字符串是直接赋值的情况
        String s3 = "hello";
        String s4 = "hello";
        System.out.println(s3 == s4); // true
        System.out.println(s3.equals(s4)); // true
        System.out.println("********************************");
        // 2.3、字符串是new出来的情况和直接赋值的情况
        String s5 = new String("world");
        String s6 = "world";
        System.out.println(s5 == s6); // false
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值