Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Valor de Numero estranho

Bom dia galera, escrevi os Getters e Setters das minhas classes normalmente e conseguia imprimir os resultado corretamente . O que eu achei estranho foi que quando eu declaro o seguinte na minha classe principal:

conta.setNumero(0300456);
System.out.println(conta.getNumero());

Compila e roda normalmente, mas o resultado de Numero que aparece não é 0300456 e sim 98606, quando tiro esse primeiro 0 aí sim aparece 300456 como resultado. Gostaria de saber o porque desse primeiro 0 interferir tanto no resultado. Lembrando que o tipo da variável numero está declarada como int.

2 respostas
solução!

Bom dia Gabriel, tudo bem com seus estudos? Espero que sim. Muito interessante a sua pergunta, mas é bem simples de entender o que está acontecendo, olhá só!

Números iniciados com zero, desde que válidos, são interpretados com base 8 (octal), ou seja, 0300456 foi interpretado com base 8 e sua representação em base 10 é 98606.

O manual alerta sobre isso na página sobre integers https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.htmla documentação é completa e sua melhor amiga.

If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8.

getInteger
public static Integer getInteger(String nm,
                 Integer val)
Returns the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value, as per the Integer.decode method, and an Integer object representing this value is returned.
If the property value begins with the two ASCII characters 0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as by the method valueOf(java.lang.String, int) with radix 16.
If the property value begins with the ASCII character 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8.
Otherwise, the property value is parsed as a decimal integer exactly as by the method valueOf(java.lang.String, int) with radix 10.
The second argument is the default value. The default value is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.

Parameters:
nm - property name.
val - default value.
Returns:
the Integer value of the property.
See Also:
System.getProperty(java.lang.String), System.getProperty(java.lang.String, java.lang.String), decode(java.lang.String)

Esse site http://www.statman.info/conversions/octal.html faz conversões entre base 8 X base 10

Então anota aí, quando for declarar seus inteiros explicitamente:

Sem 0 na frente para base 10 (decimal)Com 0 na frente para base 8 (octal)Com 0x ou # na frente para base 16 (hexadecimal)

Entendi Ramon, muito obrigado pela ajuda!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software