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.
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
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.
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
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; useInteger.valueOf(5). - Diamond
<>: lets the compiler infer type arguments from context. It works with anonymous classes since Java 9. - Array lengths are fixed. Use
ArrayListwhen you need growth. Array length is anint, so at most about two billion elements. - Array initializer shorthand:
int[] a = {1, 2};is allowed only in a declaration; elsewhere writenew 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]andnew List<String>[n]are errors because of type erasure. - Method references:
ArrayList::newandint[]::neware constructor references that callnewfor you.
Related
class,thisandsuper,var,abstract- Lessons: Classes and objects, Constructors, Arrays