What are the 8 Primitive Data Types in Java

There are 8 primitive data types in Java Programming Language.

  1. byte
  2. short
  3. int
  4. float
  5. long
  6. double
  7. char
  8. boolean

Note: The primitive data types names are case-sensitive and represented in lower cases. They are also among the reserved keywords in Java and hence cannot be used to define variables of any kind.

If you find it hard to remember them you can remember them using a mnemonic:

"Big Surfers In Los Angeles Love Finding Dolphins, Sharks, and Barracudas"


Why are they called "Primitives"?


Range information about Primitives


Primitives Examples:

  1. byte

    One should make use of byte when you know the range of the value is below -128 to 127, or when the storage is limited.

    Examples:

    byte daysInAWeek = 7;
    byte daysInAMarch = 31;
    byte age = 25;
    byte temperature = -10;

  2. short

    One should make use of short when you know the range of the value is below -32,768 to 32,767, or when the storage is limited.

    Examples:

    short daysInaYear = 365;
    short distance = 1000;
    short temperature = -273;

  3. int

    int is the default data type in numeric values in Java.

    It is widely used to represent integer values based on the vast range.

    Examples:

    int amount = 4500000;
    int counter = 54050;

  4. float

    floats are used to represent decimal values.

    A prefix of f or F is required to represent a float value.

    Examples:

    float amount = 450.25f;
    float latitude = 34.23F;
    float pi = 3.1415f;

  5. long

    A prefix of l or L is required to represent a float value.

    Used when int is not sufficient to store a value.

    Examples:

    long populationOfCity = 7794798739L;
    long distanceToSun = 149600000000l;
    long numberOfAtomsInUniverse = 10000000000000000000L;
    

  6. double

    double is the default data type in decimal values.

    Used when the decimal values need high precision, or float is not sufficient to store a value.

    Examples:

    double amount = 4500000.556545;
    double pi = 3.14159265358979323846264338327950288419716939937;

  7. char

    char's are unsigned and are used to represent single character. Examples: an alphabet, digit, symbol, or Unicode characters.

    Examples:

    char smiley = '\u263A';
    char letterA = 'A';

  8. boolean

    It is a logical data type that can have only two values true or false (in lowercase), used to store a result of the comparison.

    Examples:

    boolean isHoliday = false;
    boolean flag = true;