Java 动态性

动态性

Java本质为静态语言,而不是动态语言。动态语言显著的特点是在程序运行时,可以改变程序结构或变量类型,典型的动态语言有Python、ruby、javascript等。Java不是动态语言,但Java具有一定的动态性,表现在以下几个方面:

l  反射机制;

l  动态字节码操作;

l  动态编译;

l  执行其他脚本代码;

下面我们将介绍反射机制和动态字节码操作等内容。

1、       反射机制

反射机制指的是可以在运行时加载、探知,使用编译期间完全未知的类。我们知道,当类加载完毕后,在堆内存中,就产生了一个Class类型的对象(一个类只有一个Class对象)。这个对象包含了完整的类结构信息,通过该对象就可以获知类的结构信息,如类的方法、属性等。

所以通过一个类的Class对象,我们就可以动态的创建实例、调用方法、访问属性等。当一个类被加载时,JVM便自动产生了一个Class对象,Class对象是反射的根源,使用反射机制的前提是获取类的Class对象。Class的描述如下:

Instances of the class Class represent classes and interfaces in a running Javaapplication. An enum is a kind of class and an annotation is a kind ofinterface. Every array also belongs to a class that is reflected as a Class object that is shared byall arrays with the same element type and number of dimensions. The primitiveJava types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructedautomatically by the Java Virtual Machine as classes are loaded and by calls tothe defineClass method in the classloader.

反射机制常见的操作有:

l   动态加载类、动态获取类的信息,如构造器、方法、属性等;

l   动态构造对象;

l   动态调用类和对象的任意方法(公有和私有)、构造器等;

l   访问任意属性(公有和私有),在访问私有属性时,需要设置setAccessible属性为真,这时候告诉JVM不做安全检查,可以直接反问,而且由于不做安全检查,提高了效率;

l   处理注解;

l   获取泛型信息;

使用反射机制最大的问题就是性能问题,若大量使用反射则性能会大幅下降,如下:

public class Main

{

   public static void main(String[] args)

  {

        test1();

        test2();

        test3();

  }

  

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值