can anybody explain this why its happening
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println(i);
it prints zero.
The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character. The output is: The value of i is: 461012. The printf and format methods are overloaded.
According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.
Java permits the use of multiple assignments in one statement. In such statements, the assignment operators are applied from right to left, rather than from left to right.
If we use the "++" operator as a prefix like ++varOne; , the value of varOne is incremented by one before the value of varOne is returned. If we use ++ operator as postfix like varOne++; , the original value of varOne is returned before varOne is incremented by one.
i++
is a postincrement (JLS 15.14.2). It increments i
, but the result of the expression is the value of i
before the increment. Assigning this value back to i
in effect keeps the value of i
unchanged.
Break it down like this:
int i = 0;
int j = i++;
It's easy to see why j == 0
in this case. Now, instead of j
, we substitute the left hand side with i
. The right hand side value is still 0
, and that's why you get i == 0
in your snippet.
You meant to do this:
int i = 0;
i++;
i++;
i++;
System.out.println(i);
i++
actually does an assignment so if you add an =
you just confuse things. These other fine responders can give you the nitty gritty, some details of it escape me. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With