Java若干特性

Featured image

类加载机制

类加载器

用于加载类文件(一般是指class字节码文件,但实际上也可能是动态生成的或者网络传输来的)的类。其作用就是在运行时加载类,将class文件转为Class对象(并不是实例对象),Java类加载器的三个机制:

每一个类对象都对应一个类加载器;虚拟机判定两个类是否相同的依据是:类全名相同并且两个类的类加载器相同(执行definedClass的类加载器,而不是loadClass的加载器)即便是同一类型加载器的两个不同实例,也会认为是不同的类。这也是采用委托机制的原因,目的是保证核心库的安全;

在虚拟机内部,不同的类加载器加载类时会赋予类一个额外的名称空间,相同的类可以通过不同的类加载器加载,并存于java虚拟机中;但是二者不兼容,不可转换;这在许多框架中被用到;

四种预定义类型的类加载器:

开发者可以通过继承java.lang.ClassLoader的方式实现自己的类加载器,,一般来说只需要重写findClass方法即可,调用的时候,仍然调用loadClass;最好不要复写loadClass方法,有可能会破坏代理模式;

动态代理

反射机制

Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.

通过java代码获取已加载类的属性、方法、构造函数、注解等,并且可以使用他们。简单来说,在运行状态可以做到:

For every type of object, the Java virtual machine instantiates an immutable instance of java.lang.Class which provides methods to examine the runtime properties of the object including its members and type information. Class also provides the ability to create new classes and objects. Most importantly, it is the entry point for all of the Reflection APIs.

获取Class的几种方式:

Reflection defines an interface java.lang.reflect.Member which is implemented by java.lang.reflect.Field, java.lang.reflect.Method, and java.lang.reflect.Constructor .

Member接口的三个实现类:

反射的副作用: