What are Java’s main data types
💡 Concept: Java Data Types
Java data types classify the type of data a variable can hold, divided broadly into primitive and reference types.
📘 Quick Intro
Java has 8 primitive data types such as int, char, boolean, and several reference types like arrays and objects.
🧠 Analogy
Think of data types as different containers made for specific items, ensuring proper storage and access, just like using a jar for cookies and a bottle for water.
🔧 Technical Explanation
- Primitive types: byte, short, int, long, float, double, char, boolean.
- Reference types: Objects, arrays, classes, interfaces.
- Primitive types have fixed sizes and hold actual values.
- Reference types hold memory addresses pointing to objects.
- Java performs automatic type conversions and supports boxing/unboxing.
🎯 Use Cases
- ✅ Use primitive types for basic values like numbers, characters, and flags.
- ✅ Use reference types to store complex data structures and objects.
- ✅ Understand data types for memory optimization and correctness.
💻 Code Example: Declaring Java Variables
// Primitive types
int age = 30;
char grade = 'A';
boolean isPassed = true;
// Reference type
String name = "John Doe";
int[] numbers = {1, 2, 3};

❓ Interview Q&A
Q1: How many primitive data types does Java have?
A: Eight.
Q2: What is the size of an int?
A: 32 bits.
Q3: What is the difference between primitive and reference types?
A: Primitive types hold values, reference types hold addresses.
Q4: Can a String be a primitive type?
A: No, String is a reference type.
Q5: What is boxing?
A: Converting primitive types to wrapper classes.
Q6: What is unboxing?
A: Converting wrapper classes back to primitives.
Q7: Are arrays primitive types?
A: No, arrays are reference types.
Q8: What is the default value of boolean?
A: false.
Q9: Can primitive types be null?
A: No.
Q10: What is a wrapper class?
A: Classes that wrap primitive values.
📝 MCQs
Q1. How many primitive data types are in Java?
- Six
- Seven
- Eight
- Nine
Q2. What is the size of a char in Java?
- 8 bits
- 16 bits
- 32 bits
- 64 bits
Q3. Which is a reference type?
- int
- boolean
- String
- float
Q4. What is boxing in Java?
- Converting objects to primitives
- Converting primitives to objects
- Automatic casting
- None
Q5. What is the default value of boolean?
- true
- false
- null
- 0
Q6. Are arrays primitive types?
- Yes
- No
- Sometimes
- Depends on JVM
Q7. What is unboxing?
- Converting primitives to objects
- Converting objects to primitives
- Casting
- None
Q8. Can primitive types be null?
- Yes
- No
- Sometimes
- Only reference types
Q9. What are wrapper classes?
- Primitive types
- Classes wrapping primitive values
- Java APIs
- Interfaces
Q10. What is a primitive data type?
- Holds memory address
- Holds actual values
- Stores objects
- None
💡 Bonus Insight
Mastering Java data types helps optimize performance and ensures type safety in your applications.
📄 PDF Download
Need a handy summary for your notes? Download this topic as a PDF!