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"?

    Back in the days when computers were invented they were mainly meant to perform mathematical calculations. The initial programming languages (such as FORTRAN and COBOL) were limited to integers, real numbers, and characters. As computers become more powerful (more CPU, RAM, and memory) more complex features, such as data structures, functions, and object orientation got incorporated into programming.

    As in Java Programming int, boolean, double, etc. can be represented as both objects (Integer, Boolean, Double e.t.c) and in the traditional way, hence we call them "primitive data types".

    Primitives are the most basic building blocks of data and are used to define more complex data types or structures.


Range information about Primitives

    Primitive Data TypeRangeBytes
    byte-128 to 1271
    short-32,768 to 32,7672
    int-2,147,483,648 to 2,147,483,6474
    float3.4028235E38 to 1.4E-45 (approx)4
    long-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078
    double1.7976E308 to 4.9E-324 (approx)8
    char0 to 65,535 (unsigned)2
    booleantrue or false1

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;



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap