aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator problem 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 "Operator problem" Watch "Operator problem" New topic
Author

Operator problem

Amit Trivedi
Greenhorn

Joined: Feb 04, 2001
Posts: 25
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Can we expact questions on these topics
- javadoc
- code in a html file for Applet
Mock exam by JohnJunt is covering above topics. Please guide.
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
The answer to the operator part of your question lies in the sequence of events. With i = i++; I believe the sequence is:
1) Determine the value of the expression "i++", which is 9.
2) Increment i, making it 10.
3) Store the value of the expression (9) into i, putting it back to 9.
However, with i = ++i; the sequence is:
1) Increment i to 10.
2) Determine the value of the expression: 10.
3) Store the value into i, making it 10.
Pre-increment vs. post-increment (and -decrement) is an important distinction. Think of it this way: pre-increment first increments the value, then yields that new value, whereas post-increment yields as its value the old value and then "afterward" increments the variable.
Amit Trivedi
Greenhorn

Joined: Feb 04, 2001
Posts: 25
Thanx Bill
i got the funda. Appreciate your reply.
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
Hi,
Amit i think this one we can explain in very easy way. Look it is very important when you get job in any company if employer
finds that you are making mistake in this code increment
you will be fired immediately from the job.
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Bcoz , i=i++ means i first assign its value to i then
it will start increments. So i==9

Second i=++i means i first be incremented then it will
assign its value to i. So i==10
Got it,
- Golam Newaz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator problem