Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The new operator

Reference for the Java new operator: create objects, arrays, anonymous classes and inner class instances, with initialization, default values and common errors.

new allocates memory for an object or array and returns a reference to it. For an object, it also runs a constructor. For an array, every element starts with the default value of its type: 0, false, '\u0000' or null.

new always creates a fresh object, so two new expressions never give the same reference. Some types also have factory methods (List.of(), Integer.valueOf(), Optional.of()) that may share instances or choose a subclass; they do not use new in your code.

Java
var list = new ArrayList<String>();      // object
int[] counts = new int[5];               // array of five zeros
String[][] grid = new String[2][3];      // 2 x 3 array of null

Try it

Java Objects and arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Anonymous and inner class creation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
new ClassName(arguments)
new ClassName<>(arguments)              // diamond: infer type arguments
new Type[length]                        // array, default values
new Type[] { v1, v2 }                   // array with values
new Type[a][b]                          // rectangular multi-dimensional
new Interface() { /* members */ }       // anonymous class
outer.new Inner(arguments)              // inner class instance
Failure When
OutOfMemoryError The heap cannot hold the object or array.
NegativeArraySizeException Array length is negative.
InstantiationError / compile error Class is abstract or an interface (without an anonymous body).
Exception from the constructor The object is not created; the exception propagates.

Usage notes

  • Order of events: memory is allocated and zeroed, the constructor chain runs, then the reference is returned.
  • Strings and boxing: new String("a") makes an unnecessary copy; use literals. new Integer(5) is deprecated for removal; use Integer.valueOf(5).
  • Diamond <>: lets the compiler infer type arguments from context. It works with anonymous classes since Java 9.
  • Array lengths are fixed. Use ArrayList when you need growth. Array length is an int, so at most about two billion elements.
  • Array initializer shorthand: int[] a = {1, 2}; is allowed only in a declaration; elsewhere write new int[] {1, 2}.
  • Cannot instantiate abstract classes, interfaces, enums, or classes with only private constructors from outside.
  • Cost: allocation is cheap on the JVM, but frequent large allocations create garbage-collection pressure.
  • Generic arrays: new T[n] and new List<String>[n] are errors because of type erasure.
  • Method references: ArrayList::new and int[]::new are constructor references that call new for you.