Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple q but just to confirm

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class test {
public static void main(String args[]) {
int i,j,k,l=0;
k = l++;
j = ++k;
i = j++;
System.out.println(i);//prints 1
S.o.p(j);//prints 1
S.o.p(k);//prints 1(i have doubt on this ans)
Please could anyone tell me whether my ans are right
Regards
Neha
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it should print 1,2,1
and that result is correct
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output is 1 and 2 and 1
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Results after each statement:
k = l++; // i/0, j/0, k/0, l/1(post increment operator)
j = ++k; // i/0, j/1, k/1, l/1
i = j++; // i/1, j/2, k/1, l/1
so prints 1 2 1
HTH
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx,
i now got it.
I was getting confused in these increments.
thanxs alot once again
Regards
neha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic