An array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++. Following are some important point about Java arrays.

  • In Java all arrays are dynamically allocated.
  • Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.
  • A Java array variable can also be declared like other variables with [] after the data type.
  • The variables in the array are ordered and each have an index beginning from 0.
  • Java array can be also be used as a static field, a local variable or a method parameter.
  • The size of an array must be specified by an int value and not long or short.

Array Declaration

  1. int[] arr = new int[20];
  2. int[] arr = new int[]{1,2,3,4,5};
  3. int[] arr = {1,2,3,4,5};

Example1

           

Example2

Output:

Enter 5 numbers

20

15

16

10

25

Maximum value is: 25

Minimum value is: 10