• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization of arrays

 
sudha siva
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,
Please have a look at the following code.

class Test {
public static void main(String[] args) throws Throwable {
int ia[][] = { { 1 , 2}, null };//line1
int ja[][] = (int[][])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}
Can we assign a null value to the array of int type.

Thanks
sudha
 
Travis Benning
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so.
I think null is only to be used for object, not a numeric.
Hope this helps, if not, wait for a ranch hand to answer.
TB
 
Raghav Sam
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sudha siva:
Hai All,
Please have a look at the following code.

class Test {
public static void main(String[] args) throws Throwable {
int ia[][] = { { 1 , 2}, null };//line1
int ja[][] = (int[][])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}
Can we assign a null value to the array of int type.

Thanks
sudha

Yes. Since all arrays are also objects, you can initialize an array with a null reference.
 
Uday Kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudha,
The code is declaring and initialzing an array of array. So, each of the first arrays' elements will hold reference to the inner (second dimensional) array. And that's where null is pointing to.
HTH
 
sun ram
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array is inherited from the Object class. So we can have the null value.
-SR
Originally posted by Uday Kumar:
Sudha,
The code is declaring and initialzing an array of array. So, each of the first arrays' elements will hold reference to the inner (second dimensional) array. And that's where null is pointing to.
HTH
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This being a 2-dimensional array it means that the 2 row remains unconstructed!!
as the reference to the 2 row is null.
swapna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic