Integer和String比较解析

Integer在遇到运算符时会进行自动拆包。缓存存储的范围为-128~127 和 字符串缓存。

package com.jvmtest;

public class ZhiDongZhuangXiang {
	public static void main(String[] args) {
		Integer a=1;//Integer a = Integer.valueOf(1);
		Integer b=2;
		Integer c=3;
		Integer d=3;
		Integer e=321;
		Integer f=321;
		Long g = 3L;
		System.out.println(c==d);//true Integer.valueOf()
		System.out.println(e==f);//false
		System.out.println(c==(a+b));//true
		System.out.println(c.equals(a+b));//true
		System.out.println(g==(a+b));//true
//		System.out.println(g==c);//编译不通过
		System.out.println(g.equals(a+b));//false
		System.out.println("-----------------");
		Integer k = new Integer(3);
		int h = 321;
		int i = 321;
		System.out.println(h==i);//true
		System.out.println(k==c);//false
		System.out.println(k+1==c+1);//true
		String ab = "1234";
		String ab2 = "1234";
		String ab3 = new String ("1234");
		System.out.println(ab==ab2);//true
		System.out.println(ab==ab3);//false
		System.out.println(ab.equals(ab3));//true
		
		Integer j = new Integer(100);
		int l = 100;
		System.out.print(j == l); //true
		
	}

}

反编译后的代码

package com.jvmtest;

import java.io.PrintStream;

public class ZhiDongZhuangXiang
{
  public static void main(String[] args)
  {
    Integer a = Integer.valueOf(1);
    Integer b = Integer.valueOf(2);
    Integer c = Integer.valueOf(3);
    Integer d = Integer.valueOf(3);
    Integer e = Integer.valueOf(321);
    Integer f = Integer.valueOf(321);
    Long g = Long.valueOf(3L);
    System.out.println(c == d);
    System.out.println(e == f);
    System.out.println(c.intValue() == a.intValue() + b.intValue());
    System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
    System.out.println(g.longValue() == a.intValue() + b.intValue());
    
    System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
    System.out.println("-----------------");
    Integer k = new Integer(3);
    int h = 321;
    int i = 321;
    System.out.println(h == i);
    System.out.println(k == c);
    System.out.println(k.intValue() + 1 == c.intValue() + 1);
    String ab = "1234";
    String ab2 = "1234";
    String ab3 = new String("1234");
    System.out.println(ab == ab2);
    System.out.println(ab == ab3);
    System.out.println(ab.equals(ab3));
    
    Integer j = new Integer(100);
    int l = 100;
    System.out.print(j.intValue() == l);
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值