• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

-ve array subscript

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this allowed?

int[][] a = new int[5][-1];

How come the compiler allows it but ends up in runtime exception - NegativeArraySizeException?

what exactly are the things the compiler checks in a declared array?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Krishnan:
Is this allowed?

int[][] a = new int[5][-1];

How come the compiler allows it but ends up in runtime exception - NegativeArraySizeException?

what exactly are the things the compiler checks in a declared array?




Object creating is the matter of runtime. What compiler can check is for the
proper syntax allowed in Java as:

int [3] arr; //Compiler error, because virtual machine does not allocate
space until you actually instantiate the array object.

int[] arr = new int[]; //compiler error, missing dimension
int[] [] arr = new int[][3]; //compiler error too
int[] = new int[3]{12,3,4};//compiler error

So these are some cases where compiler can check us, but providing negative
array size or size beyond the array scope can only be determined at run
time. And there are exceptions to be thrown if any of such cases occur.


Thanks,
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic