Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How expression is working

 
Vidya Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test1
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}

I did not understood how above program is working. As how syntax of
getArray()[index=2]++; is correct. As per me getArray() is a static function that is returning an int array.

Also, getArray()[index=2]++; is working but when i replace this line with
getArray()[index=2];
or
getArray()++[index=2];
then the program is not being compiled.

Thanks,

Vidya
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vidya SIngh:
I did not understood how above program is working. As how syntax of
getArray()[index=2]++; is correct. As per me getArray() is a static function that is returning an int array.




This (I think) defines a function returning an array of ints, and is written wrong because it returns null - I expect this to get some compiler information, from which you may be able to make some forward progress. There are fundamental errors in you code that suggest shorter programs to start off with.

The program begins with:

and continues with:

getArray();// would have to have the operator " = " on the left hand side of the words " getArray " The [index = 2] assigns two to the variable index, which would have to access an array:


Your code does not say how many items you want in the array, so I chose 32 becuase it is someplace to start.

As an accident of historical origins which will not likely get a lot of fixes in the immediate time, all array's first element are always indexed at zero. Thus:


The following code will neither work nor compile.


[ May 06, 2007: Message edited by: Nicholas Jordan ]
[ May 06, 2007: Message edited by: Nicholas Jordan ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"index=2" is an assignment statement that gives the variable index the value 2. As a convenience of sorts, assignment statements are also functions that return or take on the value assigned. So "index=2" has a value of 2 and that line is addressing the int in an array with the index of 2.

Now I'm not sure what you mean when you say this "works". It compiles ok but it throws a null pointer exception which you catch and ignore. Removing the ++ doesn't compile because you've referenced the array element with the index of 2 but you haven't done anything with it. Try using it somehow:

int x = getArray()[index = 2];

Now we've assigned variable x the same value as third element of the array.

To make it even more interesting, get rid of the null pointer exception. Make your getArray() function return something useful like:

return new int[] { 0, 1, 2, 3 };

What do you think x will be with the ++? Without the ++?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic