Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP1.4

 
Anand Ko
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test{
public static void main(String args[]) {
int i=0;
i = i++;
System.out.println(i);
}
}
Answer is : 0.
Can any one tell me what happens to increment operator?
 
Mohan Bhakri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anand

In your program , the expression i = i++ ; is undefined .
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anand,
This topic was discussed many times here. You can do a search. I am sure you'll get interesting results.
Simply put:

According to the JLS the postfix operator always returns its old value then increment it.

Hope this helps.
[ December 04, 2003: Message edited by: Vicken Karaoghlanian ]
 
Jay Rosenthal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the line "i = i++;", the i on the right side of = is not incremented until afterthe value of i is assigned to the i on the left side of =. So, i is still 0 and that's what prints.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic