Thursday 28 January 2016

JAVA ARRAY

Normally, array is a collection of similar type of elements that have contiguous memory location.
Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based, first element of the array is stored at 0 index.
java array

Advantage of Java Array
  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.
Disadvantage of Java Array
  • Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at run time. To solve this problem, collection framework is used in java.

Types of Array in java
There are two types of array.
  • Single Dimensional Array
  • Multidimensional Array
Single Dimensional Array in java
Syntax to Declare an Array in java
dataType[] arr; (or)  
dataType []arr; (or)  
dataType arr[]; 

Instantiation of an Array in java

arrayRefVar=new datatype[size]; 

Example of single dimensional java array
Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.

class Testarray{  
public static void main(String args[]){  
  
int a[]=new int[5];//declaration and instantiation  
a[0]=10;//initialization  
a[1]=20;  
a[2]=70;  
a[3]=40;  
a[4]=50;  
  

//printing array  
for(int i=0;i<a.length;i++)//length is the property of array  
System.out.println(a[i]);  
  
}}
 

Wrapper class in Java

Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known and autoboxing and vice-versa unboxing.

No comments:

Post a Comment