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
}
}
运行结果

&spm=1001.2101.3001.5002&articleId=155092971&d=1&t=3&u=bc478a61fbfb4c4c8e72ef705df03b0f)
273

被折叠的 条评论
为什么被折叠?



