• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Why its not incrementing???

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i=1;

System.out.println(i=i++);
[ October 11, 2006: Message edited by: rimzy abdulkader ]
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what your problem is, but when I run this code it prints out 2.

So i is incremented.
 
rimzy abdulkader
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry there was some mistake in the code
try with the new one
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rimzy abdulkader:
sorry there was some mistake in the code
try with the new one



And where is the new one...
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,
Here you are using post increment operator. So i is printed first and then incremented..Try this
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi
The Operator you have used is post increment operator,
hence you will get incremented value after you finish the sentence.

but you have assigned the unincremented value again to the 'i'...

if you do like this, you may get incremented value.


regards
dina
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed. Summarized:

i++ has an postfix increment operator. When used in an expression, the value of i will be used in the expression first and when the expression is finished, then the value will be incremented.

++i has an prefix increment operator. When used in an expression, the value of i will be incremented first and then the value will be used in the expression.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all above,

I think some of them are misleading with their answers.

Petrus Pelser

I'm not sure what your problem is, but when I run this code it prints out 2.

So i is incremented.



int i = 1;
System.out.println(i=i++); //prints 1
System.out.println(i=i++); //prints 1
System.out.println(i); //prints 1

and as long as you do post increment of i and assign it to i itself, the new incremented value of i is never going to be assigned to i.

Regards,
Jothi Shankar Kumar. S
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please try it again.



Output will be: 1 2 3


[ October 13, 2006: Message edited by: Rajesh Kadle ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 1;System.out.println(i=i++); //prints 1System.out.println(i=i++); //prints 1System.out.println(i); //prints 1

out put will be 1 1 1 only.
Reason- we are not assigning the incremented value ot i. So value of i will not change.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The message was edited. The first code used pre-increment, thats why my answer was 2.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajesh,

I guess probably you did not compile the program and that you just guess the output to be 1,2 and 3.

Your quote,
code:

int i = 1;
System.out.println(i=i++); //prints 1 (i value before increment (1) is the result of the expression, so 1 is printed, after printing i is incremented so i value will be 2)
System.out.println(i=i++); //prints 2 (Same as above, 2 will be result of the expression so 2 is printed and i is incremented, i becomes 3)
System.out.println(i); //prints 3



You are right that i will be incremented to 2 but that incremented value is never assigned to i which means i still remains 1. Please don't mislead and show me just one java compiler where you get the output 1,2,3 for the above code.

Regards,
Jothi Shankar Kumar. S
 
Devidas Menon Aniyath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,
Please try the program to run in an Eclipse IDE and run it as a java application.
It will print 1,2,3
But if you are using simlple notepad and console it will print 1,1,1
that's why some has given answers as 1,2,3
Any way,I agree with your opinion that the output will be 1,1,1
and I don't know the reason about the other output
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In eclipse 3.2 using Java 5.0 it prints 1 1 1.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

I compiled the program using netBeans 5.0 and I'm getting 1,1,1...It is not dependent on the IDE that we use but rather the concept behind it. According to JLS, the output is 1,1,1. If not this ain't java.

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
It is not dependent on the IDE that we use but rather the concept behind it. According to JLS, the output is 1,1,1. If not this ain't java.




Most of us agree with you. But some are getting 1,2,3 - it's of interest to find out why.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

Could that be the problem with the IDE? When you try to run the same program from the command prompt, it gives correct results as 1,1,1 which is unanimous for the above program.

Regards,
Jothi Shankar Kumar. S
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I get the expected values 1 1 1 with Eclipse. I suspect that the ones getting 1 2 3 are running the wrong code.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to run this code and you'll see the results:



I think that after this example, there's no doubt anymore.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fl�vio ,

Thanks for posting code, so now there is distinction between 2 set of results.

However, it would be really helpful if some one can shed some light on fact that why we are getting 1 ,1 ,1 as result for first set.

My doubt is if we say x=i++; then value of i is incremented after expression evaluation and assigned to i.

However , when we say i=i++; then value of i should be incremented after expression evaluation and should be assigned to i. I am unable to figure out why post increment of value of i is not visible. I thought we will have one copy of i in memory so if that get updated with new value of i it should have been reflecting.

I think im unable to figure out in which order assignment and increment will be handle at runtime. Please provide some hints.

Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who do not believe that
results in i still being 0, I recommend reading Corey's Tipline article on Postincrement with Assignment.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this,

OMG, i really got "1,2,3" in eclipse3.1 with jdk5, and "1,1,1" in Console. Who can drop me a light!!!
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

That should be a problem with the IDE. Is there anyone who can get this striaght?

Regards,
Jothi Shankar Kumar. S
 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i appreciate that, Jothi
and i even got output "0" running this on my IDE

which seems to be correct while i got "0,1" running this on my IDE

It seems to be a problem of System.out.println(); in my Eclipse.
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Sorry for the late reply.

I am using Eclipse and Java 5.0. If you try the following code, as expected it is printing 1,1. Its printing 1,2 only when we include this assignment inside the 'println'.





I dont know why this is happening
[ October 16, 2006: Message edited by: Rajesh Kadle ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William and Rajesh,

I tried this on my system and I use netBeans 5.0,



And Im getting the output as below,

compile-single:
run-single:
0
0
0
0
0
0
0
0
0
0
BUILD SUCCESSFUL (total time: 0 seconds)

As you can see above...it just gives me 0 and nothing else.

Regards,
Jothi Shankar Kumar. S
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot reproduce the weird behaviours?



WebSphere Studio Application Developer 5.1.1 IE (Eclipse 3.1.2 + JRE 1.4.2) prints:

ObjectWeb Lomboz (Eclipse 3.2 + JRE 1.5.0) prints:

Command console (JDK 1.5.0) prints:

Command console (JDK 1.4.2) prints:

You're [s]all[/s] doing something wrong?
[ October 16, 2006: Message edited by: Bauke Scholtz ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bauke: "You're all doing something wrong?"

Only one (or is it two) posters are getting the wrong answer. I reckon they are not running the correct code somehow.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I was goaling on William + Rajesh indeed.
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When i tried from command console (JDK 1.4) i got the desired out put of (1 1 1) but when i tried it from Eclipse i got (1 2 3).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rajesh,
Can you delete your Eclipse project completely (delete all code and compiled classes) and start from scratch. Post the complete class you are running, not just the code in question. What Eclipse version are you using?

I just cut and pasted your code into Eclipse 3.2 and it works correctly (1 1 1). Even when using Eclipse's scratchpad to execute the code I get the same result (1 1 1).

I get the same (1 1 1) with Eclipse 3.1.2.
[ October 16, 2006: Message edited by: Barry Gaunt ]
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,

I created a new project and created the following program and ran the code. I am using eclipse version 3.1.0 and my Eclipse JDK compliance settings are as below:

Compiler Compliance Level : 1.4
Use default compliance settings: checked
Generated .class files compatibility: 1.2
source compatibility: 1.3

Code: Increment.java



Output:

1
2
3
1
1
1

In my colleague's machine with same Eclipse compliance settings, above program is showing the desired output of (1 1 1). I am not sure what is causing this, am extremely sorry for wasting rancher's valuable time. I will try to figure what is wrong with me (my pc).
[ October 16, 2006: Message edited by: Rajesh Kadle ]
 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my Eclipse is
Eclipse SDK
Version: 3.1.0
Build id: I20050627-1435

and i tried this
on my computer and !another computer!, both output "0,1".
but in the console, it output "0,0". CORRECTLY.
i dunno WHY.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, on Eclipse 3.1.2 it's all ones.
So it is a bug in the old Eclipse 3.1.0 version. Eclipse has its own compiler, it does not use the standard compiler from Sun. Perhaps some optimization is not working correctly. Maybe someone who has the time can search through Eclipse's bug list.

So you guys with the "funny" answer (1 2 3) better upgrade your Eclipse IDE.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of my collegues at work came up with this Eclipse Bug Report
 
William Yan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i downloaded eclpise 3.2.
and..
..the answer is CORRECT!
thanks to Jothi and Barry.
 
Rajesh Kadle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William,

The point here is that you understood the logic finally. All the credit goes to Barry.
Congrats Barry and thanks for bringing this long post to an end.

Regards,
Jothi Shankar Kumar. S
[ October 16, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic