O seu extrato foi bom mas, n�o est� completa:
"At run time, if evaluation of the operand expression completes
abruptly, then the postfix increment expression completes abruptly for
the same reason and no incrementation occurs. Otherwise, the value 1 is
added to the value of the variable and the sum is stored back into the
variable. Before the addition, binary numeric promotion (�5.6.2) is
performed on the value 1 and the value of the variable. If necessary,
the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the
type of the variable before it is stored. The value of the postfix
increment expression is the value of the variable before the new value
is stored."
Veja bem, o que deve occorer � que
i � atribuido o valor da express�o i++, que � naquele momento 0 e �
gravado no variavel, depois o valor do variavel � incrementada com o
valor de um e este resultdao � gravado no variavel.
sen�o, isso deveria imprimir 0 tb:
int i, j;
i = j = 0;
j = i++;
System.out.println(i);
aqui o J � 0 mas o i � um.
no caso acima h� algo diferente pois a attribui��o do i = i � efeituada,
aparentamente apos " the value 1 is added to the value of the variable
and the sum is stored back into the variable" pois o valor 1 �
sobrescrito com 0.
Br�ulio Consani Moura wrote:
>Caro amigo,
>neste caso a opera��o � pos-fixada, ou seja, primeiro se atribui o valor antes do
>incremento da variavel e entao este ocorre. Este caso � especial, pois se utiliza a
>mesma variavel. Note que o proprio i++ � uma atribuicao de i = i + 1. a linha correta
>seria somente "i++;" ou "i+=1;" para que o resultado fosse 1. No seu caso o valor de
>i permanece como o valor antes do incremento. Como na especificacao:
>"...The value of the postfix increment expression is the value of the variable before
>the new value is stored..."
>
>Resumo:
>O efeito do codigo "i = i++;" � o mesmo efeito que o do codigo "i = i" e nao
>representa efeito igual ao que foi exposto que seria:
> "i = i; i++;" (este efeito nao ocorre para o codigo apresentado)
>
>Espero ter ajudado
>consani
>
>-----Mensagem original-----
>De: Cristiano Bianchi Sanchez [mailto:[EMAIL PROTECTED]]
>Enviada em: ter�a-feira, 26 de fevereiro de 2002 11:59
>Para: [EMAIL PROTECTED]
>Assunto: [java-list] c�digo obscuro!!
>
>
>Oi lista,
>
> Algu�m pode explicar porque a seguinte implementa��o retorna 0 (zero)?
>
> {
> int i = 0;
> i = i++;
> System.out.println(i);
> }
>
> N�o seria mais 'l�gico' retornar 1, como em C++ ?
>
>Abra�os,
>
>CristianoBS
>
>------------------------------ LISTA SOUJAVA ----------------------------
>http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
>d�vidas mais comuns: http://www.soujava.org.br/faq.htm
>regras da lista: http://www.soujava.org.br/regras.htm
>historico: http://www.mail-archive.com/java-list%40soujava.org.br
>para sair da lista: envie email para [EMAIL PROTECTED]
>-------------------------------------------------------------------------
>
>
>------------------------------ LISTA SOUJAVA ----------------------------
>http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
>d�vidas mais comuns: http://www.soujava.org.br/faq.htm
>regras da lista: http://www.soujava.org.br/regras.htm
>historico: http://www.mail-archive.com/java-list%40soujava.org.br
>para sair da lista: envie email para [EMAIL PROTECTED]
>-------------------------------------------------------------------------
>
--
"'You guys (at Microsoft) still don't get it,'
because it's sort of Java with reliability, productivity and security deleted."
James Gosling
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------