int vs Integer : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

int vs Integer
Newbies are often confused by the difference between int and Integer, boolean vs Boolean, char vs Character, short vs Short, long vs Long, float vs Float or double vs Double.

To properly understand the difference, you should read an introductory textbook on Java. I will make a small stab at answering here.

Everything I say here applies analogously to char and Character, short and Short, long and Long, float and Float, double and Double.

Definitions

An int is a primitive. It is not an Object. An int is a high performance, streamlined beast for calculating numbers in the range -2,147,483,648 [-231] aka Integer.MIN_VALUE to +2,147,483,647 [2 31-1] aka Integer.MAX_VALUE. An int is a bare bones 32-bit chunk of information. int variables are mutable. Unless you mark them final, you can change their value at any time.

An Integer, is a Object that contains a single int field. An Integer is much bulkier than an int. It is sort like a Fedex box to contain the int. Integers are immutable. If you want to affect the value of a Integer variable, the only way is to create a new Integer object and discard the old one.

Which is better?

That is like asking which is better a volley ball or a volleyball in a Fedex box. It depends what you want to do with it.
int vs Integer
ability unboxed
int
boxed
Integer
Calculate with it, + - * / % ^ etc.
Pass it as a parameter
return it as a value
Use methods on it from java.lang.Integer
Store it in a Vector or other Collection
Use it as a HashMap key
serialize it
send it by itself over RMI (Remote Method Invocation)
send it as part of another Object over RMI
pass it as a generic object to a TableCellRenderer. You may pass a variety of different types via the same paramter.
Allow for a null value that means there is no value.

Converting

Fortunately it is easy to convert back and forth between int and Integer.
// to int i from Integer ii
int i = ii.intValue();

// to Integer ii from int i
Integer ii = new Integer( i );
See this Amanuensis for other conversions.

Starting with Java 1.5, with boxing and unboxing, the compiler will sometimes automatically do the conversions to and from int and Integer for you so you can treat int and Integer as if they were almost the same thing.

Why Both?

Why are there both int and Integer? For speed. ints, without any Object packaging are compact and fast. Would it not have been easier if there were only one of sort of creature that could do everything and have the compiler automatically figure out when the packaging was needed and when not? The Eiffel language designers thought so, and the Java designers are gradually coming around to the same conclusion.

Learning More

Oracle’s Javadoc on Byte class : available:
Oracle’s Javadoc on Character class : available:
Oracle’s Javadoc on Double class : available:
Oracle’s Javadoc on Float class : available:
Oracle’s Javadoc on Integer class : available:
Oracle’s Javadoc on Long class : available:
Oracle’s Javadoc on Number class : available:
Oracle’s Javadoc on Short class : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/intvsinteger.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\intvsinteger.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
no blog for this page
IP:[65.110.21.43]
Your face IP:[54.197.94.241]
You are visitor number