DEV Community

SILAMBARASAN A
SILAMBARASAN A

Posted on

Primitive Data Types in Java

in Java, primitive data types are the most basic types of data. They store simple values (not objects) and are very fast.

1. byte

  • Size: 1 byte (8 bits)
  • Range: -128 to 127
  • Used to save memory in large arrays

Example:
byte b = 100;

2. short

  • Size: 2 bytes
  • Range: -32,768 to 32,767

Example:
short s = 20000;

3. int

  • Size: 4 bytes
  • Most commonly used
  • Range: -2ยณยน to 2ยณยน-1

Example:
int num = 100000;

4. long

  • Size: 8 bytes
  • Used for very large numbers

Example:
long l = 10000000000L;

5. float

  • Size: 4 bytes
  • Decimal numbers (less precision)

Example:
float f = 5.75f;

6. double

  • Size: 8 bytes
  • More precise decimal values

Example:
double d = 19.99;

7. char

  • Size: 2 bytes
  • Stores single character

Example:
char c = 'A';

8. boolean

  • Size: 1 bit (logical)
  • Values: true / false

Example:
boolean isJavaFun = true;

Simple Summary Table

Type Size Example
byte 1 byte 100
short 2 bytes 20000
int 4 bytes 100000
long 8 bytes 10000000000L
float 4 bytes 5.75f
double 8 bytes 19.99
char 2 bytes 'A'
boolean 1 bit true

Top comments (0)