Wednesday, 23 September 2015

Java Tutorial : Java Data Types Range

Master Core Java Concepts!

Subscribe to Ram N Java for clear explanations, practical code examples, and essential Java tutorials!

Subscribe Now on YouTube

Java Data Types and Memory Ranges

In Java, every variable has a specific data type that determines its size in memory, the kind of data it can hold, and its permissible range. Understanding these byte sizes and limits is essential for writing memory-efficient applications and avoiding overflow errors.

Overview of Java Primitive Types

  • boolean: Takes 1 bit of memory and stores only two possible values: true or false.
  • char: 2 bytes (16 bits) unsigned Unicode character, spanning from 0 to 65,535 ('\u0000' to '\uffff').
  • byte: 1 byte (8 bits) signed integer with a range from -128 to 127.
  • short: 2 bytes (16 bits) signed integer with a range from -32,768 to 32,767.
  • int: 4 bytes (32 bits) signed integer with a range from -2,147,483,648 to 2,147,483,647.
  • long: 8 bytes (64 bits) signed integer with a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • float: 4 bytes (32 bits) single-precision floating-point number.
  • double: 8 bytes (64 bits) double-precision floating-point number.

Size & Range Quick Reference

Data Type Size in Memory Bit Width Permissible Range
boolean 1 bit 1 bit true or false
byte 1 byte 8 bits -128 to 127
short 2 bytes 16 bits -32,768 to 32,767
char 2 bytes 16 bits 0 to 65,535 (Unicode)
int 4 bytes 32 bits -2,147,483,648 to 2,147,483,647
long 8 bytes 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes 32 bits Single-precision floating-point
double 8 bytes 64 bits Double-precision floating-point

Key Takeaways

✔️ Fixed Sizing: Java data types maintain the same byte sizes across every platform and operating system.

✔️ Unsigned char: char is the only unsigned numerical primitive in Java, holding values from 0 to 65,535.

✔️ Appropriate Sizing: Choose the smallest data type that safely contains your data range to optimize memory performance.


Watch More Java Tutorials:

1 comment:

Tutorials