Difference between the prefix and postfix forms - Java

Explain the difference between the prefix and postfix forms of the increment operator

The prefix operator ++ adds one to its operand / variable and returns the value before it is assigned to the variable. In other words, the increment takes place first and the assignment next.

The postfix operator ++ adds one to its operand / variable and returns the value only after it is assigned to the variable. In other words, the assignment takes place first and the increment next.
String methods indexOf() and lastIndexOf() - Java
The method indexOf() returns the first occurrence (index) of a given character or a combination as parameter in a given string...
Difference between protected and default access - Java
The default access specifier is one which is not specified with a key word. If no access specifier (any one of private, protected, public) is mentioned it is known as the default access specifier...
Ways that the members of a package can be used by other packages - Java
There are two ways of affecting access levels. One, when the classes in the Java platform are used within the developer defined classes, the access levels determine the members of those class which can be used by the developer defined classes...
Post your comment
Discussion Board
prefix and postfix operator in java
class forLoop
{
public static void main(String args[])
{

for(int a=0;a<=20;a++)

{
System.out.println("value of a:"+a); //VALUE=0

System.out.println("\t "+ ++a); //preFix Increment Operator used //VALUE=1

System.out.println("\t "+a++); //postFix Increment Operator used //VALUE=1

System.out.println("\t "+a); //VALUE=2


System.out.println("\n");
}

}
}

*************OUTPUT****************

value of a:0
1
1
2


value of a:3
4
4
5


value of a:6
7
7
8


value of a:9
10
10
11


value of a:12
13
13
14


value of a:15
16
16
17


value of a:18
19
19
20
Arun Singh 09-10-2013