Loading...

Java Data Types

Variables are reserved memory locations to store values. Java is a statically-typed language, meaning every variable must be declared with a data type.

1. Primitive Types 2. Reference Types

Integer Types

Used for storing whole numbers without decimals.

byte 8-bit. Used to save space in large arrays. Range: -128 to 127.
short 16-bit. 2x smaller than int. Range: -32,768 to 32,767.
int 32-bit. The default choice for whole numbers.
long 64-bit. Used when int is not large enough. Ends with 'L' (e.g., 100L).

Floating Point Types

Used for numbers with fractional parts (decimals).

float 32-bit (Single precision). Ends with 'f'. Not for precise currency. float f1 = 234.5f;

double 64-bit (Double precision). The default choice for decimals. double d1 = 123.4;

Logic & Text

boolean

Represents one bit of information (True/False). Used for flags and conditional logic.

boolean isActive = true;
char

16-bit Unicode character. Stores a single character.

char letter = 'A';

Reference Types

Reference variables are created using class constructors. They store the address of the object, not the value itself.

  • Includes Classes, Interfaces, Arrays, Strings.
  • Default value is always null.
  • Example: String s = "Hello";

Master Reference Guide

Comparing Size, Range, and Default Values

Type Size (Bits) Default Value Range / Description
byte 8 0 -128 to 127
short 16 0 -32,768 to 32,767
int 32 0 -231 to 231-1
long 64 0L -263 to 263-1
float 32 0.0f 7 decimal digits of precision
double 64 0.0d 15 decimal digits of precision
char 16 '\u0000' 0 to 65,535 (Unicode)
boolean 1* false true / false
String N/A null Sequence of characters