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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Help" Watch "Array Help" New topic
Author

Array Help

Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
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.


SCJP 1.4, SCWCD 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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?


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
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

Joined: Aug 26, 2001
Posts: 7610
That's ok Pardon my question.
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
i am still not clear which one go with.. Error Or exception
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Sep 17, 2002
Posts: 202
Thanks for the clarification Valentin .

Reshma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Help
 
Similar Threads
a confused concept
ints and arrays........
Method Resolution
pass by value
Question Mock