Forums Register Login

PL HELP - URGENT

+Pie Number of slices to send: Send
Question ID :958005937570
The following class will print '2' when compiled and run.
class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++; // Line 1
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Ans: True
PROBLEM: why is the index 2 and not 3 . I understand that this is a postfix operator ++ but still at the end the value fo index shd be 3 i think.
Morever, i have not come accross any method call
which has an array dimension like line 1
Thanks in advance
padmini
+Pie Number of slices to send: Send
The ++ operator is not applied to 'index' but the result of getArray(). However that is null, so a nullPointer- or possibly a indexOutOfRangeException will be thrown, and caught by the catch-statement resulting in the printing of index (that previously had been assigned 2).
if you change the code to

You should get 3. See the difference?
/Mike
[This message has been edited by Mikael Jonasson (edited July 05, 2001).]
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 715 times.
Similar Threads
Confuse
Exception Question.
one more question on arrays only
How expression is working
Statement Exceution
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:43:06.