Skip to content

Commit ab506a6

Browse files
authored
Merge branch 'JavaCourse00:main' into main
2 parents ce8fb32 + e84d39f commit ab506a6

135 files changed

Lines changed: 3462 additions & 317 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ hs_err_pid*
2424
*.iml
2525
*.idea/
2626

27+
out/
2728
classes/
2829
target/
2930
build/

01jvm/AnalysisForList.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class AnalysisForList {
2+
3+
private int[] array = new int[] {1,2,3};
4+
5+
public void testFor() {
6+
for (int i : array) {
7+
System.out.println(i);
8+
}
9+
}
10+
11+
public void testForIndex() {
12+
for (int i=0;i<array.length;i++) {
13+
System.out.println(array[i]);
14+
}
15+
}
16+
17+
public void testForIndex01() {
18+
int len = array.length;
19+
for (int i=0;i<len;i++) {
20+
System.out.println(array[i]);
21+
}
22+
}
23+
24+
}

01jvm/HelloByteCode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class HelloByteCode {
2+
3+
public static void main(String[] args) {
4+
System.out.println(new HelloByteCode());
5+
}
6+
7+
}

01jvm/Hello.java renamed to 01jvm/HelloNum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Hello {
1+
public class HelloNum {
22
public static void main(String[] args) {
33
int num1 = 1; // 字面量1;
44
double num2 = 2.0D; // 大小写的D都可以

01jvm/README.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> Week01 作业题目:
1010
11-
1.(选做)自己写一个简单的 Hello.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。
11+
1.(必做)自己写一个简单的 HelloNum.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。
1212

1313
2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。
1414

@@ -25,13 +25,13 @@
2525
## 操作步骤
2626

2727

28-
### 作业1(选做
28+
### 作业1(必做
2929

30-
1. 编写代码, 根据自己的意愿随意编写, 可参考: [Hello.java](./Hello.java)
31-
2. 编译代码, 执行命令: `javac -g Hello.java`
30+
1. 编写代码, 根据自己的意愿随意编写, 可参考: [HelloNum.java](./HelloNum.java)
31+
2. 编译代码, 执行命令: `javac -g HelloNum.java`
3232
3. 查看反编译的代码。
33-
- 3.1 可以安装并使用idea的jclasslib插件, 选中 [Hello.java](./Hello.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。
34-
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -v Hello.class`
33+
- 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。
34+
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -v HelloNum.class`
3535
4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】
3636

3737

@@ -113,6 +113,36 @@ java -Xmx2g -Xms2g -XX:+UseG1GC -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCD
113113

114114
其中 [GCLogAnalysis.java](./GCLogAnalysis.java) 文件也可以从课件资料zip中找到.
115115

116+
## 几个命令用法
117+
### 1、十六进制方式查看文件
118+
`hexdump -C Hello.class`
119+
输出:`00000000 ca fe ba be 00 00 00 34 00 1c 0a 00 06 00 0e 09`
120+
121+
可以看到magic number: `cafe babe`
122+
以及`00 00 00 34`,十六进制34=十进制3*16+4=52,这是jdk8,如果是jdk11则是55,十六进制37.
123+
124+
### 2、Base64方式编码文件
125+
`base64 Hello.class`
126+
### 3、显示JVM默认参数
127+
```
128+
java -XX:+PrintFlagsFinal -version
129+
130+
java -XX:+PrintFlagsFinal -version | grep -F " Use" | grep -F "GC "
131+
132+
java -XX:+PrintFlagsFinal -version | grep MaxNewSize
133+
134+
```
135+
136+
### 4、切换不同jdk
137+
```
138+
jenv shell 1.8
139+
jenv shell 11
140+
```
141+
显示所有jdk
142+
```
143+
jenv versions
144+
```
145+
116146
## 更多资料
117147

118148
更多中英文的技术文章和参考资料: <https://github.com/cncounter/translation>

01jvm/TestAddUrl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public class TestAddUrl {
88

99
public static void main(String[] args) throws Exception {
1010
URLClassLoader classLoader = (URLClassLoader) TestAddUrl.class.getClassLoader();
11-
String dir = "/Users/kimmking/Downloads/Hello";
11+
String dir = "./lib";
1212
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
1313
method.setAccessible(true);
14-
method.invoke(classLoader, new File(dir).toURL());
14+
method.invoke(classLoader, new File(dir).getAbsoluteFile().toURL());
1515

16-
Class klass = Class.forName("Hello",true, classLoader);
16+
Class klass = Class.forName("HelloKimmking",true, classLoader);
1717
Object obj = klass.newInstance();
1818
Method hello = klass.getDeclaredMethod("hello");
1919
hello.invoke(obj);

01jvm/XlassLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55

66
/*
77
第一周作业:
8-
2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。
8+
2.(必做)自定义一个 Classloader,加载一个 lib.Hello.xlass 文件,执行 hello 方法,此文件内容是一个 lib.Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。
99
*/
1010
public class XlassLoader extends ClassLoader {
1111

1212
public static void main(String[] args) throws Exception {
1313
// 相关参数
14+
final String packageName = "lib";
1415
final String className = "Hello";
1516
final String methodName = "hello";
1617
// 创建类加载器
1718
ClassLoader classLoader = new XlassLoader();
1819
// 加载相应的类
19-
Class<?> clazz = classLoader.loadClass(className);
20+
Class<?> clazz = classLoader.loadClass(packageName + "." + className);
2021
// 看看里面有些什么方法
2122
for (Method m : clazz.getDeclaredMethods()) {
2223
System.out.println(clazz.getSimpleName() + "." + m.getName());

01jvm/java11/src/main/java/com/example/demo/DemoApplication.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

01jvm/jvm/HelloClassLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
public class HelloClassLoader extends ClassLoader {
66

77
public static void main(String[] args) throws Exception {
8-
new HelloClassLoader().findClass("jvm.Hello").newInstance();
8+
9+
new HelloClassLoader().findClass("jvm.lib.Hello").newInstance();
910
}
1011

1112
@Override

01jvm/lib/Hello.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//package lib;
2+
//
3+
//public class Hello {
4+
// static {
5+
// System.out.println("Hello Class Initialized!");
6+
// }
7+
// public void hello() {
8+
// System.out.println("Hello class say hello method.");
9+
// }
10+
//
11+
//}

0 commit comments

Comments
 (0)