Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Help

 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test{
static int[] iArr;

public static void main(String[] args){
System.out.println(iArr[0]);
}
}
The above code when executed on a linux system having JDK1.3.1, gives "Segementation Fault", if the code is executed on windows system with jdk1.4 gives NullPointerException.
Could someone let me know why it is behaving differently on different operating systems.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Segmentation Fault (probably because we are trying to access a component of an uninitialized array) is an error occuring at the Linux level. NullPointerException (because iArr is null) is occuring at the Java level. Maybe there is a difference in how different OS handle similar error conditions. All this is JVM implementation-dependent
PS: How is this question related to the SCJP exam?
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this question on some site for java cert(donot rmember which one), asking what would be the result of invoking the class... And was confused when i came across the different runtime results and wnated to confirm which one was to go with.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's ok Pardon my question.
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am still not clear which one go with.. Error Or exception
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct behavior (as per JLS 10.6) is to throw a NullPointerException at runtime since the iArr reference variable is not referencing any array object on the heap, and thus, has a null value. You should never have to deal with OS-level error at the Java level.
[ February 18, 2003: Message edited by: Valentin Crettaz ]
 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification Valentin .

Reshma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic