Primitive Data Types
Integer Types
byte: Occupies 1 byte (8 bits). Represents signed integers from -128 to 127. Ideal for manipulating raw binary data, file I/O streams, or conserving memory in large arrays.
byte rawData = 99;short: Occupies 2 bytes (16 bits). The numerical range spans from -32768 to 32767. Useful when memory savings are critical and the range falls well within the 16-bit limit.
short altitude = 10500;int: Occupies 4 bytes (32 bits). This is the default type for whole numbers, covering a range of -2^31 to 2^31 - 1. It serves as the standard choice for general arithmetic and loop counters.
int population = 850000;long: Occupies 8 bytes (64 bits). Provides a massive range of -2^63 to 2^63 - 1. Must be suffixed with 'L' or 'l'. Utilized when the int range is exceeded, such as tracking large timestamps or unique identifiers.
long galaxyDistance = 4567890123L;Floating-Point Types
float: Occupies 4 bytes, offering single-precision (around 6-7 significant decimal digits). Literals require an 'f' or 'F' suffix. Commonly applied in graphics processing and scientific simulations where extreme precision is less critical than memory footprint.
float opacity = 0.75f;To restrict a float value to N decimal places for display, utilize formatting utilities:
float temperature = 36.12345f;
String viaStringFormat = String.format("%.2f", temperature);
DecimalFormat decimalFmt = new DecimalFormat("#.##");
String viaDecimalFmt = decimalFmt.format(temperature);double: Occupies 8 bytes, delivering double-precision (approximately 15-16 significant decimal digits). This is the default type for decimal values. Chosen for financial computations, engineering, and scenarios demanding high accuracy.
double piApproximation = 3.141592653589793;Formatting a double follows the same approach as float:
double exchangeRate = 1.23456789;
String formattedRate = String.format("%.4f", exchangeRate);Character Type
char: Occupies 2 bytes, storing a single 16-bit Unicode character. Enclosed in single quotes. Unlike char, text strings are represented by the String reference type, enclosed in double quotes.
char letter = 'Z';
String word = "Zenith";Boolean Type
boolean: Represents one of two states: true or false. Its exact memory size is implementation-dependent (often 1 byte in Oracle's JVM). Java strictly prohibits integer-to-boolean conversions (0 and non-zero do not equate to false/true). The default value is false.
boolean isAuthenticated = true;Type Conversion and Promotion
Widening (Implicit) Conversion: Automatically occurs when assigning a smaller-range type to a larger-range type. For instance, assigning an int to a long, or a long to a float. Although a long holds more integer precision, a float holds a larger overall magnitude, making long-to-float a widening conversion.
int baseCount = 500;
long expandedCount = baseCount;
float fractionalCount = expandedCount;Narrowing (Explicit) Casting: Required when converting a larger-range type to a smaller-range type. This manual cast risks data truncation or precision loss.
double preciseWeight = 154.78;
int roughWeight = (int) preciseWeight;Arithmetic Promotion: When evaluating expressions, smaller data types are temporarily promoted to match the larger operand type to prevent precision loss. Specifically, byte, short, and char are always promoted to int before any arithmetic operation. If an operand is a long, float, or double, the entire expression is promoted to that respective type.
byte x = 40;
byte y = 10;
int sum = x + y; // x and y are promoted to int before addition
long bigNum = 100L;
int normalNum = 25;
long total = bigNum + normalNum; // normalNum promoted to long
double decimalVal = 5.5;
int integerVal = 2;
double result = decimalVal + integerVal; // integerVal promoted to doubleReference Data Types
Unlike primitives, reference types do not store values directly; they hold memory addresses pointing to object instances on the heap.
Classes
Objects instantiated from class blueprints act as reference types. The variable holds a reference to the heap-allocated object.
class Engine {}
Engine motor = new Engine();Interfaces
A variable declared with an interface type can reference any object whose class implements that interface.
interface Steerable {}
class Sedan implements Steerable {}
Steerable vehicle = new Sedan();Enums
Defined using the enum keyword, these represent fixed sets of constants. The variable points to one of these specific enum instances.
enum Season { SPRING, SUMMER, AUTUMN, WINTER }
Season current = Season.SUMMER;Arrays
Arrays are fixed-length, indexed collections of homogenous elements. The array variable itself is a reference to the contiguous memory block holding the elements.
int[] scores = new int[5];