This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

increment operator  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code :
class ABC
{
public static void main(String gh[])
{
int i=0
i=i++
System.out.println(i);
}
}
Guess the output !!!
Its 0 even after incrementing befre printing. Why guys, I wanna
know !!!
Help out.
regards,

[This message has been edited by Sam (edited November 06, 2000).]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam!
In ur program u replace i++ to ++i and see the output.
The result u get is obviously 1.
The reason is that in the case of
i=i++
'i' is assigned the value and then incremented.
in the case of
i=++i;
'i' is incremented and then the value is assigned.
Hope ur doubt is clear.
Good Luck
Preethi M
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer lies in order of evaluation of expression.
1. it will always evaluate expression from left to right.
2. also while evluating oprand if operand is applied a post unary oprator then first oprand is calculated and then immediately after that unary opration is performed.
3. finaly the assignment takes place.
so for
i=i++
it goes this way
1. i=0
2. immediately it performs i++
3 then assignment so i again becomes 0.
so now tell me what is the value of i for this
int i = 0
i = ++i + i++

well it is 2.

Originally posted by Sam:
Consider the following code :
class ABC
{
public static void main(String gh[])
{
int i=0
i=i++
System.out.println(i);
}
}
Guess the output !!!
Its 0 even after incrementing befre printing. Why guys, I wanna
know !!!
Help out.
regards,

[This message has been edited by Sam (edited November 06, 2000).]


 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajesh,
I agreed with your explanation and compiler too. but my question is at step 2 after performing i++, where it will store the result of the operation.
Kind Regards
jaydeep
so for
i=i++
it goes this way
1. i=0
2. immediately it performs i++
3 then assignment so i again becomes 0.
so now tell me what is the value of i for this
int i = 0
i = ++i + i++

well it is 2.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually while incrementing the value is stored
in a temp variable.
so if i say
int i=0;
j=i++;
i is incremented & stored as temp=1
& only in next command assigned to j
so if we give
System.out.print(j);
it gives 1
however
if we give i=i++ & then print the same i the temp
dosent come into the picture hence the absurdity
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry .. but I still cant understand how this works. Can someone pleeeeease explain this to me ?
 
jairajiv
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I wasnt able to understand why it works that way .. I got how to arrive at the answer thro the discussion in http://www.javaranch.com/ubb/Forum24/HTML/000775.html. Great job Maha !!

Originally posted by jairajiv:
Sorry .. but I still cant understand how this works. Can someone pleeeeease explain this to me ?


 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!