Posts

Showing posts with the label arrays

arrays in java with example

The main definition of an array is, an array represents the group of elements.  In Java, array plays a key role. The main purpose of the array is to hold a fixed set of elements. Why I said fixed set is, while creating array we have to define the size of the array. So, that array can hold only that no of elements in it. For example, when we have created main() method in our program, you can observe there is a String[] array. Otherwise, JVM can't understand and it will not consider that main method as starting point of the application. How to create an array in Java? The below are two different ways of creating an array in java. In the first way, you have to pass the elements between the braces {}.  int [] ar = {}; or int [] ar = new int [ 10 ]; As I have already mentioned, array represents the group of elements. Those elements should be of same type. Otherwise compiler will not allow to add. But, there is a possibility of casting. For example, I have an