Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between (Integer)y and new Integer(y) in java?

What is the difference between the following:

Integer in = (Integer)y;

and

Integer in = new Integer(y);

I want to convert int type to Integer type and vice versa. Here is my code for doing that:

public class CompareToDemo {

  public static void main(String[] args) {
    // Integer x=5;
    int y=25;

    System.out.println(y+" this is  int variable");

    Integer in = (Integer)y;

    //Integer in = new Integer(y);

    if(in instanceof Integer){
      System.out.println(in +" this is Integer variable");
    }
  }
}
like image 941
sunny ranjan Avatar asked Jul 04 '16 05:07

sunny ranjan


People also ask

What is new integer in Java?

new Integer(123) will create a new Object instance for each call. According to the javadoc, Integer. valueOf(123) has the difference it caches Objects... so you may (or may not) end up with the same Object if you call it more than once.

What is difference between int and Integer in Java?

In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.

How do you create a new integer in Java?

To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).

What is the difference between int [] A and int a [] in Java?

Though, there is no difference in functionality between both types of declaration. Both declare an array of integers, thus, there is no conclusion which style is more preferable, int[] a is the preferred syntax to declare an array in Java whereas int a[] was included to help the traditional C/C++ programmers.


1 Answers

If all you want to do is to convert an int primitive to an Integer object, you have four options

   Integer in = (Integer)y;         // 1 explicit cast
   Integer in = y;                  // 2 implicit cast (autoboxing)
   Integer in = new Integer(y);     // 3 explicit constructor
   Integer in = Integer.valueOf(y); // 4 static factory method

The most preferable way here is 2 (autoboxing). The explicit constructor (3) is the less preferable, as it might have some small performance hit.

Also, they are not strictly equivalent. Consider:

public static void main(String[] args) {
    int x = 25;
    Integer a = new Integer(x);
    Integer b = new Integer(x);
    System.out.println(a == b);     // false
    Integer c = Integer.valueOf(x);
    Integer d = Integer.valueOf(x);
    System.out.println(c == d);     // true
    Integer e = (Integer)x;
    Integer f = (Integer)x;
    System.out.println(e == f);     // true
}

This is because small integers are cached (details here).

like image 119
leonbloy Avatar answered Sep 18 '22 20:09

leonbloy