File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators" Watch "Operators" New topic
Author

Operators

Chitra AP
Ranch Hand

Joined: May 25, 2005
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

Joined: Oct 02, 2003
Posts: 11422
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
marc weber
Sheriff

Joined: Aug 31, 2004
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)?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

oooohhh!!! how about

x=x+++++y;

that's pretty cool!!!
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
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 ]

SCJP 1.4, SCBCD 1.3
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
as for marc

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


the answer is simple....
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...

...

do add any comments on my post


Thanks and Regards, Amit Taneja
marc weber
Sheriff

Joined: Aug 31, 2004
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

Joined: Mar 14, 2003
Posts: 810
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);


???
pls reply
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
still waiting for response ...
marc weber
Sheriff

Joined: Aug 31, 2004
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 ]
 
Consider Paul's rocket mass heater.
 
subject: Operators