In this course, we mainly learned:
Java Development Kit is the core of Java. Which includes JRE, compiler javac and basic class library.
JDK is used for the development of Java programs. The latest version is JDK 16.
Java Runtime Environment provides the runtime environment required by Java programs, including JVM and standard class libraries.
Java Virtual Machine is a fictitious computer. When JVM executes bytecode, it interprets bytecode as machine instructions on a specific platform. Therefore, Java programs can "compile once and run everywhere."
The development and execution process of a Java program is:
There are two main ways to create threads in Java:
如果采用这种方法,我们只需要重写其中的 run() 方法就可以实现我们想要的功能。
If this method is adopted, we only need to rewrite the run() method to achieve the function we want.
Like:
class MyThread implements Runnable{
@Override
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("This is My Thread!");
}
}
}
After declaring an instance of MyThread, call the start() method to start the thread:
public static void main(String[] args){
Thread t = new Thread(new MyThread());
t.start();
}
Another method is to extend the Thread class. We also need to rewrite the run() method.
Like:
class MyThread extends Thread{
@Override
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("This is My Thread!");
}
}
}
After declaring an instance of MyThread, call the start() method to start the thread:
public static void main(String[] args){
Thread t = new MyThread();
t.start();
}
方法 | 功能 |
---|---|
start() | 启动线程,调用 run() 方法 |
yield() | 暂停当前线程,执行其他线程 |
sleep(long millisec) | 让当前线程暂停 millisec 时间 |
currentThread() | 返回当前线程对象的引用 |
Reflection is an important feature of Java. It can access the properties and methods of an object during operation.
One of the most important uses of reflection is to develop a common framework. For example, Spring.
To use reflection, we need to obtain the Class object of the class. This Class object is generated by the JVM, and there is only one Class object for a class.
Java‘s java.lang.Reflect package provides the reflection function.
There are three ways to obtain a Class object:
Class clazz = Class.forName("MyClass");
Class clazz = boolean.class;
Class clazz = myclass.getClass();
We can use the Field, Method, and Constructor classes in java.lang.Reflect to get the members, methods, and constructors of the object.
原文:https://www.cnblogs.com/danielwong2021/p/15229223.html