aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ques on operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ques on operator" Watch "Ques on operator" New topic
Author

Ques on operator

Chaitali Deshpande
Greenhorn

Joined: Jan 17, 2001
Posts: 26
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
Lisa Yanchunis
Greenhorn

Joined: Nov 03, 2000
Posts: 27
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

Joined: Jan 17, 2001
Posts: 26
Thanks Lisa
I overlooked the funda.
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
I still don't see why i isn't incremented after the assignment. Any help would be appreciated.
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
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


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
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).]
shashank hiwarkar1
Greenhorn

Joined: Feb 11, 2001
Posts: 25
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


shashank
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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

sona<br />SCJP
Dilip Varma
Greenhorn

Joined: Feb 11, 2001
Posts: 3
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

ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
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).]


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
*********************************************
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.
pankaj agarwal
Ranch Hand

Joined: Oct 13, 2000
Posts: 39
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

Joined: Jul 29, 2000
Posts: 103
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ques on operator
 
Similar Threads
Doubt on a sample question from sun
Operators
operator precedence
associativity and precedence...prob..pls..help
question from KAM