Zvm
Java 实现 JVM
Install / Use
/learn @tzh476/ZvmREADME
Java实现简易JVM
主要模块和样例:
<details> <summary>1. 读取并解析class文件,如String、Thread等类(支持jdk8及以下)</summary>部分类可能在demo运行时用到:
zvm\bytecode\java\lang\System.classzvm\bytecode\java\io\PrintStream.classzvm\bytecode\java\lang\Thread.classzvm\bytecode\com\zvm\javaclass\integer\Table1.class(注解相关)
- 执行样例:
public class GaussTest {
public GaussTest() {
}
public static void main(String[] args) {
int sum = 0;
for(int i = 5; i <= 20; i += 10) {
sum += i;
}
System.out.println(sum);
}
}
输出结果:
file path : GaussTest
20
</details>
<details>
<summary>3. 方法调用(静态方法、构造方法、实例方法(支持继承多态))</summary>
- 静态递归方法执行样例(invokestatic):
public class FibonacciTest {
public static void main(String[] args) {
long x = fibonacci(8);
System.out.println(x);
}
private static long fibonacci(long n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
输出结果:
file path : FibonacciTest
21
- 构造方法调用(invokespecial)
public class FibonacciTest {
public static void main(String[] args) {
long x = fibonacci(8);
System.out.println(x);
}
private static long fibonacci(long n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
输出结果:
file path : FibonacciTest
21
- 调用实例方法,支持继承多态(invokevirtual)
public class InvokeVirtualTest {
public static void main(String[] args) {
Vector2D v2 = new Vector2D(2.1, 2.2);
Vector2D v3 = new Vector3D(3.1, 3.2, 3.3);
v2.multiply(2);
v3.multiply(3);
System.out.println(v2.x);
System.out.println(v2.y);
System.out.println(v3.x);
System.out.println(v3.y);
System.out.println(((Vector3D)v3).z);
}
}
输出结果:
file path : ch07/InvokeVirtualTest
4.2
4.4
9.3
9.600000000000001
9.899999999999999
</details>
<details>
<summary>4. 数组</summary>
- 一维int数组冒泡排序:
public class BubbleSortTest {
public static void main(String[] args) {
int[] arr = {
22, 84, 77, 11, 95, 9, 78, 56,
36, 97, 65, 36, 10, 24 ,92, 48
};
//printArray(arr);
bubbleSort(arr);
//System.out.println(123456789);
printArray(arr);
}
private static void bubbleSort(int[] arr) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < arr.length - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
private static void printArray(int[] arr) {
for (int i : arr) {
System.out.println(i);
}
}
}
输出结果:
file path : ch08/BubbleSortTest
9
10
...
- 一维double数组冒泡排序
public class DoubleBubbleSortTest {
public static void main(String[] args) {
double[] arr = {
22.2, 84.4, 77.5, 11.2, 95.3, 9.2, 78.2, 56.2,
36.1, 97.1, 65.1, 36.1, 10.3, 24.3 ,92.3, 48.3
};
//printArray(arr);
bubbleSort(arr);
//System.out.println(123456789);
printArray(arr);
}
private static void bubbleSort(double[] arr) {
boolean swapped = true;
int j = 0;
double tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < arr.length - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
private static void printArray(double[] arr) {
for (double i : arr) {
System.out.println(i);
}
}
}
输出结果:
file path : ch08/DoubleBubbleSortTest
9.2
10.3
11.2
22.2
24.3
...
</details>
<details>
<summary>5. 字符串和字符串数组</summary>
- 字符串加法,涉及类有java/lang/StringBuilder、java/lang/AbstractStringBuilder、java/lang/Math 、java/util/Arrays、java/io/FilterOutputStream、java/io/OutputStream、 java/io/PrintStream、java/lang/String:
public class StringBuilderTest {
public static void main(String[] args) {
String hello = "hello,";
String world = "world!";
String str = hello + world;
System.out.println(str);
}
}
输出结果:
file path : ch09/StringBuilderTest
总内存:8912 分配:8完成 当前已使用:8
总内存:8912 分配:12完成 当前已使用:20
...
hello,world!
...
- 字符串数组
public class ArrayDemo {
public static void main(String[] args) {
int[] a1 = new int[10]; // newarray
String[] a2 = new String[10]; // anewarray
//int[][] a3 = new int[10][10]; // multianewarray
int x = a1.length; // arraylength
a1[0] = 100; // iastore
int y = a1[0]; // iaload
a2[0] = "0abc"; // aastore
String s = a2[0]; // aaload
System.out.println( s);
a2[1] = "1xxxxyyxyy";
a2[2] = "2xxxxyyxyy";
for(int i = 0; i < 3; i++){
System.out.println(a2[i] + " stringbuilderTest");
}
}
}
输出结果:
file path : ch09/ArrayDemo
总内存:8912 分配:40完成 当前已使用:40
...
0abc
总内存:8912 分配:8完成 当前已使用:104
总内存:8912 分配:20完成 当前已使用:124
...
0abc stringbuilderTest
总内存:8912 分配:8完成 当前已使用:364
总内存:8912 分配:32完成 当前已使用:396
...
1xxxxyyxyy stringbuilderTest
总内存:8912 分配:8完成 当前已使用:580
总内存:8912 分配:32完成 当前已使用:612
...
2xxxxyyxyy stringbuilderTest
...
</details>
<details>
<summary>6. 调用本地方法</summary>
- 只实现了这个方法println,里面调用了arraycopy
public class StringBuilderTest {
public static void main(String[] args) {
String hello = "hello,";
String world = "world!";
String str = hello + world;
System.out.println(str);
}
}
输出结果:
file path : ch09/StringBuilderTest
hello,world!
</details>
<details>
<summary>7. GC相关:简单实现了标记清除算法</summary>
- 在zvm\src\main\java\com\zvm\memory\JavaHeap.java的HEAP_MAX_SIZE(此例中为32)的大小
public class GCTest1 {
private static final int SIZE = 3;
public static void main(String[] args){
test0();
test1();
test2();
}
private static void test0() {
/*字符串会创建22 byte + 8byte的数组:8byte:为String对象,22byte为char[11]*/
//System.out.println("test0 start");
int[] arr = new int[SIZE];
for (int i = 0; i < SIZE; i++){
arr[i] = 100 + i;
}
//System.out.println("test0 start");
}
private static void test1() {
//System.out.println("test1 start");
int[] arr = new int[SIZE];
for (int i = 0; i < SIZE; i++){
arr[i] = 100 + i;
}
//System.out.println("test1 start");
}
private static void test2() {
//System.out.println("test2 start");
int[] arr = new int[SIZE];
for (int i = 0; i < SIZE; i++){
arr[i] = 100 + i;
}
//System.out.println("test2 start");
}
}
输出结果:
file path : gc/GCTest1
总内存:32 分配:12完成 当前已使用:12
总内存:32 分配:12完成 当前已使用:24
总内存:32 已使用:24 当前需分配:12
总内存:32 回收情况:24->0 当前需分配:12
总内存:32 分配:12完成 当前已使用:12
</details>
其他 demo
<details> <summary>1. 嵌套类</summary>- 执行样例:
/**
1. 嵌套类:
- 静态嵌套类;
Classes
- 普通内部类(成员内部类)
- 局部内部类
- 匿名内部类
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
*/
public class T0NestedClass {
static class StaticClass{
public String staticClassKey = "staticClassVale";
public void test(){
System.out.println(staticClassKey);
}
}
/**
* 普通内部类
*/
class GenaralClass{
public String genaralClassKey = "genaralClassValue";
public void test(){
System.out.println(genaralClassKey);
}
}
public static void main(String[] args){
class LocalClass{
public String localClassKey = "LocalClassValue";
public void test(){
System.out.println(localClassKey);
}
}
AnonymousClass anonymousClass = new AnonymousClass(){
public String anonymousClassKey = "anonymousClassValue";
public void test(){
System.out.println(anonymousClassKey);
}
};
/*静态类测试*/
StaticClass staticClass = new StaticClass();
staticClass.test();
/*普通内部类测试*/
new T0NestedClass().generalClassTest();
/*局部内部类测试*/
LocalClass localClass = new LocalClass();
localClass.test();
/*匿名内部类测试*/
anonymousClass.test();
}
public void generalClassTest(){
GenaralClass genaralClass = new GenaralClass();
genaralClass.test();
}
}
class AnonymousClass{
public void test() {
}
}
输出结果:
file path : nestedclass\T0NestedClass
总内存:32000 分配:4完成 当前已使用:4
...
staticClassVale
总内存:32000 分配:0完成 当前已使用:92
总内存:32000 分配:8完成 当前已使用:100
总内存:32000 分配:8完成 当前已使用:108
总内存:32000 分配:34完成 当前已使用:142
genaralClassValue
总内存:32000 分配:4完成 当前已使用:146
总内存:32000 分配:8完成 当前已使用:154
总内存:32000 分配:30完成 当前已使用:184
LocalClassValue
anonymousClassValue
</details>
<details>
<summary>2. 类的加载、链接、初始化</summary>
- 执行样例:
public class T1ClassLink {
public static String value1 = "abc";
public static final String finalValue = "zvm";
public static String[] arr = new String[]{"arr0", "arr1", "dsafasfsdafd"};
public static final String[] finalArr = new String[]{"final-arr0", "final-arr1", "final-dsafasfsdafd"};
public String
