Forums Register Login

Array Help

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
That's ok Pardon my question.
+Pie Number of slices to send: Send
i am still not clear which one go with.. Error Or exception
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Thanks for the clarification Valentin .

Reshma
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 698 times.
Similar Threads
Question Mock
ints and arrays........
pass by value
Method Resolution
a confused concept
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:15:12.