# Operators

Chitra AP
Ranch Hand
Posts: 42
public class Oper1 {
public static void main(String[] args) {
int x = 0;
int y = 0;
x = x++ + y;
System.out.println("x = " + x);
System.out.println("y = " +y);
}

}

Please let me know why it prints 0 0. I thought it would print 1 0.

Thanx

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
look at it like this... we get to the line:

x = x++ + y;

the first thing we do is figure out what x and y are. they are both 0. add them together, you get 0.

now, increment x. x = 1.

now assign the result of your computation to x.

x becomes 0.

Joe Sondow
Ranch Hand
Posts: 195
The tricky line is
x = x++ + y;

The important thing to know is that x++ is using the postfix ++ operator, not the prefix ++ operator, so the incrementing will be done after the expression evaluates to 0.

First, the operands x++ and y get evaluated. x was 0 before this line, so x++ evaluates to 0 and then increments the value of x to 1. y evaluates to 0. So the right-hand side of the assignment operator (=) evaluates to (0 + 0), which evaluates to 0. Then x (which was set to 1 for a moment) gets assigned that 0 value because of the = operator. So at the end of the statement, x gets assigned the value 0.

marc weber
Sheriff
Posts: 11343
Also consider that Java ignores spaces. So x = x++ + y; could be written as x = x+++y; in which case it's not as clear (to a human reader) what's happening.

So ask yourself this: Why does Java interpret x+++y as (x++)+y rather than x+(++y)?

fred rosenberger
lowercase baba
Bartender
Posts: 12196
35

x=x+++++y;

that's pretty cool!!!

Ranch Hand
Posts: 97
I think because precedence of post increment is more then pre increment that is why x = x+++y is treated as x = x++ + y; as (x++)+y rather than x+(++y)
Am I right??

but in x=x+++++y;
I thought it should be x = (x++) + (++y);

but compiler error

unexpected type
required: variable
found : value
x=x+++++y;
^
why???

soni.
[ June 01, 2005: Message edited by: Soni Prasad ]

amit taneja
Ranch Hand
Posts: 813
as for marc

So ask yourself this: Why does Java interpret x+++y as (x++)+y rather than x+(++y)?

java starts parsing the expression from left to right
so whenever it gets x++ it puts as (x++)and then looks for other operator
if next operator is +y (like x+++y) then it assing next expresion as +y

but if it get expression as (x++++y) (four plus)
it add the same way i explain above but as in this case it didn't get ++operatoer to join with y it do like this ( x++ + (+y))

but in x=x+++++y;
in this case it gets increment operator (++y)
so it puts like x = (x++) + (++y);

hope i clear it.....

one more thing the expresion +-+-+-+-+1 or +-+-+-+-+x ?
any idea

guesss.... i got conused when i see code i thought it will give compiler error but it gets compiled...
it works as from left to right with methamatics
+-=-
-+=-
++=+
--=+

so the expresion get solve from left to rt. with respected calculation so

+-+-+-+-+1 = +1

hope it helps....

now the final conculsion for code like this
int x = 0;
int y = 0;
x = x++ + y;

i think we have to remember that

final result will have seprate calculation memory area
and for primitve other one...
final result will always be assinged at last

as fred explain
like this

the first thing we do is figure out what x and y are. they are both 0. add them together, you get 0.

now, increment x. x = 1.

now assign the result of your computation to x.

here final result is assinged at last after primitive increment which is overided by final result...

...

marc weber
Sheriff
Posts: 11343
Originally posted by amit taneja:
... but in x=x+++++y;
in this case it gets increment operator (++y)
so it puts like x = (x++) + (++y);
...

I don't think so. Compare the results of trying to compile the following different cases:
x = x+++++y;
x = ((x++)++)+y;
x = (x++)+(++y);

You might also try compiling the following variations:
x = x++++y;
x = (x++)+(+y);

And consider the significance of unary operator precedence over binary operators (e.g., ++ over +).
[ June 02, 2005: Message edited by: marc weber ]

amit taneja
Ranch Hand
Posts: 813
Originally posted by marc weber:

I don't think so. Compare the results of trying to compile the following different cases:
x = x+++++y;
x = ((x++)++)+y;
x = (x++)+(++y);

And consider the significance of unary operator precedence over binary operators (e.g., ++ over +).

[ June 02, 2005: Message edited by: marc weber ]

so which is write
x = x+++++y;
x = ((x++)++)+y; //this is right or
x = (x++)+(++y); // this is right

actually when trying to compile the following program

class increment
{
public static void main(String args[])
{
int x=0;
int y=0;
int l=0;
l=x+++++y;
System.out.println("x="+x +" y= "+y+"l= "+l);
}
}

following error is comming..

D:\java_prac>javac increment.java
increment.java:9: unexpected type
required: variable
found : value
l=x+++++y;
^
1 error

why so ?
and also giving error when compiling
l=((x++)++)+y);

its not showing error when compiling like
l=(x++)+(++y);

???

amit taneja
Ranch Hand
Posts: 813
still waiting for response ...

marc weber
Sheriff
Posts: 11343
Originally posted by amit taneja:
...
x = x+++++y;
x = ((x++)++)+y; //this is right or
x = (x++)+(++y); // this is right
...

Based on the similar compilation errors, it appears that x+++++y is being interpreted as ((x++)++)+y. Unfortunately, I can't explain exactly why.

Note that section 15.7.2 of the Java Language Specification states, "The Java programming language also guarantees that every operand of an operator ... appears to be fully evaluated before any part of the operation itself is performed."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18740

From this, one might reason that Java tries to associate with the left operand as many unary operators as possible from left to right before interpreting a binary operator. So x is followed by a unary ++, and then another ++ before the binary +.

I hope someone with more expertise is able to clarify.
[ June 13, 2005: Message edited by: marc weber ]