aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PL HELP - URGENT" Watch "PL HELP - URGENT" New topic
Author

PL HELP - URGENT

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;
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
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?
/Mike
[This message has been edited by Mikael Jonasson (edited July 05, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PL HELP - URGENT