首页 > 编程知识 正文

java中integer最大值,int与integer的用法

时间:2023-05-03 12:35:33 阅读:146730 作者:1321

全部展开

java Integer和int之间==的比较问题。 3231313353236313431303231303231363533 e 78988 e69 d 833133365643662要求说明

publicstaticvoidmain (字符串[ ] args ) {

//todo自动- generated method stub

integera=newinteger(1;

integerb=newinteger(1;

int c=1;

Integer e=1;

system.out.println(a==b: ) ) a==b );

system.out.println(a==c: ) ) a==c );

system.out.println(a==e: ) ) a==e );

system.out.println(c==e: ) ) c==e );

}

结果:

a==b:false

a==c:true

a==e:false

c==e:true匿名用户提问

最佳答案由提问者推荐

Integer是封装的int对象,两个对象==比较堆栈的值

integera=newinteger(1;

integerb=newinteger(1;

a和b存储Integer堆中的地址,而不是值

a、b堆内的地址明显不同,因此a==b为false

int c=1; int是值类型,引用类型Integer与值类型int进行比较,明显是比较值

堆栈的值是自己的值,因为int不会在堆中打开内存

因此,a==c比较各自的值,a==c为真

Integer e=1; 这是特殊的,直接分配有独立的内存,每次分配时检查内存中是否有与自己一致的值,如果有,向e支付其内存地址,如果没有,打开新的内存

请尝试以下示例:

Integer t=1;

Integer t1=1;

t==t1为true,如上所述,此时t和t1指向相同的存储器

new一定会开发新内存,直接分配值不一定会开发新内存

a的引用指向堆,e指向他专用的内存,因此他们的内存地址不同

所以a==e是false

c==e表示a==c,与引用类型和值类型相同

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。