Java Integer

By: Stephen Patrick | 24 May 2016 | Category: Java Type System

Related Contents

Java Integer

The Integer data type in Java belongs to the java.lang package. This package is indirectly imported by the Java compiler so you don’t have to. Like other, numerical types the Integer class extends from the java.lang.Number class, which like every class in the Java ecosystem extends from java.lang Object.

Wow, hang on a second, java.lang.Number, Integer class, what are you talking about. Can’t we just define an integer like this:

   int one = 1;

Well, yes and as you know thats what we call a integer literal and is referred to as a primitive type. Well, before moving on lets quickly remind ourselves of what the Integer Data Type is

Java Integer Data Type

The integer type is smaller than the long data type, but is bigger in size than the short data type which is greater in size than the byte data type and a byte is just 8 bits. The integer data type is a 32-bit signed two’s complement integer. Integer literals can be defined in octal (base 8), decimal (base 10), or hexadecimal (base 16). A decimal integer is specified by a sequence of digits beginning with one of the characters 1-9. Java 8 and later, the integer data type can represent an unsigned 32-bit integer.

Lets define some integers:

   1.  int one = 1;
   2.  int hexFifteen= 0xf;
   3.  int octalTen = 012;

In the above code snippet, the first line above defines an integer literal with the value of 1. The second line above defines an integer with a value of 15 using hexadecimal notation, and the final line above defines an integer with a value of 10 using octal notation. So what we have done above is define integers as integer literals using the primitive Java types.

What Is The Java Integer Class

In Java everything is class, and all objects created from a class extends the super object, java.lang.Object. So, whats the difference between a class and an object. Well, its quite simple, we can think of a class as a template, a blueprint for writing code and an object is an instance of that class that exists in memory, with its own unique identity.

Think of it this way, open your wallet and take a look at the notes, in your wallet or coins in your pocket. Now think about this for a moment. There are many of the same types of notes and coins in the world, and think about how they were created, they were all created from the same template or printing plate. So a Java class is just a way for us to take a concept we wish to model and represent that in code. When we need to create a number of instances of that concept we create a number of objects by using the new operator similar to a printing press creating n number of notes from the same plate.

Why Are There Two Types Of Integers ?

Well there is really only one type of integer, if we remember everything in Java, is a class. Primitive type integers such as those defined by int i = 1 are really easy to work with for example adding two integers:

   int one = 1;
   int two =2;
   //equals three
   int sum = one + two;

   int sum = 1 + 2;

Primitive types can be optimized by the compiler. But in Java we work with objects we call methods on an object, so sometimes it can be easier to work with an integer as an Object. In this case Java provides the java.lang.Integer wrapper type that can wrap an int providing an object orientated feel to the int primitive type. For example the integer wrapper class provides a static method valueOf() that takes a primitive int and converts it into an Integer class.

   Integer ten = Integer.valueOf(10)

Now consider using an API that has a method requiring an Integer class i.e. (Integer) and you have an int primitive type i.e. (int) well, wouldn’t it be painful to have to code Integer.valueOf(10) or create a new Integer type every time. Well, from Java 1.5 onwards you don’t have to. The Java 1.5 specification introduced autoboxing and unboxing so this conversion happens automatically.

Also note that Integer.valueOf() is not the only way to create an integer you could use the Integer constructor:

   Integer fromInt = new Integer(3);
   Integer fromString = new Integer("3");

However you should refrain from using the constructor, you should always use the value of method as this can offer performance gains within your code. In fact, the autoboxing mechanism uses the valueOf() method.