Ben Ethridge

Ranch Hand

Posts: 108

posted 14 years ago

Hi, all.

Why is j 1, not 0? I thought that all post-operators acted

after the expression was complete. So now my question is:

What's really meant by an "expression"?

class OpTest {

public static void main(String []args){

int i = -1;

int j = i++ + ++i + +i--;

System.out.println(j);// j is 1.

}

}

I found this on a JCertify mock exam question. I couldn't find a good explanation, though.

Respectfully,

Ben

Why is j 1, not 0? I thought that all post-operators acted

after the expression was complete. So now my question is:

What's really meant by an "expression"?

class OpTest {

public static void main(String []args){

int i = -1;

int j = i++ + ++i + +i--;

System.out.println(j);// j is 1.

}

}

I found this on a JCertify mock exam question. I couldn't find a good explanation, though.

Respectfully,

Ben

Corey McGlone

Ranch Hand

Posts: 3271

posted 14 years ago

Charles,

You can do a search in this forum to find a lot of examples of this, but let me try to explain.

The operands of the expression are evaluated from left to right. We start with this:

First, we evaluate the leftmost operand and we have this:

Notice that, because this was a postincrement expression, we insert the initial value of i, -1, and then increment i to make it 0. We then go on to the next operand:

Now we have a pre-increment expression so we increment i to 1 and then insert it into the expression. Finally, we have:

This time, we have a post-decrement operator so we insert the initial value of i, 1, and then decrement i to 0.

So, that gives you the result of j=1.

I hope that helps,

Corey

You can do a search in this forum to find a lot of examples of this, but let me try to explain.

The operands of the expression are evaluated from left to right. We start with this:

First, we evaluate the leftmost operand and we have this:

Notice that, because this was a postincrement expression, we insert the initial value of i, -1, and then increment i to make it 0. We then go on to the next operand:

Now we have a pre-increment expression so we increment i to 1 and then insert it into the expression. Finally, we have:

This time, we have a post-decrement operator so we insert the initial value of i, 1, and then decrement i to 0.

So, that gives you the result of j=1.

I hope that helps,

Corey

Ben Ethridge

Ranch Hand

Posts: 108

posted 14 years ago

Hmmm...Sounds like you are saying post-increment/pre-increment happens sometime during the "expression" evaluation as opposed to before/after it

My understanding of java so far is that:

a+=b+=c means a+=(b+=c) (right to left), but d=a+b+c means (a+b)+c (left to right). So I was hoping that an a++ or --b stuck in there, would take place after or before the ENTIRE "expression"

This leads back to my orig question: What's an "expression" in java? Is a+b+c one expression? or two? I've looked through several books but can't find a good clear definition yet, and how that relates to pre/post-decrement.

Respectfully,

Ben

My understanding of java so far is that:

a+=b+=c means a+=(b+=c) (right to left), but d=a+b+c means (a+b)+c (left to right). So I was hoping that an a++ or --b stuck in there, would take place after or before the ENTIRE "expression"

This leads back to my orig question: What's an "expression" in java? Is a+b+c one expression? or two? I've looked through several books but can't find a good clear definition yet, and how that relates to pre/post-decrement.

Respectfully,

Ben

Ben Ethridge

Ranch Hand

Posts: 108

posted 14 years ago

Ok, cowpeople. I think I have the answer, or part of it anyway. None of this stuff is very difficult, once you get your terminology right. Part of my confusion was that my java book shows this:

y=x+z

as an "expression", or implies that it is anyway. It isn't, or at least that isn't the whole story.

If I now understand the meaning of the word "expression" in java, both x and z above and x++ and z-- below (and maybe y as well??) are considered expressions:

y=x++ + z--

... and you can have expressions within expressions within expressions, (sort of, as you will see later below):

y = ((x++ + z--)+ a);

My confusion was that all this time, I thought the whole "statement" was the "expression".

Interesting that this doesn't compile:

int i4 = (i1++ + i2--)--;

...so I guess () isn't always considered an "expression". Maybe it is never considered an expression?

Once you know what constitutes an "expression", at least in my orig example, the computation is relatively easy to make, but honestly, I still can't find a good clear definition (hopefully with examples) of "expression" in java...

...or "statement" for that matter :-)

Is there a javaranch glossary?

"Wherefore art thou 'Romeo'?"

Respectfully,

Ben

y=x+z

as an "expression", or implies that it is anyway. It isn't, or at least that isn't the whole story.

If I now understand the meaning of the word "expression" in java, both x and z above and x++ and z-- below (and maybe y as well??) are considered expressions:

y=x++ + z--

... and you can have expressions within expressions within expressions, (sort of, as you will see later below):

y = ((x++ + z--)+ a);

My confusion was that all this time, I thought the whole "statement" was the "expression".

Interesting that this doesn't compile:

int i4 = (i1++ + i2--)--;

...so I guess () isn't always considered an "expression". Maybe it is never considered an expression?

Once you know what constitutes an "expression", at least in my orig example, the computation is relatively easy to make, but honestly, I still can't find a good clear definition (hopefully with examples) of "expression" in java...

...or "statement" for that matter :-)

Is there a javaranch glossary?

"Wherefore art thou 'Romeo'?"

Respectfully,

Ben

It is sorta covered in the JavaRanch Style Guide. |