Skip to main content

Variables and Data Types

Integer Data Types

There are 4 data types, that store numeric data in programs. They are:

  1. byte
  2. short
  3. int
  4. long

byte

A byte is an 8-bit signed integer.

  • Occupies 1 byte.
  • The default value is 0.
  • It can store a min value of -2^7 or -128 and a maximum value of (2^7 - 1) or 127.
public class ByteType {
    public static void main(String[] args) {
        byte A = -1;
        byte B = 20;
        byte C = 3;

        byte add = (byte) (B + C);
        byte subtract = (byte) (B - C);

        System.out.println(add); // 23
        System.out.println(subtract); // 17
    }
}

The maximum and minimum values of byte can be found at:

byte high = Byte.MAX_VALUE;   // 127
byte low = Byte.MIN_VALUE;    // -128

short

A short is a 16-bit signed integer.

  • Occupies 2 bytes.
  • The default value is 0.
  • It can store a min value of -2^15 or -32768 and a maximum value of (2^15 - 1) or 32767.
public class ShortType {
    public static void main(String[] args) {
        short A = -48;
        short B = 987;
        short C = 17;

        short add = (short) (B + C);
        short subtract = (short) (B - C);

        System.out.println(add); // 1004
        System.out.println(subtract); // 970
    }
}

The maximum and minimum values of short can be found at:

short high = Short.MAX_VALUE; // 32767
short low = Short.MIN_VALUE;  // -32768

int

This is one of Java's most used data types, storing 4 bytes of data. According to Java API, the Integer class wraps a value of the primitive type int in an object.

An int is a 32-bit signed integer.

  • Occupies 4 bytes.
  • The default value is 0.
  • It can store a min value of -2^31 or -2B and a maximum value of (2^31 - 1) or 2B.
public class IntType {
    public static void main(String[] args) {
        int A = -48;
        int B = 987;
        int C = 17;

        int add = B + C;
        int subtract = B - C;

        System.out.println(add); // 1004
        System.out.println(subtract); // 970
    }
}

The maximum and minimum values of short can be found at:

int max = Integer.MAX_VALUE; // -2147483648
int min = Integer.MIN_VALUE; // 2147483647

long

By default, long is a signed integer (In Java 8, it can be either signed/unsigned).

Signed: It can store a minimum of 2^63 and a maximum of (2^63 - 1).

Unsigned: It can store a minimum value of 0 and a maximum value of (2^64 - 1).

  • Occupies 8 bytes.
  • The default value is 0L.

If you assign long A = 100, Java assumes it as a int type. Appending L makes it a long.

public class LongType {
  public static void main(String[] args) {
    long A = -42;
    long B = 284;
    long C = 73;
    long bigNumber = 549755813888L;

    long addedLongs = B + C; // 284 + 73 = 357
    long subtractedLongs = B - C; // 284 - 73 = 211

    System.out.println(addedLongs); // 357
    System.out.println(subtractedLongs); // 211
  }
}

The maximum and minimum values of long can be found at:

// Max and Min
long high = Long.MAX_VALUE; // 9223372036854775807L
long low = Long.MIN_VALUE;  // -9223372036854775808L
Note: Letter L appended at the end of the long literal is case insensitive, however it is good practice to use capital as it is easier to distinct from digit one.

The following example uses an operator ==, which compares if the left and right values are equal and matching.

2L == 2l // true

Operators are covered in detail in the following chapters.