Array:
- Primitive variable store only one value at a time.
- Array variable can store more than one element of same type.
- For example marks[] = {56, 45, 90, 67, 56};
- Array elements store in side by side locations
- Array elements can access using index starts from 0 to length-1;
Array creation:
int[] arr = {3, 7, 1, 9, 5, 2, 7, 6}; |
Memory representation:

Find the Length of Array:
length:
- ‘length’ variable returns length of array.
- ‘length’ is a pre-defined instance variable.
- ‘length’ variable invokes on array object.
class Code { public static void main(String[] args) { int[] arr = {4, 2, 8, 9, 1, 6, 7, 4}; System.out.println(“Length is : ” + arr.length); } } |
Array Object:
- Array is an object in java.
- We can create empty array using ‘new’ keyword.
Creating empty array with specific size as follows
class Code { public static void main(String[] args) { int[] arr = new int[7]; System.out.println(“Lengt is : ” + arr.length); } } |
Note:
- If we just declare the array then memory will not be allocated to array.
- We cannot perform any operations of array without memory.
- For example, finding length to array gives error
class Code { public static void main(String[] args) { int[] arr; System.out.println(“Lengt is : ” + arr.length); } } |