File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes simple int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple int" Watch "simple int" New topic

simple int


Joined: Feb 19, 2001
Posts: 6
hello everyone
int i;
result is 0
Lalit Sha

Joined: Jan 11, 2001
Posts: 28
Could you please write the code completely.
Scott Pedigo

Joined: Feb 15, 2001
Posts: 15

[This message has been edited by Scott Pedigo (edited February 19, 2001).]
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
Like Scott says, when you do

i gets incremanted after the assignment, so x==0;
if you did

i would be incremanted before the assignment, so x==1.
You wouldn't normally do i=i++ - perhap's you're thinking of i++ or i+=1 or i=i+1, which all simply increment i by 1.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
As an aside, I feel strongly that pre-increment (++i) works in a much more intutive way than post-increment (i++), and blame lots of textbook and programming standards writers (starting with Kernighan and Richie) for using post-increment so much where it is not needed.
Personally, I always use pre-increment unless I have a real need for the special behaviour of post-increment, and I try and teach my development teams the same habit.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
when u say
int i;
i = i++;
The output is 0 because
int i ;
i is set to 0
then when u say
i = i + +
this means increment the value of i after performing the task assigned
in this case the task is System.out.println(i)
hence o
if u put it in a loop
the next time
i = i ++ is executed
it will print 1

sona<br />SCJP
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
Yeah, I was always taught post-increment, and now I always use it unless I have a reason not to. I suppose pre-increment is more intuative, but you tend to get into a 'style', don't you.
I think maybe people teach i++ because it's more like i=i+1. Or maybe it just rolls of the tongue easier.
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Depends how you say it. I find "increment i" to be more sensible to say that "i increment" (which usually begs the question "you increment what?")
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Java humor

"JavaRanch, where the deer and the Certified play" - David O'Meara
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
i plus plus.
I agree. Here's the link:
subject: simple int
It's not a secret anymore!