Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsJava Basics, JVM & Memory
✓ FreeBeginner· 8 min read

Data Types, Wrapper Classes & Equality — Interview Questions

Is Java fully object-oriented, primitives vs references, null and pointers, == vs equals(), wrapper classes, autoboxing and its traps.

Published September 25, 2026


How to use this lesson

These questions look basic, but they hide some of the most common bugs in real Java code: comparing Integers with ==, and NullPointerExceptions from unboxing. Answer them with a small code example; it proves you've actually hit these issues.

Q1. Is Java a 100% object-oriented language?

Short answer: No. Java has eight primitive types (byte, short, int, long, float, double, char, boolean) that are not objects. It also has static members that belong to a class rather than an object. A "pure" object-oriented language, such as Smalltalk, treats everything as an object.

Key points to cover:

  • Every primitive has an object counterpart (a wrapper class), so you can switch between the two worlds when you need to.
  • Some people also cite static methods and fields as non-OO features.

Learn it in depth → Classes and Objects

Q2. What are the advantages of Java being only partially object-oriented?

Short answer: Performance and simplicity. Primitives are stored directly (on the stack, or inline inside objects and arrays), with no object header and no garbage-collection cost. That makes arithmetic-heavy code fast and memory-efficient.

Key points to cover:

  • An int[] of a million elements is about 4 MB. A List<Integer> of the same size is several times larger, because each element is a separate object with a header, plus a reference to it.
  • Primitives map directly to CPU operations, which helps with interoperability and low-level work.
  • Project Valhalla (value classes) aims to give objects some of these benefits in future Java versions.

Q3. Why is object-oriented programming used in enterprise projects?

Short answer: Large systems need to be divided among many developers and to keep changing for years. OOP helps by modelling the domain as objects with clear responsibilities, hiding internal details behind interfaces, and allowing reuse and extension without rewriting existing code.

Key points to cover:

  • Encapsulation limits how far a change ripples through the code.
  • Abstraction and interfaces let teams work against contracts. The same idea underpins Spring's dependency injection and testing with mocks.
  • Polymorphism lets you add behaviour (a new payment method, say) by adding a class instead of editing a chain of ifs.

Learn it in depth → Object-Oriented Design Refresher

Q4. What is the difference between primitive and non-primitive data types?

Short answer: Primitives hold the value itself. They have a fixed size, can't be null, and have no methods. Non-primitive (reference) types hold a reference to an object on the heap. They can be null, they have methods, and their size depends on the object.

Key points to cover:

PrimitiveReference
Examplesint, double, boolean, charString, arrays, Integer, any class or interface
Default value (fields)0, 0.0, false, '\u0000'null
StoredThe value itselfA reference; the object lives on the heap
Compared with ==By valueBy identity (same object?)
Generics / collectionsNot allowed (List<int> is invalid)Allowed
  • Sizes: byte 1 byte, short 2, char 2 (UTF-16 code unit), int 4, float 4, long 8, double 8. boolean's size is JVM-specific.

Q5. Can a primitive be null?

Short answer: No. A primitive always holds a value. Fields get a default (0, false, …), and local variables must be assigned before use, or the code won't compile. To represent "no value", use the wrapper type (Integer, which can be null), or OptionalInt.

Common trap: a JPA entity with an int column that's nullable in the database. A NULL either fails to load or silently becomes 0. Use Integer for nullable columns.

Q6. Does Java have pointers?

Short answer: Not in the C/C++ sense. Java has references, which point to objects, but you can't do pointer arithmetic, take the address of a variable, or cast an integer to a reference.

Key points to cover:

  • This removes whole classes of bugs, such as buffer overflows and dangling pointers, and lets the GC move objects around during compaction.
  • A NullPointerException is named after the underlying idea: dereferencing a reference that points to nothing.
  • Low-level memory access still exists for special cases: the old sun.misc.Unsafe, and the Foreign Function & Memory API (standard since Java 22).

Q7. What is the difference between == and .equals()?

Short answer:

  • For primitives, == compares values.
  • For objects, == compares references (is it the same object?), while .equals() compares logical content, as defined by the class.
String a = new String("java");
String b = new String("java");
System.out.println(a == b);        // false: two different objects
System.out.println(a.equals(b));   // true: same characters

Key points to cover:

  • Object.equals() defaults to ==. Classes such as String, Integer, LocalDate and records override it to compare content.
  • If you override equals, you must override hashCode as well, or HashMap and HashSet break.
  • Use Objects.equals(a, b) to compare safely when either side may be null.

Common trap: "java" == "java" is true, because string literals are interned in the string pool. That fools people into thinking == works for strings.

Learn it in depth → equals() and hashCode()

Q8. What are wrapper classes?

Short answer: Classes that wrap a primitive in an object: Byte, Short, Integer, Long, Float, Double, Character and Boolean in java.lang.

Key points to cover:

  • They're immutable and final.
  • They provide utilities: Integer.parseInt("42"), Integer.valueOf(42), Integer.MAX_VALUE, Character.isDigit(c), Double.compare(a, b).
  • The numeric wrappers extend Number.

Q9. Why do we need wrapper classes? Why must collections use them?

Short answer: Many Java APIs work only with objects. Generics and collections (List<Integer>, Map<String, Long>) can't hold primitives, because type parameters must be reference types. Wrappers also let you represent a missing value (null), and they supply parsing and conversion methods.

Key points to cover:

  • Other places that need objects: Optional<Integer>, reflection, serialisation frameworks, and nullable database columns in JPA entities.
  • For performance-critical numeric code, prefer primitive arrays, or primitive streams (IntStream) that avoid boxing.

Learn it in depth → Generics

Q10. What are autoboxing and unboxing?

Short answer: Autoboxing is the compiler automatically converting a primitive to its wrapper (int → Integer). Unboxing is the reverse. The compiler inserts Integer.valueOf(x) and x.intValue() calls for you.

List<Integer> scores = new ArrayList<>();
scores.add(90);                 // autoboxing: scores.add(Integer.valueOf(90))
int first = scores.get(0);      // unboxing: scores.get(0).intValue()

Q11. Where can autoboxing produce unexpected results?

Short answer: Comparing wrappers with ==. Integer.valueOf caches the values −128 to 127. Inside that range, equal values share one object, so == happens to return true. Outside it, you get two different objects, and == returns false.

Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b);        // true: both come from the cache
System.out.println(c == d);        // false: two distinct objects
System.out.println(c.equals(d));   // true: always compare wrappers with equals()

Key points to cover:

  • A performance surprise: Long sum = 0L; for (…) sum += i; creates a new Long object on every iteration. Use the primitive long.
  • Overload resolution: list.remove(1) on a List<Integer> removes the element at index 1, not the value 1. Use list.remove(Integer.valueOf(1)) to remove by value.

Q12. When can autoboxing or unboxing cause a NullPointerException?

Short answer: Whenever a null wrapper is unboxed, because the compiler calls .intValue() on null.

Map<String, Integer> stock = new HashMap<>();
int qty = stock.get("apple");        // NPE: get() returns null, and unboxing null fails

Integer discount = null;
boolean flag = true;
int value = flag ? discount : 0;     // NPE too: the ternary unboxes 'discount'

Key points to cover:

  • Fixes: stock.getOrDefault("apple", 0), keeping the wrapper type until you've checked for null, or using Optional.
  • Since Java 14, helpful NPE messages tell you exactly what was null ("because the return value of Map.get(Object) is null"), which makes these bugs quick to find.

Follow-up questions this topic invites — and their answers

Q: What's the difference between Integer.valueOf() and new Integer()? A: valueOf can return a cached instance (−128 to 127), while new Integer always created a new object. The new Integer(…) constructors are deprecated for removal. Always use valueOf, or let autoboxing call it.

Q: What does Integer.parseInt("12a") do? A: It throws a NumberFormatException, which is unchecked. Validate the input, or catch that exception, when you parse user-provided strings.

Q: Why does 0.1 + 0.2 != 0.3 in Java? A: double is binary floating point, and it can't represent 0.1 exactly, so small rounding errors accumulate. For money, use BigDecimal (created from a String), or store amounts as long minor units such as paise or cents.

Q: What is the default value of a local int variable? A: It doesn't have one. Local variables aren't initialised automatically, and the compiler rejects reading one before it's assigned. Only fields and array elements get default values.

Q: What happens with int x = Integer.MAX_VALUE + 1? A: It silently overflows to Integer.MIN_VALUE. Use Math.addExact, which throws on overflow, or long, when overflow is possible.

Previous

JVM Memory & Garbage Collection — Interview Questions

Next

Classes, Objects, Packages & Access Modifiers — Interview Questions

AI Tutor

Lesson: Data Types, Wrapper Classes & Equality — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.