LearnThatStack Ace your next interview
Java · question
Question 1 of 95

Explain the difference between primitive and reference data types.

beginner
← All Java questions
Re-explain
Visual ↓

A primitive type variable holds the value itself, while a reference type variable holds a reference that points to an object. Java has eight primitives:

  • Integer types: byte, short, int, long
  • Floating-point types: float, double
  • boolean for true or false, and char for a single character

Each primitive has a fixed size and can never be null.

Classes, interfaces and arrays are reference types, and so are enums. Their objects are always allocated on the heap. Assigning one reference variable to another copies the reference, not the object, so both variables then point to the same object.

Where a primitive lives depends on where it is declared. A local int sits in the method's stack frame, but an int field sits inside its object on the heap.

int x = 10; // primitive - stores value 10
String str = "Hello"; // reference - stores memory address of String object
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
What x and str really hold
after int x = 10int x = 10;
Stack frame
x10

x is a local primitive, so 10 sits in its own stack frame slot. Java creates nothing on the heap.

after String strString str = "Hello";
Stack frame
x10
strnew1
Heap
1Stringnew
value"Hello"

str is a reference, so it stores the String's address, not the text. The String lives on the heap.

after other = strString other = str;
Stack frame
x10
str1
othernew1
Heap
1String
value"Hello"

Java copies the address, not the object. Now str and other both point at the same single String.

A primitive holds its value. A reference holds the address of a heap object, so copying it gives a second reference and no second object.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Java cheatsheet.

← Back to all Java questions
Pro · $10/mo

82 of 95 Java answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime