Integer和Integer常量池】的更多相关文章

抛出样例: Integer a1  = new Integer(123);        Integer a2  = new Integer(123);        System.out.println(a1 == a2);   //false  因为只要遇到new关键词,肯定会在堆里面重新分配空间给实例对象,所以两个地址肯定不同                a1  = 123;        a2  = 123;        System.out.println(a1 == a2); …
public class Test{ Integer i1 = 127 ; // 常量池本来就有,直接饮用常量池. String s1 = new String("aaaa"); //首先会检查常量池,如果常量池没有 aaaa,则在常量池创建 aaaa, 然后拷贝一份到堆中,其中 s1 是堆中字符串的引用. String s2 = new String("aaaa"); //检测到常量池中有 aaaa,直接饮用常量池.s2 为创建在栈中指向字符串的引用. Integ…
要说Java中的栈,堆,方法区和常量池就要提到HotSpot,HotSpot是Sun JDK 和 Open JDK中所带的虚拟机. (Sun JDK 和 Open JDK除了注释不同,代码实现基本上是一样的) 以下说的内容都是围绕HotSpot. Stack(栈):分为VM Stack(虚拟机栈)和Native Method Stack(本地方法栈),不过HotSpot虚拟机直接把本地方法栈和虚拟机栈合二为一了. 虚拟机栈: 线程私有的, 描述的是Java方法执行的内存模型,方法调用的同时创建一…
举例: public class Test { @org.junit.Test public void intTest() { Integer t1 = 128; Integer t2 = 127; } } 使用 javap -c 查看字节码 public void intTest(); Code: 0: sipush 128 3: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 6: asto…
一个Java question,求输出结果   public class IntegerTest { public static void main(String[] args) { objPoolTest(); } public static void objPoolTest() { Integer i1 = 40; Integer i2 = 40; Integer i3 = 0; Integer i4 = new Integer(40); Integer i5 = new Integer(4…
我们先看一个关于Integer的例子 public static void main(String[] args) { // TeODO Auto-generated method stu Integer i1 = 40; Integer i2 = 40; Integer i3 = 0; Integer i4 = new Integer(40); Integer i5 = new Integer(40); Integer i6 = new Integer(0); System.out.print…
1.Integer的常量池 看下面一段代码: package cn.qlq.test; public class ArrayTest { public static void main(String[] args) { Integer i1 = new Integer(1); Integer i2 = new Integer(1); System.out.println(i1.hashCode()); System.out.println(i2.hashCode()); System.out.p…
Integer中有个静态内部类 IntegerCache ,里面有个cache[],也就是Integer常量池  大小为一个字节(-128-127). (jdk1.8.0_101)源码 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configure…
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 先普及一个基本概念:Java中基本数据类型的装箱和拆箱操作 自动装箱 在JDK5以后,我们可以直接使用Integer num = 2:来进行值的定义,但是你有没有考虑过?Integer是一个对象呀,为什么我可以不实例化对象,就直接来进行Value的定义呢? 一般情况下我们在定义一个对象的时候,顶多赋值为一个null 即空值: 比如:Person pserson = null:但是肯定不可…