KnowledgeBoat Logo

Data Types & Variables

Primitive Data Types: Integers

ICSE Computer Applications



Integers contain 4 primitive data types - byte, short, int & long.

Integer data types in Java to explain ICSE Computer Applications

All the data types in this group are used to store whole number values, they cannot store fractional numbers. Those of you who have some experience with other programming languages specially C++ might recall that other programming languages provide this concept of signed and unsigned variants of numeric data types. Java has no such concepts of signed and unsigned variants. All numeric data types in Java except for char can store positive and negative numbers. These data types in integer group differ widely in their size and range. Let's now look at each one of them in detail.

byte

The smallest integer type is byte. It has a size of 8 bits and can store whole numbers in the range of -128 to 127.

short

short has a size of 16 bits and can store whole numbers in the range of -32,768 to 32,767.

int

int is the most commonly used integer data type in Java. It has a size of 32 bits and can store whole numbers in the range of –2,147,483,648 to 2,147,483,647.

long

long as the name suggests is used to store really long numbers. It has a size of 64 bits and can store whole numbers in this huge range – -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807.

Sizes of different Integer data types in Java

This BlueJ program demonstrates Integer group data types:

public class IntegerDatatypesDemo
{
    public void demoIntegerDataTypes() {
        
        /*
         * Size of Byte: 8 bits
         * Range of Byte: -128 to 127
         */
        byte first = 10, second = 20;
        
        System.out.println("Value of first is " + first);
        System.out.println("Value of second is " + second);
        
        /*
         * Size of short: 16 bits
         * Range of short: -32,768 to 32,767
         */
        short shortA, shortB;
        shortA = -100;
        shortB = 30000;
        
        System.out.println("Value of shortA is " + shortA);
        System.out.println("Value of shortB is " + shortB);
        
        /*
         * Size of int: 32 bits
         * Range of int: -2,147,483,648 to 2,147,483,647
         */
        int intExample = 100000;
        System.out.println("Value of intExample is " + intExample);
        
        /*
         * Size of long: 64 bits
         * Range of long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
         */
        long worldPopulation = 7710648611l;
        long gdpOfIndia = 2700000000000L;
        
        System.out.println("World Population is " + worldPopulation);
        System.out.println("GDP of India is " + gdpOfIndia);
    }
}

We have this function demoIntegerDataTypes. Here, first we declare variables of byte datatype using the byte keyword followed by the name of the variables. Then we assign values of 10 and 20 to first and second byte type variables, respectively. Next we print the value of these variables. Let's execute the program.

Next, we declare 2 short variables shortA and shortB and assign values of -100 and 30000 to them. Let’s see what happens if we assign out of range values to them.

The compiler starts giving errors and will not let us do so.

After that we declare an int variable intExample and print its value. Then we declare 2 long variables worldPopulation and gdpOfIndia and assign them appropriate values. Notice the suffix L in long worldPopulation = 7710648611l; and long gdpOfIndia = 2700000000000L;. If we don’t specify this, the compiler will give an error.

BlueJ error for not suffixing long literal with L in Java

So whats wrong, why are we getting this error? Whenever you specify a number in your program, Java assigns a data type to that number too. By default, all integer literals are of type int in Java. We will look at integer literals in detail a little bit later, for now understand that whenever you write a whole number in your program it is an integer literal. These huge values here are outside the range of int and that’s why we are getting these errors. The error says integer number too large. Compiler is treating this number as an int value and complaining that it is outside the range of int. To fix this, we need to explicitly tell the compiler to treat these numbers as long values and we do so by specifying L as the suffix of the number. The case of L doesn’t matter you can write capital or small L both are equivalent for this.

PrevNext