• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How expression is working

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ++?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks Marshal. Hope Stan James got kick out of such syntax.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic