• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Increment Operator

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys can any one clear my doubt?

public class Test
{
public static void main(String[] args) {
int i= 1;
i=i++;
i=i++;
i=i++;
i++;
System.out.println("the value of i=" +i);

}

Answer is 2


why the incremented values are not assigning to 'i'?

thanks in advance..
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
suppose you have a expression like
int x= 1 , y = 2 , z = 0;
z = x++ + ++y ;

now what happens is :
1) first the expression on the right hand side is evaluated as below :
a) first all the prefix operators are applied to the operands .
here in the above example since y has prefix operator y becomes 3 .

2) Now the expression is evaluated and the result of the expression is kept in a temporary location.
3) If the operands have any postfix operator that is evaluated for that operand .

4) Now the assignment to the LEFT HAND SIDE takes place . The LEFT HAND SIDE is given the value evaluated in the step 2 mentioned above .

..If you apply above rules correctly you should be able to get that the value of i as 2 .
let us see what happens for
int i = 1;
i= i++;

if you follow my rules above then
Step 1) Not applicable
Step 2) the value of the expression is 1.
Step 3) the value of i becomes 2
Step 4) the value of the expression evaluated to 1 is now assigned to i ( overwriting the value 2 )
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to read this excellent article regarding this topic.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthick and Aalok,

JavaRanch has a policy on display names. Your names are not according to this policy. Your name should consist of a first name, space and a second name.

Please take a moment to carefully read the policy, and then change your name so that it is according to the policy. You can change your name by editing your profile.

Please note that we take the naming policy seriously - which means that we might close your account if you don't change your name.

Jesper Young - Bartender
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is asked quite often. In fact, we have a FAQ on it.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the output is : 5

please chech in the system once.
 
Karthick Thiagarajan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthick T, please check your private messages.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic