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

Why its not incrementing???

rimzy abdulkader
Greenhorn

Joined: Oct 04, 2006
Posts: 18
int i=1;

System.out.println(i=i++);
[ October 11, 2006: Message edited by: rimzy abdulkader ]

SCJP 5.0
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
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

Joined: Oct 04, 2006
Posts: 18
sorry there was some mistake in the code
try with the new one
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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


And where is the new one...


The Best way to predict your future is to create it - Every great individual common man
Devidas Menon Aniyath
Greenhorn

Joined: Apr 28, 2006
Posts: 12

Hi,
Here you are using post increment operator. So i is printed first and then incremented..Try this


Anything acquired without effort, and without cost is generally unappreciated, often discredited<br />-:Napoleon Hill
Dhinakaran
Greenhorn

Joined: Oct 06, 2006
Posts: 2

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
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
Please try it again.



Output will be: 1 2 3


[ October 13, 2006: Message edited by: Rajesh Kadle ]

-Raj
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
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.


Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
The message was edited. The first code used pre-increment, thats why my answer was 2.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Apr 28, 2006
Posts: 12
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In eclipse 3.2 using Java 5.0 it prints 1 1 1.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Aug 03, 2002
Posts: 7729
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Aug 03, 2002
Posts: 7729
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.
Fl�vio Albuquerque Camilo
Greenhorn

Joined: Aug 29, 2005
Posts: 1
Try to run this code and you'll see the results:



I think that after this example, there's no doubt anymore.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
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


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For those who do not believe that
results in i still being 0, I recommend reading Corey's Tipline article on Postincrement with Assignment.
William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69
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!!!


Java is better and better, yet bitter and bitter.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Sep 26, 2006
Posts: 69
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

Joined: Sep 06, 2004
Posts: 26
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Aug 03, 2002
Posts: 7729
@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

Joined: Oct 08, 2006
Posts: 2458
Sorry, I was goaling on William + Rajesh indeed.
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
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

Joined: Aug 03, 2002
Posts: 7729
@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

Joined: Sep 06, 2004
Posts: 26
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

Joined: Sep 26, 2006
Posts: 69
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 03, 2002
Posts: 7729
One of my collegues at work came up with this Eclipse Bug Report
William Yan
Ranch Hand

Joined: Sep 26, 2006
Posts: 69
i downloaded eclpise 3.2.
and..
..the answer is CORRECT!
thanks to Jothi and Barry.
Rajesh Kadle
Greenhorn

Joined: Sep 06, 2004
Posts: 26
Thanks all.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why its not incrementing???