wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initialization of arrays" Watch "Initialization of arrays" New topic
Author

Initialization of arrays

sudha siva
Ranch Hand

Joined: Sep 18, 2001
Posts: 30
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

Joined: Jan 24, 2002
Posts: 74
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


Sun Certified Programmer for Java 2 Platform
Raghav Sam
Ranch Hand

Joined: Apr 12, 2001
Posts: 412
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.


Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.<br />- Dr. Seuss
Uday Kumar
Greenhorn

Joined: Jan 22, 2002
Posts: 24
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

Joined: Dec 18, 2001
Posts: 61
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


SCSecA,SCNA,SCSA,SCWCD,SCJP
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
This being a 2-dimensional array it means that the 2 row remains unconstructed!!
as the reference to the 2 row is null.
swapna


SCPJ2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initialization of arrays