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.

Comments

Popular posts from this blog

Write a program to add two number using inline function in C++?

Traversing of elements program with algorithm and Flowchart