Java: Int vs. Integer

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

'Int' and 'Integer' are not the same in Java. They both have their uses, and advantages and disadvantages. Here, you'll learn the difference between the Java primitive data type 'int' and the class 'Integer', which offers additional useful capabilities.

Basic vs Enhanced

Our legs are our most basic (i.e., primitive) form of transportation, always available when we need them, requiring minimum effort and overhead to use. They're also very efficient for transporting us short distances. However, our legs have limitations, such as how much can be transported and how fast it can be done. Our legs can be augmented by wrapping a mechanical device around them, such as an exoskeleton. The exoskeleton enhances our transportation capabilities by allowing us to go further, faster and transport additional items. However, there's a price to be paid for these additional capabilities. The exoskeleton has construction, maintenance and overhead costs associated with its use. Which do you use for when?

Primitive Int Data Type

Java has a primitive data type, int, for storing and manipulating integer values. It's efficient (using just 32-bits of storage per value) and fast as possible (since the computer's hardware operates on int values directly). However, there are some desirable things which can't be done directly using int variables. Here are a few examples:

  • Convert a series of text characters such as '1950' (possibly obtained from the keyboard or an external file) into binary representation so the corresponding numeric value can be used in an arithmetic calculation with other numeric values.
  • Convert the binary value stored in an int variable to a series of characters representing its decimal (or some other) representation (possibly to print or be retained in a file).
  • There are many useful Java utility classes which require the use of generic objects and not specific primitive data types. The List class allows creating and manipulating a list of objects each containing arbitrarily complex data. However, an int can't be used directly by List because a primitive data type is not an object.


Int Limitations


Integer Class

Java provides a special class named Integer which 'wraps' itself around an int data primitive value (i.e., the class contains an int inside) and provides a group of useful methods which provide enhanced functionality. It's like placing an int inside a box and the only way to access the value stored inside is through special portals (i.e., the methods) provided on top of the box. It's possible to create (i.e. construct via the 'new' operator) an Integer object from an int primitive data type, as well as from a String object (which encapsulates a series of text characters). If possible, the String object text will be automatically converted into a binary representation of the numeric value it represents and stored in the Integer object's internal int value. Otherwise, a NumberFormatException error will be raised by the Java system if the text isn't a correct representation of a numeric value.


Int Limitations


The Pros and Cons

Once constructed, an Integer object not only can be used in ways a primitive int data type can't, but it can also be used in most places an int can, such as arithmetic operations. This is very convenient from the programmer's perspective.


Integer Class Pros


So, why not always use an Integer object instead of an int? Like the exoskeleton, there's a price paid for the extra power and flexibility. Each Integer object can occupy 4 (or more) times the storage space as a primitive 'int', so if the extra capabilities aren't needed and your Java program must hold many (i.e., thousands or more) integer values in memory, consider using int instead of Integer. Even though improvements have been made in later Java implementations, arithmetic operations using Integer can be several (2 or more) times slower than the same operations using a primitive int, so if you're going to be doing many calculations and fast execution time is critical, consider using int instead of Integer.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support