- public static void main(String[] args) {
- Integer a=200 ;
- Integer b=200 ;
- int c= 200 ;
- System.out.println("a==c " +(a==c));
- System.out.println("b==c " +(b==c));
- System.out.println("a==b " +(a==b));
- }
请问LZ,对于这段代码的运行结果是:
- a==c true
- b==c true
- a==b false
既然a和b都是通过Integer新建的对象,而c是在栈中的常量,为什么a、b会和c的地址相等,而a和b的地址又不相等呢?
我绝对大家要学会看.class文件
Integer a=200;
int c=200;
System.out.println("a==c");
被编译器这样编译:
Integer a=Integer.valueOf(200); 这个时候引用a存储的是堆栈中Integer对象的地址 (注意a是对象引用)
int c=200; 编译器把常量200写进常量池,然后栈中的变量c里面存放的就是这个值200 (注意c是变量名)
int temp=a.intValue(); 大家可以去看看intValue()的源代码,实际上编译器并没有temp这样的变量,而直接把intValue()的返回结构压入操作数栈。我这些写实为了大家明白过程(不存在temp的).
System.out.println(temp==c);
大致的过程就是这样
总结一下:
(1) 当把一个整形常量或变量直接赋值给Integer引用时,比如 Integer a=200, 或 int b=200; Integer a=b;
这是编译器会调用Integer.valueOf()方法来自动打包int基本类型。
(2) 当比较一个Integer对象和一个int基本类型变量的时候,编译器会调用Integer对象.intValue()将Integer对象自动拆包,然后比较两个整形变量。
注意编译器给我们做了很多工作,大家不要忘记了他的辛苦。要把它的汗水记得明明白白它的心!!
相关推荐
Transform Coding with Integer-to-Integer Transforms.pdf H.264學習資料
HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...
Clock_Integer 指令库应用例程rar,Clock_Integer 指令库应用例程
HugeInteger.java
bigdecimal转integer
mybatis返回int会报错,改成Integer封装类型可以解决,具体参考我的博客
由于使用模板,所以只有一个Integer.h头文件 支持如下操作:>, >=, <, , ==, !=, +, +=, -, -=, *, =, /, /=,(输出);支持利用基本整数类型(int,unsigned,long long,……)和字符串(char和std::string)构造类型
Gaussian Integer
创建一个大整数类HugeInteger,该类用一个40个元素的数组来存放一个大整数(最多不超过40位)。 构造函数原型: HugeInteger(String); (1)定义几个大整数算术运算的成员函数,包括input、output、add和sub, add, ...
Logic and Integer Programming
北大POJ1503-Integer Inquiry 解题报告+AC代码
Mybatis Generator将tinyint映射成Integer的解决办法
c++ 大整数库 big integer 来自https://mattmccutchen.net/bigint/
good book in integer programming
前端项目-big-integer,An arbitrary length integer library for Javascript
leetcode上Roman to Integer的完整C++代码,已被accepted
mybatis逆向工具generator,中文注释,把自动生成的Byte改成了Integer
在一些需求中,我们可能需要将 BigDecimal 值转换为 Integer 类型。这时,可以使用 BigDecimal 的 intValue() 方法进行转换。 当 BigDecimal 值小于或等于 Integer.MAX_VALUE(2^31-1)时,intValue() 方法返回 ...
C++实现的大整数(big integer)类的源代码。
[C++]大数Integer类。支持大数加减乘除。支持如下操作:>, >=, <, , ==, !=, +, +=, -, -=, *, =, /, /=,(输出);支持利用基本整数类型(int,unsigned,long long,……)和字符串(char和std::string)构造类型;支持取...