Sunday, 16 June 2013

How arrays are declared in Java? Explain with example?



Declaration of Array:
Array in Java may be declared in two forms :
1.       type   arrayname[ ];
2.      type   [ ] arrayname;
After declaring an array, we need to create it in the memory. Java allows us to create arrays using new operator only, as shown below :
Arrayname = new type[size];
Example :
number = new int[5];
average = new float[10];
These lines create necessary memory locations for the arrays number and average and designate them as int and float respectively. Now the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.

No comments:

Post a Comment