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);
  }
}


你可能感兴趣的:(Java)