• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator problem

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Bill
i got the funda. Appreciate your reply.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic