Let's assume the original value of 'i' is 0. Think of the steps like this:But yet when we have
i = i++;
shouldn't the post-increment part 'i++' change/increase the value of i from 0 to 1 as it did in the last case.
I don't know how else to put it. The expression "i++" is evaluated to be the original value contained in 'i'. Whether or not 'i' is actually incremented or not is pretty much irrelevant. It's the result of the expression "i++" (the original value) that gets assigned back to 'i'.I am confused too..
=> i = (0++); // replace i by 0
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by huang daobin:
can someone help me
code:
public class A {
public static void main(String arg[]) {
int i = 0;
i = i++;
System.out.println(i);
}
}
the output is 0,why?
Couldn't agree more, but this is the kind of "crap" that shows up repeatedly on mock exams, and it helps to have an understanding of what's going on here for purposes of the SCJP.Ecch. Whatever y'all are doing don't do crap like
int j = k++;
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
This is How should i conclude this topic
i=1;
when i=i++; happens what goes behind the scenes,
= is always of higher precedence than ++,--
so when we execute
i=i++;
TABLE 2 . 1
Operators in Java, in Descending Order of Precedence
Category Operators
Unary
++ -- + - ! ~
(type)
Arithmetic
* / %
+ -
Shift
<< >> >>>
Comparison
< <= > >= instanceof
== !=
Bitwise
& ^ |
Short-circuit
&& ||
Conditional
?:
Assignment
=
op=
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Hi all,
Can anyone explain me:
1. int i= 0;
i=i++ + ++i + ++i;
System.out.println(i);
OUTPUT IS: 5
2. int i= 0;
i=i++ + ++i + --i;
System.out.println(i);
OUTPUT IS: 3
The problem is that the example isn't quite accurate. In both cases, the assignment happens *after* the increment. The terms pre- and post-increment do not refer to any assignment, but rather whether the increment happens before or after the expression "i++" is evaluated. With pre-increment, the value is incremented before the expression is evaluated. With the postfix increment operator, the value is incremented after the expression is evaluated; i.e., per the JLS, the value of the postfix increment expression is the value of the variable before the new value is stored.here is some useful example for u.
NOTE : the unary operation is perform on binary.
that is b++; is operation on binary
and b=b+1; is operation on integer.
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|