码迷,mamicode.com
首页 > 编程语言 > 详细

java 高级用法整理

时间:2018-06-21 13:31:44      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:tps   error   级别   not   逻辑   对象引用   retention   www.   功能   

一、retentionpolicy.class vs runtime区别

java5,增加了注解的功能;其中retentionpolicy注解的生命周期,提供了三种选择策略 source、class和runtime三种选择;

source:源码级别的,主要是方便程序上查看;它的生命周期是编译后,就消亡了,不会存留在classs文件中

class:编译级别的,字节码文件中会留存。生命周期在类加载到虚拟机的时候,消亡;所以它不会加载到java虚拟机中。不过他可以通过aspectj、becl(asm)等字节码库去使用,从而达到修改字节码的效果

runtime:运行时级别;可以被加载到虚拟机中,因此可以通过java反射获取到

参考:

1、https://stackoverflow.com/questions/5971234/retentionpolicy-class-vs-runtime

2、关于becl的用法:https://blog.csdn.net/garfielder007/article/details/56319242

二、String.intern()方法

intern 逻辑判断:当调用 intern 方法时,如果常量池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用

参考1中,有段文字,是这样写的

Invoking intern method on a string object, either creates a new object that enters the intern-pool or return an existing object from the pool that has same value. The invocation on any object which is not in the intern-pool, does NOT move the object to the pool. It rather creates another object that enters the pool.

这是基于JKD1.7之前的,JDK7就做了优化,常量池支持对象引用的存储;因此如果常量池没有equals 对于此字符串对象的,将会将字符串对象的引用加入到常量池中;

其次关注下:JDK7,将常量池从永生区转移到了堆中,避免java.lang.OutOfMemoryError: PermGen space错误

 

参考:

1、https://stackoverflow.com/questions/1855170/when-should-we-use-intern-method-of-string-on-string-literals

2、https://www.cnblogs.com/Kidezyq/p/8040338.html

3、https://tech.meituan.com/in_depth_understanding_string_intern.html

 

java 高级用法整理

标签:tps   error   级别   not   逻辑   对象引用   retention   www.   功能   

原文地址:https://www.cnblogs.com/draem0507/p/9208209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!