• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Ques on operator

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from velmurugan's notes
public class Precedence{
final public static void main(String args[])
{
int i=0;
i=i++;
i=i++;
System.out.println(i);
}
}
o/p is 0
velmurugan's explanation is "=" has lowest precedence.
my question - even if values is not assigned ,value is incremented with"++" operator so why 0
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Precedence{
final public static void main(String args[])
{
int i=0;
i=i++;//the value of i is used (i=0), then it is incremented(i=1), the original value is then assigned to "i" (i back to 0).
i=i++;// same thing
System.out.println(i);
}
}
o/p is 0
velmurugan's explanation is "=" has lowest precedence.
my question - even if values is not assigned ,value is incremented with"++" operator so why?
See above, i is incremented, but then i is assigned back to 0
Lisa
 
Chaitali Deshpande
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lisa
I overlooked the funda.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't see why i isn't incremented after the assignment. Any help would be appreciated.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
Variable i is incremented after the assignment but the incremented value is not the one which is stored in variable i in the expression i=i++. What i gets is the value prior to incrementing it.
I have attempted to explain it..
If you use the postfix increment operator (i++) alone by itself then the variable on which it operates gets incremented and the program flow moves on. But if you use these operator on the right side of another variable assignment (i=i++), then it is incremented AFTER IT IS USED.The incremented value of i is thrown off since the assignment has already been made prior to incrementing it (Remember !! IT IS POSTFIX) . Any subsequent
usage of variable i will give the value stored in it and not the incremented value.
Clear as mud ?
I would suggest, running the same example with prefix increment operator (++x) and see the result
Hope that helps you
-Sandeep Nachane
------------------
Visit my homepage at
www.tipsmart.com
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't find myself able to understand any of the answers provided here.
By my logic, when you say, i=i++; (previous value of i= 0)
what happens in steps is here -
1. i = i; // i =0 at this moment.
2. i = i + 1; // due to i++ that way i is incremented later than the assignment and therefore the final value in i should be 1 and not zero.
I think the trick lies in the fact that we are assigning i++ to i itself. I don't know the trick but surely want to understand. All the explanations based on postfix notation etc. also converge to the same point failing me understanding the thing.
Help plz.
regards
raghav..
[This message has been edited by Raghvendra Sharma (edited February 21, 2001).]
[This message has been edited by Raghvendra Sharma (edited February 21, 2001).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lisa,
Try this........
public class shift{
int i;
public static void main(String sha[]){
shift s=new shift();
s.cal();
}
public void cal(){
int i=0;
i++;
i++;
System.out.println(i);
}
}

What will be the output?
1.0--------------As per ur assignment and increment prescedence .
2.2-------------Actual observed.
Can any body throw some more light on this

Shashank
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if u guys say that the original value is assigned and not the incremented value, how do u explain this

class ex
{
public static void main(String a[]) {
int i=1;
System.out.println(i++ + i++);
System.out.println(((i++) + (i++)));
System.out.println((i++) + (i++));
}
}
The output is 3, 7,11
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Burke:
I still don't see why i isn't incremented after the assignment. Any help would be appreciated.



Michael,
When the same variable(i) is used on both sides of the assignment operator, like
int i=0;
i = i++;
,I split it into two steps and it gives me the correct answer. The above statement now becomes
int k, i=0;
k = i++;
i = k; // i=0
Regards,
Dilip

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try my hand at clearing this up
First of all, the confusion only arises when the increment (or decrement) ops (++) are used in combination with any other operation, including assignment (=).
If we were to use EITHER postfix OR prefix increment op by itself, it would not matter which we were to use, the result would always be i + 1.
But, as soon as you try and evaluate an incremented operand to something, the difference between prefix and postfix becomes very important: Postfix always EVALUATES to the value BEFORE the operation (incrementing or decrementing), whereas Prefix always evaluates to the value AFTER the operation (the way that makes sense). Here is an example:
i = 0 ;
j = i++ ; // j is still zero
j = ++i ; // NOW j is one
BUT, if we were to use either prefix or postfix op WITHOUT evaluating (assigning):
i = 0 ;
i++ ; // i is now one (same as if we had used ++i)
++i ; // i is now two
Hope that helps,
~Ryan

[This message has been edited by ryan burgdorfer (edited February 22, 2001).]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*********************************************
class ex
{
public static void main(String a[]) {
int i=1;
System.out.println(i++ + i++);
System.out.println(((i++) + (i++)));
System.out.println((i++) + (i++));
}
}
The output is 3, 7,11
*********************************************
Let me try to explain with you, sona.
System.out.println(i++ + i++);
Because unary postfix op eval first and plus op has left to right order,
the left i++ is evaluated and the result is 1, but at that time i is incremented to be 2.
then the right i++ is evaluated and the result is 2, and at that time i is incremented to be 3.
From the above result, 1 + 2 returns 3.
The same applies to the following System.out statement.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a little more confusion :-)

it is confusing because the statement is like this:
i=i++;
for a moment, think of i on the LHS as some other variable(say j),
then the statement will be
j=i++;
what would be the result now??? the original value of i, of course!!!

HTH
pankaj
 
Michael Burke
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep and others thanks for the replies. Sandeep I see what you are saying. I just skimmed through these replies as it is late and I have to get up early tomorrow. I guess the moral is don't use postfix ops in this manner.
 
You get good luck from rubbing the belly of a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic