Variables in JAVA before reading this post].In few scenarios, more than one value needs to be stored under a single variable. This can be achieved by means of an Array.Array can store more than one element.Array is a data structure which stores multiple elements under the same variable.Arrays is used to store the elements of same data type.The size of the array is fixed and it cannot be changed. The size of the array is mentioned during the time of declaring the array.
In java,Arrays can be used to store the primitive types and also reference types.
Declaring arrays:
datatype[] ArrayName; [or] datatype Arrayname[];
Where ArrayName is the name of the array.
e.g:
Declaring an array
Primitive type array declaration
int IntArray[]=new int[3];
int[] IntArray1;
IntArray1 = new int[2];
Reference type array declaration
Loan[] EducationalLoan =new Loan[2];
Loan HousingLoan[];
HousingLoan=new Loan[3];
Initializing Declared arrays.
IntArray[0]=1;
IntArray[1]=2;
IntArray[2]=3;
IntArray1[0]=1;
IntArray1[2]=1;
EducationalLoan[0]=new Loan();
EducationalLoan[1]=new Loan();
HousingLoan[0]=new Loan();
HousingLoan[1]=new Loan();
HousingLoan[2]=new Loan();
Declaring and initializing the array in one go
int[] IntArray2 ={1,2,3,4,5,6};
Loan[] PropertyLoan={new Loan(),new Loan(),new Loan(),new Loan(),new Loan()};
A variable is used to store a single value [Suggest you to read In java,Arrays can be used to store the primitive types and also reference types.
Declaring arrays:
datatype[] ArrayName; [or] datatype Arrayname[];
Where ArrayName is the name of the array.
e.g:
Declaring an array
Primitive type array declaration
int IntArray[]=new int[3];
int[] IntArray1;
IntArray1 = new int[2];
Reference type array declaration
Loan[] EducationalLoan =new Loan[2];
Loan HousingLoan[];
HousingLoan=new Loan[3];
Initializing Declared arrays.
IntArray[0]=1;
IntArray[1]=2;
IntArray[2]=3;
IntArray1[0]=1;
IntArray1[2]=1;
EducationalLoan[0]=new Loan();
EducationalLoan[1]=new Loan();
HousingLoan[0]=new Loan();
HousingLoan[1]=new Loan();
HousingLoan[2]=new Loan();
Declaring and initializing the array in one go
int[] IntArray2 ={1,2,3,4,5,6};
Loan[] PropertyLoan={new Loan(),new Loan(),new Loan(),new Loan(),new Loan()};
Sign up here with your email
ConversionConversion EmoticonEmoticon