If we are creating an array object then it will allocate memory and default value i.e zero is been set by the JVM in the allocated memory.

If we want to store the value at the time of declaring the array reference then we have the following statement,

< data_type >  < var_name > [  ]  =  {< value1 > , < value2 >,< value3 >.....} ;

int  value [ ] =  {  10, 20, 30 } ; // Valid statement 

int  value [ ] =  {  10, 20 ,30,  , } ; // Invalid statement 

int  value [ ] ;
value =  {  10, 20 ,30 } ; // Invalid statement it should be in one line .


 Anonymous Array 

Now if we have a requirement to assign the data at the time of creating the array object then we have the following statement and this is also known as Anonymous Array .

int  value [ ] =  new int[ ] {  10, 20, 30 } ; 

We can also assign one reference to another reference variable. Like this,

int  tmp [ ] ;
tmp = new int [ ] { 10,20,30 } ;

Note : We should not specify any size while creating the anonymous array . 

Important points must be taken care while using array in java

1> We cannot assign one reference variable to another at the same level or who does not have any inheritance relation . For ex-

int  arr [  ]  = new  byte [ 4 ] ;  // Invalid cannot store byte into int type . 

2> We cannot use + operator with the array reference for indicating the next member of the array because java do not deals with the address at the user level.

int  arr [  ]  = new  int [ 4 ] ;
( arr + 1 ) ;                          // Invalid in Java

3> Cannot assign direct a string value to a array reference . For ex-

String  arr  [ ] = " Java " ; 

The above statement is invalid and give compilation error saying " incompatible type " because compiler find the Java as a string type which is one of the class in java where as on the left side we have a String array. So it is " incompatible type " . Correct form is as following ,

String  arr  [ ] = { " Java " }  ; 

Now Java is a string is of array type. 

Related Posts:

  • Anonymous Array in Java If we are creating an array object then it will allocate memory and default value i.e zero is been set by the JVM in the allocated memory. If we want to store the value at the time of declaring the array reference then we h… Read More
  • Introduction to Array in Java Like all other programming languages , in java also it is defined as the collection of homogeneous data ( similar type of data ) but with different implementation that means in java Array is a reference type, which stores th… Read More
  • Multi-Dimension Array In java Multi-dimension array defined as the array of array . Multi-dimension include two and three dimensions array. 2 dimension array --- > Collection of  1 Dimension array . 3 dimension array --- > Collection… Read More
  • Assign one array reference into another Let us discuss about how to assign a array reference into another array reference and how the memory is being created and how value are being stored in the array. Let me make you more clear by taking an example , int  … Read More

1 comment:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic