☕ Java

Data Types in Java

Java needs to know exactly what kind of data it's dealing with before it can store or process it. Integers, decimals, characters, true/false — each has its own type. Knowing which to use (and why) makes your programs efficient and bug-free.

Primitive Data Types — The Building Blocks

Java has 8 primitive types built directly into the language. These are not objects — they're raw values stored directly in memory, which makes them fast. Each one has a specific size and range:
Java
byte   b = 100;           // 8-bit  | Range: -128 to 127 | Use: saving memory in large arrays
short  s = 30000;         // 16-bit | Range: -32,768 to 32,767 | Rarely used
int    i = 100000;        // 32-bit | The go-to integer type for most use cases
long   l = 9999999999L;   // 64-bit | For very large numbers (e.g., phone numbers, timestamps)
float  f = 3.14f;         // 32-bit | Decimal — less precise, saves memory
double d = 3.14159265;    // 64-bit | Decimal — more precise, the default for decimals
char   c = 'A';           // 16-bit | A single character (uses Unicode, so it handles ₹, ©, etc.)
boolean flag = true;      // true or false — nothing else, no 0/1 tricks like in C

Which Numeric Type Should You Use?

Practical guide: • Counting things (age, quantity, score)? Use int. • Financial amounts, weights, temperatures? Use double. • Very large numbers (population of earth, epoch milliseconds)? Use long. • Memory-constrained data in huge arrays? Consider float or byte. • Never use float for money — the imprecision will cause rounding errors. Use double or BigDecimal.

Non-Primitive (Reference) Types

Primitive types hold the actual value. Reference types hold a reference (pointer) to an object in memory. These are more powerful but also heavier. The most common ones: • String — a sequence of characters. Technically a class, not a primitive, but Java treats it specially. • Arrays — a fixed-size collection of values of the same type • Classes — your own custom types (Student, BankAccount, Product...) • Interfaces — contracts that classes agree to follow
Java
String name = "TechLearn";          // text — most used reference type
int[] scores = {85, 90, 78, 95};    // array of integers
String[] cities = {"Dubai", "London", "Tokyo"}; // array of Strings

Type Casting — Converting Between Types

Sometimes you need to convert one type to another. Java does some conversions automatically (when it's safe), and requires you to be explicit for others (when data might be lost).
Java
// Widening Cast (automatic) — smaller type fits into larger, no data loss
int i = 100;
double d = i;   // Java does this silently. d = 100.0

// Narrowing Cast (manual) — you're forcing a larger type into a smaller one
// You must explicitly cast, because you might lose data
double pi = 3.14159;
int approx = (int) pi;  // approx = 3 — the decimal part is cut off, not rounded

// String to int — common in real apps (parsing user input, API responses)
String userInput = "42";
int parsed = Integer.parseInt(userInput);  // parsed = 42