Integera=127; //Integer.valueOf(127) Integerb=127; //Integer.valueOf(127) log.info("\nInteger a = 127;\nInteger b = 127;\na == b ? {}", a == b); // true
Integerc=128; //Integer.valueOf(128) Integerd=128; //Integer.valueOf(128) log.info("\nInteger c = 128;\nInteger d = 128;\nc == d ? {}", c == d); //false //设置-XX:AutoBoxCacheMax=1000再试试
Integere=127; //Integer.valueOf(127) Integerf=newInteger(127); //new instance log.info("\nInteger e = 127;\nInteger f = new Integer(127);\ne == f ? {}", e == f); //false
Integerg=newInteger(127); //new instance Integerh=newInteger(127); //new instance log.info("\nInteger g = new Integer(127);\nInteger h = new Integer(127);\ng == h ? {}", g == h); //false
Integeri=128; //unbox intj=128; log.info("\nInteger i = 128;\nint j = 128;\ni == j ? {}", i == j); //true
static { // high value may be configured by property inth=127; StringintegerCacheHighPropValue= sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { inti= parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h;
Stringa="1"; Stringb="1"; log.info("\nString a = \"1\";\nString b = \"1\";\na == b ? {}", a == b); //true
Stringc=newString("2"); Stringd=newString("2"); log.info("\nString c = new String(\"2\");\nString d = new String(\"2\");\nc == d ? {}", c == d); //false
Stringe=newString("3").intern(); Stringf=newString("3").intern(); log.info("\nString e = new String(\"3\").intern();\nString f = new String(\"3\").intern();\ne == f ? {}", e == f); //true
Stringg=newString("4"); Stringh=newString("4"); log.info("\nString g = new String(\"4\");\nString h = new String(\"4\");\ng == h ? {}", g.equals(h)); //true
Exception in thread "main" java.lang.ArithmeticException: / by zero at io.github.dunwu.javacore.exception.RumtimeExceptionDemo01.main(RumtimeExceptionDemo01.java:6)
Exception in thread "main" io.github.dunwu.javacore.exception.MyExceptionDemo$MyException: 自定义异常 at io.github.dunwu.javacore.exception.MyExceptionDemo.main(MyExceptionDemo.java:9)
// 反射获取 digits 方法成功 java.lang.NoSuchMethodException: java.lang.String.toString(int) at java.lang.Class.getMethod(Class.java:1786) at io.github.dunwu.javacore.exception.ThrowsDemo.f1(ThrowsDemo.java:12) at io.github.dunwu.javacore.exception.ThrowsDemo.f2(ThrowsDemo.java:21) at io.github.dunwu.javacore.exception.ThrowsDemo.main(ThrowsDemo.java:30)
Exception in thread "main" io.github.dunwu.javacore.exception.ExceptionChainDemo$MyException2: 出现 MyException2 at io.github.dunwu.javacore.exception.ExceptionChainDemo.f2(ExceptionChainDemo.java:29) at io.github.dunwu.javacore.exception.ExceptionChainDemo.main(ExceptionChainDemo.java:34) Caused by: io.github.dunwu.javacore.exception.ExceptionChainDemo$MyException1: 出现 MyException1 at io.github.dunwu.javacore.exception.ExceptionChainDemo.f1(ExceptionChainDemo.java:22) at io.github.dunwu.javacore.exception.ExceptionChainDemo.f2(ExceptionChainDemo.java:27) ... 1 more
===========PrintallColor=========== RED ordinal:0 GREEN ordinal:1 BLUE ordinal:2 ===========PrintallSize=========== BIG ordinal:0 MIDDLE ordinal:1 SMALL ordinal:2 green name():GREEN green getDeclaringClass():classorg.zp.javase.enumeration.EnumDemo$Color green hashCode():460141958 green compareTo Color.GREEN:0 green equals Color.GREEN:true green equals Size.MIDDLE:false green equals 1:false green==Color.BLUE:false
枚举的特性
枚举的特性,归结起来就是一句话:
除了不能继承,基本上可以将 enum 看做一个常规的类。
但是这句话需要拆分去理解,让我们细细道来。
基本特性
如果枚举中没有定义方法,也可以在最后一个实例后面加逗号、分号或什么都不加。
如果枚举中没有定义方法,枚举值默认为从 0 开始的有序数值。以 Color 枚举类型举例,它的枚举常量依次为 RED:0,GREEN:1,BLUE:2。
java.lang.reflect 包下主要包含一些实现反射功能的工具类。实际上,java.lang.reflect 包所有提供的反射 API 扩充了读取运行时注解信息的能力。当一个注解类型被定义为运行时的注解后,该注解才能是运行时可见,当 class 文件被装载时被保存在 class 文件中的注解才会被虚拟机读取。 AnnotatedElement 接口是所有程序元素(Class、Method 和 Constructor)的父接口,所以程序通过反射获取了某个类的AnnotatedElement 对象之后,程序就可以调用该对象的如下四个个方法来访问注解信息:
Sets the maximum permanent generation space size (inbytes). This option was deprecated in JDK 8, and superseded bythe -XX:MaxMetaspaceSize option.
-XX:PermSize=size
Sets thespace (inbytes) allocated tothe permanent generation that triggers a garbage collection ifit is exceeded. This option was deprecated un JDK 8, and superseded bythe -XX:MetaspaceSize option.
Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded. This threshold for a garbage collection is increased or decreased depending on the amount of metadata used. The default size depends on the platform.
-XX:MaxMetaspaceSize=size
Sets the maximum amount of native memory that can be allocated for class metadata. By default, the size is not limited. The amount of metadata for an application depends on the application itself, other running applications, and the amount of memory available on the system.
以下示例显示如何将类类元数据的上限设置为 256 MB:
XX:MaxMetaspaceSize=256m
字节码问题
ASM 5.0 beta 开始支持 JDK8
字节码错误
1 2
Caused by: java.io.IOException: invalid constant type: 15 at javassist.bytecode.ConstPool.readOne(ConstPool.java:1113)
说明:logger.debug("Processing trade with id: " + id + " and symbol: " + symbol); 如果日志级别是 warn,上述日志不会打印,但是会执行字符串拼接操作,如果 symbol 是对象,会执行 toString()方法,浪费了系统资源,执行了上述操作,最终日志却没有打印。
正例:(条件)
1 2 3
if (logger.isDebugEnabled()) { logger.debug("Processing trade with id: " + id + " and symbol: " + symbol); }
正例:(占位符)
1
logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/schacon/.ssh/id_rsa): Created directory '/home/schacon/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/schacon/.ssh/id_rsa. Your public key has been saved in /home/schacon/.ssh/id_rsa.pub. The key fingerprint is: d0:82:24:8e:d7:f1:bb:9b:33:53:96:93:49:da:9b:e3 schacon@mylaptop.local