Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How expression is working  RSS feed

 
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 ++?
 
Mian Amjad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this line:
getArray()[index=2]++;
the only confusing part for me is getArray()[index=2] syntax. The array index is not passed as a parameter as usual here. Is this a new syntax?
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan James died about nine years ago, so this is all old code, not new syntax at all.
It is syntax which might pass the javac tool's surveillance, but will guarantee dismissal if used at work. You need to go through that horrible expression left to right. You start with a method call, which returns an array, so you can replace
getArray()
with
someArrayOrOther
That means you can use the [] operator to find the elements corresponding to its indices.
The you are passing the new value of index after reassigning it inside the expression
So you are finding the 3rd element of the array and (assuming it is a number) applying the ++ operator to it. You can start with an array [0, 1, 2, 3] and later have it [0, 1, 3, 3]. Only if that method returns null, you will get a nice Exception before that.
 
Mian Amjad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks Marshal. Hope Stan James got kick out of such syntax.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!