Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 8)
8.
Which one of the following will declare an array and initialize it with five numbers?
Array a = new Array(5);
int [] a = {23,22,21,20,19};
int a [] = new int[5];
int [5] array;
Answer: Option
Explanation:

Option B is the legal way to declare and initialize an array with five elements.

Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor. If you don't see the brackets, you can be certain there is no actual array object! In other words, an Array object (instance of class Array) is not the same as an array object.

Option C is wrong because it shows a legal array declaration, but with no initialization.

Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.

Discussion:
25 comments Page 3 of 3.

Saki said:   8 years ago
Why option C is false? I don't know this. It's not true? Please explain.

Harsh said:   1 decade ago
Awesome. Option C describes only array declaration not initialization.

Amit said:   1 decade ago
How can you say that option C wrong please anybody explain me?

Cutie said:   9 years ago
Difference b/w instantiate and initialize ?

Anyone ?
(1)

Hi_hello said:   1 decade ago
int a [] = new int[5]; why is t wrong?


Post your comments here:

Your comments will be displayed after verification.