0

Since arrays are objects in java, and objects should be instantiated with the "new" keyword, I wonder how is it possible then to instantiate an array without using the new keyword, for example:

int[]test = {33,44,55};
Pawan.Java
  • 671
  • 1
  • 5
  • 21
Moayad
  • 1
  • 1
  • 7
    Because the designers of the Java language decided to provide a shortcut when initializing an array in the same statement that's declaring an array variable. It's "syntactic sugar". The values inside the brackets are number literals, another feature of the Java language. – Slaw Jun 13 '22 at 07:22
  • 2
    It is probably just syntactic sugar. "In the background", the constructor will most likely still be called. – Octavian Mărculescu Jun 13 '22 at 07:22
  • While at it, there are in fact also other ways to create instances, actually bypassing constructors (its mostly legacy though, avoid it), for example the Clone- and Serialization-API. – Zabuzard Jun 13 '22 at 07:24
  • Syntactic sugar - may be inspired by C++ aggregates initialization https://en.cppreference.com/w/cpp/language/aggregate_initialization – m.antkowicz Jun 13 '22 at 07:26
  • [Because the JLS says so](https://docs.oracle.com/javase/specs/jls/se18/html/jls-10.html#jls-10.3). Your claim 'objects should be instantiated with the "new" keyword' is false. There are several other ways. – user207421 Jun 13 '22 at 07:26

0 Answers0