• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jdk compiler maturity

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does a code like this result in compile time error of Index Out of Bound error

public class A
{
public static void main(String[] args)
{
String[] test = new String[10];
test[1000] = "iii";
}
}


I understand the code would have been test[k] = "iii"; where k is some other variable. However in this case it is a direct number 1000.

I tried to compile in both Sun JDK and Oracle JRockit but both of them does not complain at compile time.


Regards,
Nagendra

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does a code like this result in compile time error of Index Out of Bound error


Are you sure about "compile time error" ?

I tried to compile in both Sun JDK and Oracle JRockit but both of them does not complain at compile time.


So with which compiler are you getting a compile time error ??
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raja Nagendra Kumar wrote:Why does a code like this result in compile time error of Index Out of Bound error


Its a runtime not compile time error.

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compare an array to a street. The street has 10 houses. You then try to go to the 1001st house. Doesn't make sense, does it? It doesn't make sense to the JVM either so it throws an ArrayIndexOutOfBoundsException.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the OP is asking why, since it's "obvious" that the index is out-of-bounds, the compiler doesn't complain.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah right. I think because that wouldn't be as easy as it seems. The size of the array can change; if I add just one line between these two statements it would become valid all of a sudden:
Or even this:
The compiler cannot keep track of all possible values a variable (including arrays) can have, especially if the value can come from a method call, that perhaps delegates to another method, etc.

During runtime the actual value is known and therefore it is easier to check.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this:



There is no possible way the compiler can know what the size of the returned array will be. Yes, in some trivial cases, it might be able to determine such things, but since it can't all the time, my guess is they said "why bother?"
 
reply
    Bookmark Topic Watch Topic
  • New Topic