File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array declaration" Watch "Array declaration" New topic

Array declaration

ketki kalkar
Ranch Hand

Joined: May 09, 2007
Posts: 36

Is this a valid array declaration?

double []arr = new double[10];

If yes, how?

One of chisolm exams states that square brackets should not be placed before the variable.

Cor Takken
Ranch Hand

Joined: May 21, 2007
Posts: 58
Yes, it is valid... as to why.... My guess is that the compiler is clever enough to 'assign' the two square brackets to the String. It might be compiler dependant though, I am not familiar with the compiler specs.

But if anybody knows better for sure, I pre-emptively stand corrected

SCJP 1.5
Dolphins are grey, but they dream in colour.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710

double []d = new double[10];
One of chisolm exams states that square brackets should not be placed before the variable.

Hi Ketki,

Are you sure it was written there?
IMHO, it should be square brackets should not be places after
variable name. There is no error if you don't do so. It is matter of
readability of code or general convention.

double []d = new double[10];
is more recommended than
double d[] = new double[10];


Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Originally posted by Chandra Bhatt:
[QB]double []d = new double[10];
Or even better (IMHO):
double[] d = new double[10];
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Though i m not pretty sure of the implementation of it, obviously the compiler may look for the presence of square brackets around the variables (of both primitives and instances) and if in case it finds any, its assured that the variable should be an *array* and must be treated as an object. So allocates a space in heap for the same!

I have also read the same in K & B's book and all other articles/tutorials/Best Practices as follows:

int[] intArray = new int[10];

gives you a clear picture and interpretation saying that the variable "intArray" is an array of integers


int intArray[] = new int[10];

may give you an impression that the variable "intArray" is of type primitive int just by looking at the datatype.

But to the compiler both are same; As chandra said, just to improve the readability of the code, its better and suggested to place the square brackets before the variable (means, along with the datatype)!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
This is plain and simple. Just keep in mind!!!
Array in Java can be declared and defined like--

int[] x = null;
int []x = null;
int x[] = null;
int x[] = new int[50];
int x[] = {5,10,1,100}

Thanks, AR
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
All you need to know about Array is this!
Arrays are dynamically created objects in Java code. An array can hold a number of variables of the same type. The variables can be primitives or object references; an array can even contain other arrays.

Declaring array variables
When we declare an array variable, the code creates a variable that can hold the reference to an array object. It does not create the array object or allocate space for array elements. It is illegal to specify the size of an array during declaration. The square brackets may appear as part of the type at the beginning of the declaration or as part of the array identifier:
int[] i; // array of int
byte b[]; // array of byte
Object[] o, // array of Object
short s[][]; // array of arrays of short

Constructing arrays
You can use the new operator to construct an array. The size of the array and type of elements it will hold have to be included. In the case of multidimensional arrays, you may specify the size only for the first dimension:
int [] marks = new int[100];
String[][] s = new String[3][];

Initializing arrays
An array initializer is written as a comma-separated list of expressions, enclosed within curly braces:
String s[] = { new String("apple"),new String("mango") };
int i[][] = { {1, 2}, {3,4} };
An array can also be initialized using a loop:
int i[] = new int[5];
for(int j = 0; j < i.length;j++){
i[j] = j;

Accessing array element
Arrays are indexed beginning with 0 and ending with n-1, where n is the array size. To get the array size, use the array instance variable called length. If you attempt to access an index value outside the range 0 to n-1, an ArrayIndexOutOfBoundsException is thrown.
I agree. Here's the link:
subject: Array declaration
It's not a secret anymore!