一、实验预热
1、解释String str = "hello";和String str = new String("hello");的区别。
String str = "hello";会先在方法区的字符串常量池中查找是否有"hello",如果有则直接取出,没有则创建
String str = new String("hello");会直接创建一个对象
2、请说明访问权限修饰符protected所修饰的属性和方法的访问权限。
本类、同包、子类
3、请解释什么是GC, 以及垃圾怎么处理。
GC就是垃圾回收机制,JVM会不定期的自动回收垃圾,也可以调用System.gc()手动请求JVM进行垃圾回收。
4、请简述final, finally, finalize的区别。
1)final用于声明属性、方法和类,分别表示属性不可变、方法不可覆盖、类不可继承。 内部类要访问局部变量,局部变量必须定义成final类型,否则调用内部类方法时会造成外部类局部变量和内部类中对应的变量的不一致,例如:
public class Out {
public void test(String a) {
class In {
public void function() {
System.out.println(a);
}
}
a="hello";
new In().function();
}
public static void main(String[] args) {
new Out().test("hi");
}
}
Java1.8在局部变量没有重新赋值的情况下会默认局部变量为final型。
2)finally是异常处理语句结构的一部分,表示总是执行。
3)finalize是Object类的一个方法,在垃圾回收机制执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾回收时的其他资源回收,例如关闭文件等。JVM不保证此方法总被调用
5、为什么String可以用+号进行拼接?
JVM看到+号会自动编译成StringBuffer.append(),然后再返回一个String类型的对象
二、实验内容
1、有如下代码操作:
for (int x = 0; x < 3000; x++) {
str += x;
}
1)求出这个操作执行所需要的时间
代码:
package leif.tests;
public class ExperimentalReport {
public static void main(String[] args) {
@SuppressWarnings("unused")
String str = "";
long start = System.currentTimeMillis();
for (int x = 0; x < 3000; x++) {
str += x;
}
long end = System.currentTimeMillis();
System.out.println("操作执行所需要的时间:" + (end - start) + "毫秒");
}
}
结果截图:

2)求出这个操作前JVM的内存情况(MAX、TOTAL、FREE)以及操作后JVM的内存情况,最后请用GC释放掉这部分操作消耗掉的内存,重新输出释放后JVM的内存情况
代码:
package leif.tests;
public class ExperimentalReport {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("操作前JVM的内存情况");
System.out.println("maxMemory:" + runtime.maxMemory());
System.out.println("totalMemory:" + runtime.totalMemory());
System.out.println("freeMemory:" + runtime.freeMemory());
@SuppressWarnings("unused")
String str = "";
for (int x = 0; x < 3000; x++) {
str += x;
}
System.out.println("操作后JVM的内存情况");
System.out.println("maxMemory:" + runtime.maxMemory());
System.out.println("totalMemory:" + runtime.totalMemory());
System.out.println("freeMemory:" + runtime.freeMemory());
runtime.gc();
System.out.println("释放后JVM的内存情况");
System.out.println("maxMemory:" + runtime.maxMemory());
System.out.println("totalMemory:" + runtime.totalMemory());
System.out.println("freeMemory:" + runtime.freeMemory());
}
}
结果截图:

2、现在有两种字符串初始化方法:
1)String string1 = "Hello";
2)String string2 = new String("Hello");
以及两种String拼接方法:
1)StringBuffer stringBuffer = new StringBuffer("Hello");
stringBuffer.append(" world");
2)String string3 = "Hello";
String string4 = string3 + " world";
请算出上述各方法的执行时间。
代码:
package leif.tests;
public class ExperimentalReport {
public static void main(String[] args) {
long start = System.nanoTime();
@SuppressWarnings("unused")
String string1 = "Hello";
long end = System.nanoTime();
System.out.println("1)初始化方法的执行时间:" + (end - start));
start = System.nanoTime();
@SuppressWarnings("unused")
String string2 = new String("Hello");
end = System.nanoTime();
System.out.println("2)初始化方法的执行时间:" + (end - start));
start = System.nanoTime();
StringBuffer stringBuffer = new StringBuffer("Hello");
stringBuffer.append(" world");
end = System.nanoTime();
System.out.println("1)拼接方法的执行时间:" + (end - start));
start = System.nanoTime();
String string3 = "Hello";
@SuppressWarnings("unused")
String string4 = string3 + " world";
end = System.nanoTime();
System.out.println("2)拼接方法的执行时间:" + (end - start));
}
}
结果截图:
