Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

increment

 
sitaram irrinki
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test1
{
public static void main(String[] args)
{
int i=0;
System.out.println(i);
i=i++;
System.out.println(i);
i=i++;
System.out.println(i);
i=i++;
System.out.println(i);
}
}

when i run this program then it is output all zero's why.
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct syntax for the ++ operator is:

i++;

That increments the variable i, no need for the i= bit.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very frequently asked question. For example, see this recent thread: http://www.coderanch.com/t/377970/java/java/Increment-Operator
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I finally put it into our FAQ: http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic