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] -------------------------------------------------------------------------