请教高手:Integer与int区别 ,另外发帖后该怎么怎么结贴。
public class Test2 { public static void main(String[] args) {
int a=120; 
int b=120; 
System.out.println(a==b); 

int c=150; 
int d=150; 
System.out.println(c==d);
}
}
//结果 true
      true   public class Test2 { public static void main(String[] args) {
Integer a=120; 
Integer b=120; 
System.out.println(a==b); 

Integer c=150; 
Integer d=150; 
System.out.println(c==d);
}
}
//结果 true
      false
public class Test2 { public static void main(String[] args) {
Integer a=120; 
Integer b=120; 
System.out.println(a==b); 

Integer c=150; 
Integer d=c; 
System.out.println(c==d);
}
}//结果 true
      true

解决方案 »

  1.   

    int 基本类型
    Integer 封装类是个对象结贴就点  结贴去...  
      

  2.   

    int是JAVA的一个基本类型,而Integer是JAVA的一个类,对应 int。因为在某些地方不可以用int而要用Integer。而且基本类型运算的速度也要快。 
    int 是变量的基本类型 
    Integer 是int的外覆类型 
    “基本类型有所谓的‘外覆类(wrapper classes)’如果你想在heap内产生用以代表该基本类型的非原始对象(nonprimitive object),那么外覆类型就可派上用场。” 
    引自《Thinking in Java》
      

  3.   

    int 基本类型 
    Integer 是int的封装类
      

  4.   

    int原始数据类型,==是比较两个值是否相等
    Integer是int的封装对象,==是比较两个引用地址是否为同一个内存地址
      

  5.   

    http://blog.csdn.net/ZangXT/archive/2008/11/19/3334030.aspx
      

  6.   

    int 基本类型 
    Integer 封装类是个对象 好像128是个自动转型的临界点,从 core java那里看来的
      

  7.   

    首先int是原始数据类型,==比较的是值,所以只要值相等就返回true
    Integer是引用类型,==比较的是引用指向的内存地址是否相同,相同就返回true,否则返回false
    Integer i = 150;
    编译器调用Integer.valueOf(150)来生成Integer对象,Integer类对-128到127这些数预生成了缓存对象
    在这些范围之内调用多次Integer.valueOf其实返回的是同一个内存地址的引用
    而在这些范围之外则通过new Integer()的方式在堆中分配不同的内存地址
      

  8.   


    答:Integer i=120; 等价于:  Integer i=Integer.valueOf(120);
    而:Integer类中valueOf(int v)的实现代码是://-128 至 127 要缓存. public static Integer valueOf(int i) {
            final int offset = 128;
            if (i >= -128 && i <= 127) { // must cache
                return IntegerCache.cache[i + offset];
            }
            return new Integer(i);
        }
    从代码中你就能看出:只有超出-128 至 127 范围的数,才会每一个i都会new Integer(..)一下的.
      

  9.   

    int 是基本类型Integer是包装类比较两个Integer的值应该用equals方法。而且Integer是个final类,自加自减之后是不起作用的。
    例如:
    Integer i=new Integer(0);
    i++;//这是不起作用的
    i=i+1;//这样才能起作用
      

  10.   

    int是原始数据类型..值都是保存在内存中的栈中
    Integer是原始数据类型的封装类..是分配在内村的堆中..也可以说一个是值传递..一个是引用传递
      

  11.   

    int 是基本数据类型
    Integer  是个包装类  
      
      

  12.   

    Integer is Object
    int is primitive type
      

  13.   

    Integer  a=3;
    Integer  b=3;
    等价于Integer.valueof(3)查看源文件你会发现这样做会使用该类中的静态内部类,她已经对-128到127之间的数进行缓存,调用
    valueOf返回的是该静态类中的Integer而不是新创建的,所以二者比较相等。另外对整形的包装类都会对-128到127之间的数进行
    缓存。
      

  14.   

    int 基本类型 
    Integer 是int的封装类
    基本就是这个区别
      

  15.   

    基本类型----对应-----包装类(相关的自动装箱和自动拆箱)
    int------------------Integer
    double---------------Double
    float----------------Float
    ...........
    呵,我也新手,学习中...
      

  16.   

    int Integer 
    char Charactor
    boolean Boolean
    double Double
    .
    .
    .
      

  17.   

    int是基础类型,而Integer 是对相类型,
    int变量之间用==比较时,是比较二者的数值是否相同,Integer对相之用==比较时是比较引用对相的内存地址是否相同,要比较数值就要使用equals关键字
      

  18.   

    正解,学到了。。
       public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
        }看了Integer类说明····这里可以看出