Cosmic Module
J
Qubits of DPK
March 15, 2026
Core Java
Layman Explanation
Data types are labels Java uses to know what kind of value you're storing and how much memory to reserve. Like different sized boxes — a shoebox for shoes, a matchbox for matches.
Real World Analogy
Imagine a hotel booking form. Name field accepts text, Age accepts only numbers, Date accepts only dates. Each field has a "type" — that's exactly what data types do in Java.
The 8 Primitive Data Types
Code Examples
java
QUBITS OF DPK
Primitive vs Non-Primitive
javascript
QUBITS OF DPK
Default Values
️ Local variables have NO default — must initialize manually.
️ Common Mistakes & Traps
java
QUBITS OF DPK
Production Use Cases
- int — user IDs, counters, loop indices
- long — timestamps (System.currentTimeMillis()), large IDs
- double — prices, percentages, calculations
- boolean — feature flags, login status, permission checks
- char — DSA string problems, character manipulation
Interview Answer (SDE-2)
"Java has 8 primitive data types divided into integers (byte, short, int, long), decimals (float, double), character (char), and logical (boolean). Primitives store actual values in stack memory. Non-primitives like String store references pointing to heap objects. Key traps: long needs L suffix, float needs f suffix, and local variables must be manually initialized — no defaults unlike instance variables."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between primitive and non-primitive data types?
Primitives (int, double, char, boolean, byte, short, long, float): store the actual value directly in stack memory, fixed size, no methods, not nullable, faster. Non-primitives (String, arrays, classes, interfaces): store a reference (address) in stack pointing to the actual object in heap memory, can be null, have methods, can be much larger. Example: int x = 5 stores 5 in stack. String s = "hi" stores the memory address in stack, "hi" object in heap.
Q2. Why does Java have both int and long?
Memory efficiency and range tradeoff. int (4 bytes) handles most integers up to ~2.1 billion — sufficient for ages, IDs, counters. long (8 bytes) handles up to ~9.2 quintillion — needed for timestamps (System.currentTimeMillis()), large financial numbers, file sizes, population data. Using int when you don't need long saves 4 bytes per variable — significant for arrays of millions of elements.
Q3. What is the default value of a boolean instance variable?
false. All numeric instance variables (int, long, double etc.) default to 0. char defaults to '\u0000'. boolean defaults to false. Reference types (String, objects) default to null. Critical note: local variables (inside methods) have NO default value — using uninitialized local variable is a compile error.
Q4. Why do we need f suffix for float literals?
Because Java's default type for decimal literals is double (8 bytes), not float. When you write 3.14, Java creates a double. Assigning it to float without a cast would be a narrowing conversion — Java doesn't do that implicitly. The f suffix explicitly tells the compiler "this is a float literal": float temp = 36.6f. Without it: compile error possible lossy conversion from double to float.
Q5. What happens if you assign a long value without L suffix?
Compile error if the value exceeds int range (-2,147,483,648 to 2,147,483,647). long x = 10000000000 — Java sees 10000000000 as an int literal first, realizes it overflows int range, and throws compile error: integer number too large. Fix: long x = 10000000000L. For values within int range like long x = 100, no error since 100 fits in int and gets widened automatically to long.