🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Primitive Data Types in Java  


What are Data Types ?

  • In Java, a data type specifies the type of value a variable can hold.
  • It tells the compiler how much memory to allocate and what kind of data to expect.
  • For example :
    int rollno = 101;   // 'int' is a data type for integers
    float marks = 91.4f;  // 'float' is for decimal values
    char grade = 'A';  // 'char' is for single characters
  • There are 2 types of data types in Java :-
    • Primitive Data Types
    • Non-Primitive Data Types

Primitive Data Types

Primitive data types are pre-defined, basic building blocks in Java, representing fixed-size simple values like integers, decimals, or characters. Example: int rollno = 101;.
We have explained the primitive data types in points as below :-

  • a) Primitive Data Types are pre-defined data types
    • Primitive data types are the most basic building blocks in Java or any other programming language.
    • They are pre-defined by the Java compiler and represent simple data values, like numbers or characters.
    • Primitive data types are used to store simple, fixed-size data like integers, decimal numbers, or single characters.
    • For example : int rollno = 101;
      In this case:
      • The compiler knows that int can only hold whole numbers (no decimals).
      • It allocates 4 bytes of memory to store this value directly.
    • Some Points to Note :
      • Primitive Data Types are built into the language and are not created by the programmer.
      • Primitive Data Types are faster and more memory-efficient because they directly store the value, unlike objects which store references.
      • Primitive Data Types are not objects but raw values.
  • b) Primitive Data Types are of 8 types
    • Java provides 8 pre-defined data types i.e.
      boolean, char, byte, short, int, long, float and double.
    • Each type is suited for specific use cases:
      • boolean: Use for true/false values.
      • char: Use to store a single character (e.g., 'A').
      • byte: Use for small integers (-128 to 127).
      • short: Use for medium-sized integers (-32,768 to 32,767).
      • int: Use for whole numbers (default choice for integers).
      • long: Use for large integers beyond int range.
      • float: Use for small decimal numbers (less precision).
      • double: Use for large decimal numbers (high precision).
  • c) Primitive Data Types have fixed memory sizes
    • Each primitive data type has a fixed size in memory, regardless of the system.
    • The fixed size ensures that any particular data type will always take a particular memory size, no matter the operating system or hardware.
    • For example :
      int number = 123; // Allocates 4 bytes for storage.
      char letter = 'A'; // Allocates 2 bytes for a Unicode character.
      float price = 19.99f; // Allocates 4 bytes for a decimal value.
    • This fixed size ensures consistent behavior across different platforms, which is part of Java’s "write once, run anywhere" philosophy.
    • Below is the memory size and range of each data type:
      Data Type Default Size Default Value Range Corresponding Wrapper Classes
      boolean 1 bit
      (not precisely defined)
      false true or false Boolean
      char 2 bytes
      (16 bits)
      '\u0000' 0 to 65,535 (Unicode) Character
      byte 1 byte
      (8 bits)
      0 -128 to 127 Byte
      short 2 bytes
      (16 bits)
      0 -32,768 to 32,767 Short
      int 4 bytes
      (32 bits)
      0 -2,147,483,648 to 2,147,483,647 Integer
      long 8 bytes
      (64 bits)
      0L -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Long
      float 4 bytes
      (32 bits)
      0.0f Approximately ±3.40282347E+38F Float
      double 8 bytes
      (64 bits)
      0.0d Approximately ±1.79769313486231570E+308 Double
    • If we attempt to store a value outside the specified range, it results in a compilation or runtime error.
      For example:
      byte smallNumber = 200; // Error: 200 exceeds the byte range of -128 to 127.

Non-Primitive Data Types

Click Here to read Non-Primitive Data Types deeply.