Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Initialization of arrays  RSS feed

 
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
 
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
 
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.
 
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
 
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

 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!