DEV Community

LAKSHMI G
LAKSHMI G

Posted on

Java Data Types

Java, data types define the type of data a variable can hold. They are divided into two main categories:

  1. Primitive Data Types
  2. Non-Primitive (Reference) Data Types

1. Primitive Data Types:

  • Primitive data types are the most basic building blocks of Java programs. They store simple values directly in memory and are used for efficient computation.

What are Primitive Data Types?
Java provides 8 primitive data types.
byte
short
int
long
float
double
char
boolean

Byte Data Type in Java

  • size of 1 byte- 8 bits
  • Stores very small integers values
  • byte range (-127 to 128)

Short Data Type in Java

  • Size of 2 byte- 16 bits
  • Stores small integer values larger than byte
  • Range of short -32,768 to 32,767
  • useage:Useful when you need to save memory but need to store slightly larger numbers than byte
    int Data Type in Java

  • Size of 4 byte- 32 bits

  • Stores standard integer values and is the most commonly used

  • numeric type

  • Range of int Standard integer

  • Usage: Ideal for counting, loops, arithmetic operations, and general-purpose integer storage.

long Data Type in Java

  • Size of 8 byte- 64 bits
  • Default value is 6L.(100000L)
  • Stores large integer values that exceed the range of int
  • Usage: Ideal for calculations involving large numbers, like population counts, financial data, or scientific computations.

Float Data Type in Java

  • Size of 4 byte- 32 bits
  • Stores decimal (floating-point) numbers with single precision.
  • Default value is 0.0f (99.9f)
    -** Usage*: Ideal for fractional numbers, like measurements, prices, or scientific calculations where less precision is acceptable.
    **Double Data Type in Java
    *

  • Size of 8 byte- 64 bitsStores decimal (floating-point) numbers with double precision.

  • Default value is 0.0d (99.99)

  • Usage: Ideal for precise fractional calculations, like financial computations, scientific formulas, or large-scale measurements.

char Data Type in Java

Size of 2 byte- 16 bits
Stores a single character or Unicode value.
Range: '\u0000' (0) to '\uffff' (65,535)
Usage: Ideal for storing letters, digits, symbols, or any single character.

Boolean Data Type in Java

  • Size of 1 bit (internally handled by JVM, size may vary)
  • Stores true or false values only.
  • Usage: Ideal for logical operations, conditions, and flags in programs.

Top comments (0)