what is difference between integer a = 5 and new Integer(5)?

前端 未结 5 869
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 22:33

if i write below code(in java):

Integer a =new Integer(5);
Integer b=new Integer(5);
if(a==b){
  System.out.println(\"In ==\");
}
if(a.equals(b)){
 System.o         


        
相关标签:
5条回答
  • 2020-12-05 22:46

    In Java, you should never use the new Integer, even though it is valid syntax it is not the best way to declare integers as you have found out. Use instead Integer.valueOf(int) This has multiple advantages.

    You are not creating extra objects needlessly. Whenever you use the new operator you are forcing the vm to create a new object which is not needed in most cases. valueOf(int) will return a cached copy. Since Integer objects are immutable this works great. This means that you can use == though in practice you should use a null safe compare like in Apache ObjectUtils

    The == operator tests for equality. References are only equal when they refer to the same object in memory. equals method ensures 2 object instances are 'equivalent' to each other.

    Integer a = new Integer(5);
    Integer b = a;
    
    a == b; // true!
    a.equals(b); // true
    b = new Integer(5);
    a == b; // false
    a.equals(b); // true
    

    Primitives are equal whenever their value is the same.

    int a = 5; int b = a;
    a == b;  // true!
    
    0 讨论(0)
  • 2020-12-05 22:54

    Integer a = 5; is called autoboxing, compiler converts this expression into actual

    Integer a = Integer.valueOf(5);
    

    For small numbers, by default -128 to 127, Integer.valueOf(int) does not create a new instance of Integer but returns a value from its cache. So here

    Integer a = 5;
    Integer b= 5;
    

    a and b point to the same Object and a == b is true.

    0 讨论(0)
  • 2020-12-05 22:57

    Integer wrapper shares few properties of String class. In that it is immutable and that can be leveraged by using intern() like functionality.

    Analyse this:

    String a = "foo";
    String b = "foo";
    String c = new String("foo");
    String d = new String("foo");
    
    a == b   //true
    c == d   //false
    

    The reason is when JVM creates a new String object implicitly it reuses existing String object which has the same value "foo", as in case a and b.

    In your case JVM implicitly auto-boxes the ints and re-uses existing Integer object. Integer.valueOf() can be used explicitly to re-use existing objects if available.

    0 讨论(0)
  • 2020-12-05 23:00

    I believe when you create using new operator it creates object reference. In first case, there are two object references and they are not same but their value is same. That is not the situation in second case.

    0 讨论(0)
  • 2020-12-05 23:05

    for primitive data types like int the equality operator will check if the variables are equal in value

    for reference data types like your java.lang.Integer objects, the equality operator will check if the variables reference the same object. In the first case, you have two "new" and separate integer objects, so the references are different

    0 讨论(0)
提交回复
热议问题