An array is a container object that holds a fixed number of values of a single type. we can store similar data types .The length of an array is established when the array is created. After creation, its length is fixed.
Like declarations for variables of other types, an array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type.
Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

Ex. :    int[] arr=new int [5];

in the above example we are declaring an int type array with size 5. so we can insert only int variables and only 5 variables if we will try to store 6th variable it will throw an exception.
we can store the elements in array like the above example and always  remember array worked based on index and index always start from 0.
we also can declare the array like this .
ex 2 : int arr[] ={3,45,7,3,8,9,54};

We can print the array using for-each [for(int n:arr)]  loop . here int is the type of array . n is the variable where we will store the temporary elements which we are retrieving from the array .

Take The Input size of array and elements of array at  runtime from the user and than print the array.

import java.util.*;
import java.util.Scanner;

public class TestArray
public static void main(String[] args)
Scanner scn =new Scanner(;
System.out.println("type size of array");
int n=scn.nextInt();
int [] arr=new int[n];
for(int i=0;i<n;i++)
for(int m:arr)



Post a Comment