☕ 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 CWhich 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 StringsType 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 = 42Related Topics in Java Basics
Variables in Java
A variable is just a named box in memory that holds a value. Java is strict about what goes in each box — you tell it the type upfront. Once you get this, the rest of Java clicks into place.
Primitive Data Types
Java has eight primitive data types — the most basic building blocks for storing data. Unlike objects, primitives are stored directly in memory, making them fast and efficient. Understanding each type, its size, range, and when to use it is fundamental to writing correct Java programs.
Non-Primitive Data Types
Non-primitive data types — also called reference types — are everything beyond Java's eight primitives. Strings, arrays, classes, interfaces, enums, and records all fall into this category. They're more powerful than primitives, but they work differently in memory, comparison, and nullability. Understanding the distinction is essential for writing correct Java.
Literals
A literal is a fixed value written directly in your source code — 42, 3.14, 'A', true, "Hello". Every literal has a type, and understanding how Java interprets each one prevents subtle bugs around type mismatches, overflow, and precision loss.