What does the `new` keyword do in Java when you create an object?
answer
- allocate heap memory
- default-init fields then run constructor
- returns a reference
- each new = distinct identity
- no new = null reference
basics
~20 snew makes a fresh object. It sets aside memory on the heap for the object, runs its constructor to set it up, and gives you back a reference (an address) you store in a variable.
solid answer
~40 sThe `new` keyword instantiates a class: it allocates memory on the heap for a new object of that type, initializes the object's fields (defaults first, then constructor logic), runs the matching constructor, and returns a reference to the freshly created object. That reference is what you assign to a variable. So `Dog d = new Dog();` creates a Dog on the heap and stores its reference in `d`. Without `new` you only have a reference variable that points to nothing (`null`); `new` is what actually produces the object the reference points at. Each `new` call yields a distinct object with its own identity and its own copy of instance fields.
code
java · 5 linesDog d; // reference variable, points to nothing yet (default null for a field)
d = new Dog(); // new: allocate on heap, run constructor, assign the reference to d
Dog a = new Dog();
Dog b = new Dog();
System.out.println(a == b); // false: two distinct objects, two distinct referencesgo deeper
Knows new creates an object, calls a constructor, and you store the result in a variable.
Explains the full sequence: heap allocation, default field init, constructor (with super and initializers), then a reference is returned and assigned.
Distinguishes reference from object, notes identity per new, and mentions objects can be created without a literal new (literals, boxing, factories).
Discusses allocation cost, escape analysis / scalar replacement letting the JIT avoid heap allocation, and TLAB (thread-local allocation buffers) for fast bump-pointer allocation.
## What "creating an object" means In Java, a **class** is a blueprint (a template describing what fields and methods an object has). An **object** (also called an *instance*) is a concrete thing built from that blueprint, living in memory at runtime. The act of building one object from a class is called **instantiation**. The **`new` keyword** is the operator that performs instantiation. When you write: ```java Dog d = new Dog(); ``` several distinct things happen, in order: 1. **Memory allocation.** The JVM (Java Virtual Machine — the program that runs your compiled Java) reserves a chunk of memory big enough to hold one `Dog` object. This memory comes from the **heap**, a large region of RAM the JVM manages for all objects. Allocation produces the raw object. 2. **Default field initialization.** Every instance field is first set to its *default* zero-value: numeric fields become `0`, `boolean` becomes `false`, and any reference field becomes `null`. This happens before any of your code runs, so a field is never "garbage". 3. **Constructor execution.** A **constructor** is a special method with the same name as the class and no return type; its job is to set up the new object. `new Dog()` calls the no-argument `Dog` constructor. Field initializers and instance-initializer blocks also run here, after the implicit `super(...)` call to the parent class constructor. 4. **Returning a reference.** Once the object is built, `new` evaluates to a **reference** — essentially the heap address (handle) of that object. That reference is the *value* of the `new` expression. The assignment `Dog d = ...` then stores that reference into the **reference variable** `d`. `d` is not the object; `d` holds a pointer-like handle to the object on the heap. ## Why `new` matters Without `new`, you can declare a reference variable (`Dog d;`) but it points to nothing — it is `null`, or unassigned. Trying to call a method through a `null` reference throws `NullPointerException`. `new` is the bridge from "a variable that could point to a Dog" to "an actual Dog exists". Every `new` call creates a **brand-new, distinct object** with its own **identity** and its own copy of all instance fields. So `new Dog()` and `new Dog()` are two different dogs even if their fields are equal. ## Things that are *not* `new` Not every object is born from a visible `new`: - String literals (`"hi"`) create String objects via the constant pool. - Autoboxing (`Integer x = 5;`) and array literals create objects. - Factory methods (`List.of(...)`, `Integer.valueOf(...)`) create or reuse objects internally. But conceptually they all do the same heap allocation + initialization that `new` does.
- Where does the object created by `new` live, and where does the reference variable live?The object lives on the heap (shared, GC-managed). A local reference variable lives on the thread's stack and holds the handle to the heap object; an instance reference field lives inside its owning heap object.
- Does `new` always return a different object than a previous `new` of the same type?Yes. Each `new` expression allocates a fresh object with its own identity, so `new T() == new T()` is always false (they are different references).
new is like ordering a custom-built house from a blueprint: the builder clears a plot (allocates memory), erects the standard frame (default fields), follows your spec sheet (constructor), and hands you the address (reference). The address slip is not the house.
- Saying `new` returns the object itself rather than a reference to it
- Claiming the variable IS the object
- Thinking `new` puts the object on the stack
- Believing two `new` calls can return the same object