int vs Integer Java


For a new learner of Java, it does not make any difference between int vs integer. He uses both just to refer a data type int. But he gets surprised once he comes to know there is a class called Integer in Java placed in java.lang package.

Some people argue that Java is not a fully object-oriented language as Java does not support multiple inheritance and AWT components are platform-dependent. Multiple inheritance is just removed to avoid confusion of virtual functions and Java proved today that an OOPs language can exist without full support of multiple inheritance.

But I can say, Java is fully an object-oriented language with the these facts. In Java, everything can be viewed as an object. A primitive data type int can be seen as an object with Integer class. A simple text file can be viewed as an object with File class. An address of system like 127.0.0.1 as an object with URL class etc.

Now let us come to our topic int vs Integer. int is a data type and Integer is a class. Why it is necessary for Java to introduce Integer class when there is primitive datatype int.

Java data structures like Vector, ArrayList, HashSet etc. are designed to store only objects as elements. As int not class or object, it cannot be stored by any DS of Java. So, now you feel the necessity of int as an object. For this purpose only, Integer class is introduced. Now yo can say, Integer class is introduced just to give an appearance of an object for simple datatype int. Yes, you are very correct. See the following.

Vector vect = new Vector();
int marks = 50;
vect.addElement(marks); // raises error upto JDK 1.4
// now convert marks to Integer object and then store
Integer i1 = new Integer(marks);
vect.addElement(i1); // happily accepts

From JDK 1.5, vect.addElement(marks); works because JDK 1.5 automatically converts itself marks into Integer object. This concept is known as autoboxing. But it is must to store Integer object.

Following program on int vs Integer illustrates the conversion of int to Integer object and back.

public class Demo
{
  public static void main(String args[])
  {
    int marks = 50;                       // just a primtive data
    System.out.println(marks+10);         // you can use marks in any arithmetic operation

// to convert marks into Integer object
    Integer i1 = new Integer(marks);   
    System.out.println(i1);               // prints 50
    System.out.println(i1+20);            // it gives error upto JDK 1.4, but works from JDK 1.5 as i1 is implicitly converted back to int

// to get back; to convert Integer object into int form
    int x = i1.intValue();
    System.out.println(x+x);              // works as x is datatype int; prints 100

    double y = i1.doubleValue();
    System.out.println(y+y);              // works as y is datatype double; prints 100.0
  }
}

int vs Integer: Now what you can do more with Integer object than int.

1. You can use an object.
2. You can serialize.
3. You can assign a null value
4. You can store in a DS like Vector.
5. You can return to a method call in RMI (Remote Method Invocation).

Like Integer, Double and Float classes etc. also exist and all these are known as wrapper classes. Many methods like intValue(), doubleValue(), floatValue() etc. exist with Integer class that return an int, a double and float.

Leave a Comment

Your email address will not be published.