为什么注释字符串值不被暂存?
2022-09-02 22:13:14
						以下代码段打印 4 个不同的哈希代码,尽管重用字符串常量和文本。为什么字符串值不在注释元素上暂存?
public class Foo {
    @Retention(RetentionPolicy.RUNTIME)
    @interface Bar {
        String CONSTANT = "foo";
        String value() default CONSTANT;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(System.identityHashCode(Bar.CONSTANT));
        System.out.println(System.identityHashCode(Foo.class.getMethod("test1").getAnnotation(Bar.class).value()));
        System.out.println(System.identityHashCode(Foo.class.getMethod("test2").getAnnotation(Bar.class).value()));
        System.out.println(System.identityHashCode(Foo.class.getMethod("test3").getAnnotation(Bar.class).value()));
    }
    @Bar
    public void test1() {}
    @Bar("foo")
    public void test2() {}
    @Bar(Bar.CONSTANT)
    public void test3() {}
}
 
					 
				 
				    		 
				    		 
				    		 
				    		