在java中,这样的类类型是什么编译? [英] In java, what does such class type compile to?

查看:206
本文介绍了在java中,这样的类类型是什么编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是定义 class 类型的代码:

 注释类型; 

public class Example {

public static void main(String [] args){


}
}

通过 javac / p>

  public class annotationtype.Example {
public static Class< annotationtype.Example>类;
{
class = Class.forName(annotationtype.Example)
}
public annotationtype.Example(){}
public static void main(java.lang .String [] args){}
}

我的主要焦点是 Class< annotationtype.Example> class 静态成员变量在上面的代码中。此外,此成员变量 Class< annotationtype.Example>类实际上指向类型类Class 的对象,该对象维护元数据 class Example after class Example 被加载到内存中。



我的理解是否正确?

解决方案

JLS中所述的语言规范15.8.2


类文字是一个由类,接口,数组或基本类型的名称组成的表达式,或伪类型void,后跟一个'。'和令牌类。



C.class的类型,其中C是类的名称,接口或
数组类型(§4.3)是类< C>。



p.class的类型,其中p是原语的名称类型(§4.2),
是类,其中B是在
转换后的类型p的表达式的类型(§5.1.7)。



void.class(§8.4.5)的类型是Class


javac 不会为每个类创建一个静态的字段,但它会识别一个类文字表达式并正确编译。 / p>

以类为例:

  public class Hello {
public static void main(String [] args){
Class<?> myClass = Hello.class;
System.out.println(Hello,+ myClass);
}
}

这将编译为(仅包含字节码的相关部分) :

  public class Hello minor version:0 major version:52 flags:
ACC_PUBLIC,ACC_SUPER
pool:
#1 = Methodref#11.#20 // java / lang / Object。< init>:()V
#2 = Class#21 // Hello
......
public static void main(java.lang.String []);
descriptor:([Ljava / lang / String;)V
标志:ACC_PUBLIC,ACC_STATIC
代码:
stack = 3,locals = 2,args_size = 1
0:ldc#2 //类Hello
2:astore_1

code> javac 在常量池中引用了Hello类,然后在 main 中引用它时加载该常量。 。


Below is the code that defines class type:

package annotationtype;

public class Example {

    public static void main(String[] args){


    }
}

that gets functionally compiled by javac to:

public class annotationtype.Example{
    public static Class<annotationtype.Example> class;
    {
        class = Class.forName("annotationtype.Example")
    }
    public annotationtype.Example(){}
    public static void main(java.lang.String[] args){}
}

My major focus is on the Class<annotationtype.Example> class static member variable in the above code. In addition, this member variable Class<annotationtype.Example> class is actually pointing to an object of type class Class that maintains meta data of class Example after class Example gets loaded into memory.

Is my understanding correct?

解决方案

Class literals are part of the language specification as noted in JLS 15.8.2

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.

The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.

The type of p.class, where p is the name of a primitive type (§4.2), is Class<B>, where B is the type of an expression of type p after boxing conversion (§5.1.7).

The type of void.class (§8.4.5) is Class<Void>.

javac doesn't create a static class field for each class, but it will recognize a class literal expression and compile it correctly.

Take for example the class:

public class Hello {
        public static void main(String[] args){
                Class<?> myClass = Hello.class;
                System.out.println("Hello, " + myClass);
        }
}

This compiles to (only relevant sections of bytecode included):

public class Hello   minor version: 0   major version: 52   flags:
 ACC_PUBLIC, ACC_SUPER 
Constant pool:    
#1 = Methodref          #11.#20        // java/lang/Object."<init>":()V    
#2 = Class              #21            // Hello
......
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
  stack=3, locals=2, args_size=1
     0: ldc           #2                  // class Hello
     2: astore_1

You can see that javac has put a reference to the Hello class in the constant pool and then loaded that constant when I refer to it in main.

这篇关于在java中,这样的类类型是什么编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆