File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PL HELP - URGENT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PL HELP - URGENT" Watch "PL HELP - URGENT" New topic


padmini Babu
Ranch Hand

Joined: Feb 10, 2001
Posts: 103
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;
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
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
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?
[This message has been edited by Mikael Jonasson (edited July 05, 2001).]
I agree. Here's the link:
subject: PL HELP - URGENT
It's not a secret anymore!