This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How expression is working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How expression is working" Watch "How expression is working" New topic

How expression is working

Vidya Singh

Joined: Mar 21, 2007
Posts: 28
class Test1
public static int[ ] getArray() { return null; }
public static void main(String[] args)
int index = 1;
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
then the program is not being compiled.


Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
"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 ++?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: How expression is working
It's not a secret anymore!