What is the difference between Integer and int in Java?

JavaClassInt

Java Problem Overview


For example why can you do:

int n = 9;

But not:

Integer n = 9;

And you can do:

Integer.parseInt("1");

But not:

int.parseInt("1");

Java Solutions


Solution 1 - Java

int is a primitive type. Variables of type int store the actual binary value for the integer you want to represent. int.parseInt("1") doesn't make sense because int is not a class and therefore doesn't have any methods.

Integer is a class, no different from any other in the Java language. Variables of type Integer store references to Integer objects, just as with any other reference (object) type. Integer.parseInt("1") is a call to the static method parseInt from class Integer (note that this method actually returns an int and not an Integer).

To be more specific, Integer is a class with a single field of type int. This class is used where you need an int to be treated like any other object, such as in generic types or situations where you need nullability.

Note that every primitive type in Java has an equivalent wrapper class:

  • byte has Byte
  • short has Short
  • int has Integer
  • long has Long
  • boolean has Boolean
  • char has Character
  • float has Float
  • double has Double

Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.

Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.

Solution 2 - Java

An Integer is pretty much just a wrapper for the primitive type int. It allows you to use all the functions of the Integer class to make life a bit easier for you.

If you're new to Java, something you should learn to appreciate is the Java documentation. For example, anything you want to know about the Integer Class is documented in detail.

This is straight out of the documentation for the Integer class:

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

Solution 3 - Java

An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.

Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.

If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.

(In the above text, the term "variable" means local variable, field or parameter)

Solution 4 - Java

Integer refers to wrapper type in Java whereas int is a primitive type. Everything except primitive data types in Java is implemented just as objects that implies Java is a highly qualified pure object-oriented programming language. If you need, all primitives types are also available as wrapper types in Java. You can have some performance benefit with primitive types, and hence wrapper types should be used only when it is necessary.

In your example as below.

Integer n = 9;

the constant 9 is being auto-boxed (auto-boxing and unboxing occurs automatically from java 5 onwards) to Integer and therefore you can use the statement like that and also Integer n = new Integer(9). This is actually achieved through the statement Integer.valueOf(9).intValue();

Solution 5 - Java

Integer is an wrapper class/Object and int is primitive type. This difference plays huge role when you want to store int values in a collection, because they accept only objects as values (until jdk1.4). JDK5 onwards because of autoboxing it is whole different story.

Solution 6 - Java

This is taken from Java: The Complete Reference, Ninth Edition > Java uses primitive types (also called simple types), > such as int or double, to hold the basic data types supported by the > language. Primitive types, rather than objects, are used for these > quantities for the sake of performance. Using objects for these values > would add an unacceptable overhead to even the simplest of > calculations. Thus, the primitive types are not part of the object > hierarchy, and they do not inherit Object. > > Despite the performance > benefit offered by the primitive types, there are times when you will > need an object representation. For example, you can’t pass a primitive > type by reference to a method. Also, many of the standard data > structures implemented by Java operate on objects, which means that > you can’t use these (object specific) data structures to store primitive types. To > handle these (and other) situations, Java provides type wrappers, > which are classes that encapsulate a primitive type within an object. > > Wrapper classes relate directly to Java’s autoboxing > feature. The type wrappers are Double, Float, Long, Integer, Short, > Byte, Character, and Boolean. These classes offer a wide array of > methods that allow you to fully integrate the primitive types into > Java’s object hierarchy.

Solution 7 - Java

int is a primitive type and not an object. That means that there are no methods associated with it. Integer is an object with methods (such as parseInt).

With newer java there is functionality for auto boxing (and unboxing). That means that the compiler will insert Integer.valueOf(int) or integer.intValue() where needed. That means that it is actually possible to write

Integer n = 9;

which is interpreted as

Integer n = Integer.valueOf(9);

Solution 8 - Java

In Java int is a primitive data type while Integer is a Helper class, it is use to convert for one data type to other.

For example:

         double doubleValue = 156.5d;
         Double doubleObject  = new Double(doubleValue);
         Byte myByteValue = doubleObject.byteValue ();
         String myStringValue = doubleObject.toString();

Primitive data types are store the fastest available memory where the Helper class is complex and store in heap memory.

reference from "David Gassner" Java Essential Training.

Solution 9 - Java

int is a primitive type that represent an integer. whereas Integer is an Object that wraps int. The Integer object gives you more functionality, such as converting to hex, string, etc.

You can also use OOP concepts with Integer. For example, you can use Integer for generics (i.e. Collection<Integer>).

Solution 10 - Java

int is a primitive data type while Integer is a Reference or Wrapper Type (Class) in Java.

after java 1.5 which introduce the concept of autoboxing and unboxing you can initialize both int or Integer like this.

int a= 9
Integer a = 9 // both valid After Java 1.5.

>why Integer.parseInt("1"); but not int.parseInt("1"); ??

Integer is a Class defined in jdk library and parseInt() is a static method belongs to Integer Class

So, Integer.parseInt("1"); is possible in java. but int is primitive type (assume like a keyword) in java. So, you can't call parseInt() with int.

Solution 11 - Java

To optimize the Java code runtime, int primitive type(s) has been added including float, bool etc. but they come along with there wrapper classes so that if needed you can convert and use them as standard Java object along with many utility that comes as their member functions (such as Integer.parseInt("1")).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionminoView Question on Stackoverflow
Solution 1 - JavaDarkhoggView Answer on Stackoverflow
Solution 2 - JavaNadir MuzaffarView Answer on Stackoverflow
Solution 3 - Javauser5585864View Answer on Stackoverflow
Solution 4 - JavaLionView Answer on Stackoverflow
Solution 5 - JavakosaView Answer on Stackoverflow
Solution 6 - JavaTeoman shipahiView Answer on Stackoverflow
Solution 7 - JavaRoger LindsjöView Answer on Stackoverflow
Solution 8 - JavaAqeel HaiderView Answer on Stackoverflow
Solution 9 - JavaM SView Answer on Stackoverflow
Solution 10 - JavaVikrant KashyapView Answer on Stackoverflow
Solution 11 - JavaanandView Answer on Stackoverflow