ProgrammingBackend Developer

How does the new operator work in Java, what exactly happens when creating an object and what pitfalls exist?

Pass interviews with Hintsage AI assistant

Answer.

The new operator in Java is used to create new instances of objects. The process of creating an object involves allocating memory, initializing fields, and invoking a constructor.

Background

In classical programming languages, memory allocation and object initialization could happen separately. In Java, they are combined and managed by the Java Virtual Machine (JVM), which reduces the number of errors and memory leaks.

Issue

Misunderstanding the processes occurring during object creation can lead to incorrect initialization, memory leaks, or unexpected behavior.

Solution

When using the new operator:

  1. The JVM allocates memory for the object on the heap.
  2. Fields are filled with default values (null, 0, false) at the first stage.
  3. Initializers are executed, including the initialization of fields.
  4. The object's constructor is called.
  5. The variable receives a reference to the created object.

Example code:

Person p = new Person("Ivan", 20);

After this, a separate Person object appears in memory that can be used.

Key features:

  • Integrity and correctness of the object after creation is guaranteed.
  • There is no direct need to free memory (the garbage collector works).
  • Each call to new creates a separate object (except for the singleton pattern).

Trick Questions.

Can we avoid using the new operator when creating objects?

Yes. For example, when cloning (clone()), deserializing, using reflection (Class.newInstance()), but they have their own nuances and limitations.

Does new create a new object in the string pool?

No. If you create a string like this — new String("abc"), a new object will be created on the heap, even if such a string already exists in the String pool. It’s better to use string literals.

Is the behavior of new different for arrays?

Yes. For arrays, the new operator allocates memory for all elements of the array and initializes them with default values, but does not call constructors for elements unless they are primitives.

String[] arr = new String[5]; // All elements will be null

Common mistakes and anti-patterns

  • Using new to create strings (instead of literals), which leads to unnecessary objects.
  • Errors with initialization and the order of constructors, especially during inheritance.
  • Forgetting about garbage collection — attempts to manually free memory.

Real-life example

Negative case

A developer writes:

String s1 = new String("hi"); String s2 = new String("hi"); System.out.println(s1 == s2); // false

Pros:

  • Each string is guaranteed to be a separate object.

Cons:

  • Excessive memory load.
  • Comparison using == will not work (different objects).

Positive case

A developer writes:

String s1 = "hi"; String s2 = "hi"; System.out.println(s1 == s2); // true

Pros:

  • Strings are optimized through the String pool.
  • Memory is saved, comparison is faster.

Cons:

  • Comparison using == is only correct for literals, not in all cases.