.JVM运行过程(. JVM runtime)

7.JVM运行过程
类在JVM的执行流程共有三步:加载、链接和初始化。
加载
JVM将java类的二进制形式加载到内存中,并将他缓存在内存中,以便后面使用,如果没有找到指定的类就会抛出异常classNotFound,进程在这里结束。没有错误就继续在Java堆中生成一个代表这个类的java.lang.Class对象,作为方法区域数据的访问入口。
链接
这个阶段做三件事:验证、准备和解析(可选)。
验证是JVM根据java语言和JVM的语义要求检查这个二进制形式。例如,如果篡改经过编译后的类文件,那么这个类文件可能就不能使用了。
准备是指准备要执行的指定的类,准备阶段为变量分配内存并设置静态变量的初始化。在这个阶段分配的仅为类的变量(static修饰的变量),而不包括类的实例变量。对非static的变量,JVM会将其设置成“零值”,而不是其赋值语句的值:
public static int num = 8;
那么在这个阶段,num的值为0,而不是8。 final修饰的类变量将会赋值成真实的值。
解析是检查指定的类是否引用了其他的类/接口,是否能找到和加载其他的类/接口。这些检查将针对被引用的类/接口递归进行,JVM的实施也可以在后面阶段执行解析,即正在执行的代码真正要使用被引用的类/接口的时候。
初始化
在这最后一步中,JVM用赋值或者缺省值将静态变量初始化,初始化发生在执行main方法之前。在指定的类初始化前,会先初始化它的父类,此外,在初始化父类时,父类的父类也要这样初始化。这个过程是递归进行的。
简而言之,整个流程是将类存进内存中,检查类的对应调用的类和接口是否可正常使用,再对类进行初始化的过程。
8.非对称加密和对称加密
对称加密采用了对称密码编码技术,它的特点是文件加密和解密使用相同的密钥加密,也就是密钥也可以用作解密密钥,这种方法在密码学中叫做对称加密算法。对称加密算法使用起来简单快捷,密钥较短,且破译困难。常见的对称加密算法有DES、3DES、Blowfish、IDEA、RC4、RC5、RC6和AES 。
与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。常见的非对称加密算法有:RSA、ECC(移动设备用)、Diffie-Hellman、El Gamal、DSA(数字签名用)。

Hash算法特别的地方在于它是一种单向算法,用户可以通过hash算法对目标信息生成一段特定长度的唯一hash值,却不能通过这个hash值重新获得目标信息。因此Hash算法常用在不可还原的密码存储、信息完整性校验等。常见的Hash算法有MD2、cf MD4、MD5、HAVAL、SHA。

9.spring事务写在哪一部分,为什么不写在DAO, Controller层
Spring的AOP的连接点是方法级的
因为一个Service完成一个服务,但是可能会调用很多个DAO层的功能,如果Transaction放在DAO层的话,做完一个DAO,就会提交一次事务,永久修改数据库,后面在调用另外一个DAO,但是throws Exception,对于整个的Service来说,应该是要完全回滚的,但是只能回滚到当前的DAO,所以这就破坏了事务的ACID。
10.数据库驱动为什么使用反射调用不直接new
使用反射的一个很重要的原因是解耦。首先要明白JDBC是Java的一种规范,通俗一点说就是JDK在java.sql.*下提供了一系列的接口(interface),但没有提供任何实现(也就是类)。 所以任何人都可以在接口规范之下写自己的JDBC实现(如MySQL)。而若调用者也只调用接口上的方法(如我们),那么当未来有任何变更需要时(例如要从MySQL迁移至Oracle),则理论上不需要对代码做任何修改就能直接切换(可惜SQL语法没能统一规范)。

————————

7. JVM operation process
The execution process of class in JVM consists of three steps: loading, linking and initialization.
load
The JVM loads the binary form of the Java class into memory and caches it in memory for later use. If the specified class is not found, an exception classnotfound will be thrown, and the process ends here. If there is no error, continue to generate a Java. Net file representing this class in the Java heap Lang. class object, as the access entry of method area data.
link
This phase does three things: validation, preparation, and parsing (optional).
Verification is that the JVM checks the binary form according to the Java language and the semantic requirements of the JVM. For example, if you tamper with a compiled class file, the class file may not be available.
Preparation refers to preparing the specified class to be executed. In the preparation phase, allocate memory for variables and set the initialization of static variables. At this stage, only class variables (static modified variables) are allocated, not class instance variables. For non static variables, the JVM will set them to “zero value” instead of the value of their assignment statement:
public static int num = 8;
Then at this stage, the value of num is 0 instead of 8. The class variable decorated with final will be assigned a real value.
Parsing is to check whether the specified class references other classes / interfaces, and whether other classes / interfaces can be found and loaded. These checks will be performed recursively for the referenced class / interface, and the JVM implementation can also perform parsing at a later stage, that is, when the executing code really needs to use the referenced class / interface.
initialization
In this last step, the JVM initializes the static variable with an assignment or default value, which occurs before the main method is executed. Before the specified class is initialized, its parent class will be initialized. In addition, when initializing the parent class, the parent class of the parent class will also be initialized in this way. This process is recursive.
In short, the whole process is to store the class in memory, check whether the corresponding calling class and interface of the class can be used normally, and then initialize the class.
8. Asymmetric encryption and symmetric encryption
Symmetric encryption adopts symmetric cipher coding technology. Its characteristic is that file encryption and decryption use the same key encryption, that is, the key can also be used as decryption key. This method is called symmetric encryption algorithm in cryptography. Symmetric encryption algorithm is simple and fast to use, the key is short, and it is difficult to decipher. Common symmetric encryption algorithms include DES, 3DES, blowfish, idea, RC4, RC5, RC6 and AES.
Unlike symmetric encryption algorithms, asymmetric encryption algorithms require two keys: public key and private key. The public key and the private key are a pair. If the data is encrypted with the public key, only the corresponding private key can be decrypted; If the data is encrypted with a private key, it can only be decrypted with the corresponding public key. Because encryption and decryption use two different keys, this algorithm is called asymmetric encryption algorithm. Common asymmetric encryption algorithms include RSA, ECC (for mobile devices), Diffie Hellman, El Gamal and DSA (for digital signature).

Hash算法特别的地方在于它是一种单向算法,用户可以通过hash算法对目标信息生成一段特定长度的唯一hash值,却不能通过这个hash值重新获得目标信息。因此Hash算法常用在不可还原的密码存储、信息完整性校验等。常见的Hash算法有MD2、cf MD4、MD5、HAVAL、SHA。

9. Where is the spring transaction written and why is it not written in the Dao and controller layer
Spring’s AOP connection points are method level
Because a service completes a service, but it may call the functions of many Dao layers. If the transaction is placed in the Dao layer, after completing a Dao, it will commit a transaction and permanently modify the database. Later, it will call another Dao. However, the throws exception should be rolled back completely for the whole service, but it can only be rolled back to the current Dao, So this destroys the acid of the transaction.
10. Why does the database driver not directly call new using reflection
A very important reason for using reflection is decoupling. First of all, you should understand that JDBC is a Java specification. Generally speaking, JDK is in Java sql.* The following provides a series of interfaces, but does not provide any implementation (that is, classes). Therefore, anyone can write his own JDBC implementation (such as MySQL) under the interface specification. If the caller only calls the methods on the interface (such as US), when there is any need for change in the future (for example, migrating from Mysql to Oracle), it can be switched directly without any modification to the code in theory (unfortunately, the SQL syntax is not standardized).