Machine-translated by Google. Questions are authored in English.
Top

Discussion

How do you instantiate an array in Java?

  • A.int arr[] = new int(3);
  • B.int arr[];
  • C.int arr[] = new int[3];
  • D.int arr() = new int(3);

Answer: C

Correct answer: int arr[] = new int[3];

In Java an array object is created with new followed by the type and the length in square brackets: int arr[] = new int[3];. Round brackets after a type mean a constructor call, and int is a primitive with no constructor. int arr[]; alone only declares the reference - no array exists until new runs.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.